|
| 1 | +using AiDotNet.Enums; |
| 2 | + |
| 3 | +namespace AiDotNet.Deployment.Configuration; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Configuration for model compression - reducing model size while preserving accuracy. |
| 7 | +/// </summary> |
| 8 | +/// <remarks> |
| 9 | +/// <para><b>For Beginners:</b> Model compression makes your trained AI model smaller and faster to load. |
| 10 | +/// Think of it like compressing a ZIP file - you get a smaller file that can be restored to its original form. |
| 11 | +/// |
| 12 | +/// Why use compression? |
| 13 | +/// - Smaller model files (50-90% size reduction) |
| 14 | +/// - Faster model loading and deployment |
| 15 | +/// - Lower storage and bandwidth costs |
| 16 | +/// - Enables deployment on resource-constrained devices |
| 17 | +/// |
| 18 | +/// Trade-offs: |
| 19 | +/// - Some compression types are lossy (slight accuracy reduction, typically 1-2%) |
| 20 | +/// - Compression/decompression adds a small processing overhead |
| 21 | +/// |
| 22 | +/// Compression happens automatically when you save (serialize) a model and |
| 23 | +/// decompression happens automatically when you load (deserialize) it. |
| 24 | +/// You never need to handle compression manually. |
| 25 | +/// |
| 26 | +/// Example: |
| 27 | +/// <code> |
| 28 | +/// // Use automatic compression (recommended for most cases) |
| 29 | +/// var result = await builder |
| 30 | +/// .ConfigureModel(model) |
| 31 | +/// .ConfigureCompression() |
| 32 | +/// .BuildAsync(x, y); |
| 33 | +/// |
| 34 | +/// // Or customize compression settings |
| 35 | +/// var result = await builder |
| 36 | +/// .ConfigureCompression(new CompressionConfig |
| 37 | +/// { |
| 38 | +/// Mode = CompressionMode.Full, |
| 39 | +/// Type = CompressionType.HybridHuffmanClustering, |
| 40 | +/// NumClusters = 256 |
| 41 | +/// }) |
| 42 | +/// .BuildAsync(x, y); |
| 43 | +/// </code> |
| 44 | +/// </para> |
| 45 | +/// </remarks> |
| 46 | +public class CompressionConfig |
| 47 | +{ |
| 48 | + /// <summary> |
| 49 | + /// Gets or sets the compression mode (default: Automatic). |
| 50 | + /// </summary> |
| 51 | + /// <remarks> |
| 52 | + /// <para><b>For Beginners:</b> Choose how compression is applied: |
| 53 | + /// - None: No compression (full size, maximum accuracy) |
| 54 | + /// - Automatic: System chooses best approach (recommended) |
| 55 | + /// - WeightsOnly: Compress only model weights |
| 56 | + /// - Full: Compress entire serialized model |
| 57 | + /// </para> |
| 58 | + /// </remarks> |
| 59 | + public ModelCompressionMode Mode { get; set; } = ModelCompressionMode.Automatic; |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Gets or sets the compression algorithm type (default: WeightClustering). |
| 63 | + /// </summary> |
| 64 | + /// <remarks> |
| 65 | + /// <para><b>For Beginners:</b> Different algorithms offer different trade-offs: |
| 66 | + /// - WeightClustering: Groups similar weights (good balance of speed and compression) |
| 67 | + /// - HuffmanEncoding: Lossless variable-length encoding (no accuracy loss) |
| 68 | + /// - HybridHuffmanClustering: Combines both for maximum compression |
| 69 | + /// </para> |
| 70 | + /// </remarks> |
| 71 | + public CompressionType Type { get; set; } = CompressionType.WeightClustering; |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Gets or sets the number of clusters for weight clustering (default: 256). |
| 75 | + /// </summary> |
| 76 | + /// <remarks> |
| 77 | + /// <para><b>For Beginners:</b> This is like choosing how many "bins" to sort weights into. |
| 78 | + /// 256 clusters is the industry standard (equivalent to 8-bit quantization). |
| 79 | + /// More clusters = higher accuracy but less compression. |
| 80 | + /// Fewer clusters = more compression but lower accuracy. |
| 81 | + /// |
| 82 | + /// Common values: |
| 83 | + /// - 16: Aggressive compression (4-bit equivalent) |
| 84 | + /// - 256: Standard compression (8-bit equivalent, recommended) |
| 85 | + /// - 65536: Light compression (16-bit equivalent) |
| 86 | + /// </para> |
| 87 | + /// </remarks> |
| 88 | + public int NumClusters { get; set; } = 256; |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Gets or sets the decimal precision for Huffman encoding (default: 4). |
| 92 | + /// </summary> |
| 93 | + /// <remarks> |
| 94 | + /// <para><b>For Beginners:</b> Controls how many decimal places to keep when rounding weights |
| 95 | + /// for Huffman encoding. Higher precision = better accuracy but less compression. |
| 96 | + /// 4 decimal places is a good default for most models. |
| 97 | + /// </para> |
| 98 | + /// </remarks> |
| 99 | + public int Precision { get; set; } = 4; |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Gets or sets the convergence tolerance for clustering algorithms (default: 1e-6). |
| 103 | + /// </summary> |
| 104 | + /// <remarks> |
| 105 | + /// <para><b>For Beginners:</b> This determines when the clustering algorithm stops iterating. |
| 106 | + /// Smaller values = more precise clusters but slower compression. |
| 107 | + /// The default (0.000001) works well for most cases. |
| 108 | + /// </para> |
| 109 | + /// </remarks> |
| 110 | + public double Tolerance { get; set; } = 1e-6; |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Gets or sets the maximum iterations for clustering algorithms (default: 100). |
| 114 | + /// </summary> |
| 115 | + /// <remarks> |
| 116 | + /// <para><b>For Beginners:</b> Limits how long the clustering algorithm runs. |
| 117 | + /// More iterations can improve cluster quality but takes longer. |
| 118 | + /// 100 iterations is sufficient for most models. |
| 119 | + /// </para> |
| 120 | + /// </remarks> |
| 121 | + public int MaxIterations { get; set; } = 100; |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// Gets or sets the random seed for reproducible compression (default: null for random). |
| 125 | + /// </summary> |
| 126 | + /// <remarks> |
| 127 | + /// <para><b>For Beginners:</b> Set this to a specific number if you want compression |
| 128 | + /// to produce identical results every time. Useful for testing and debugging. |
| 129 | + /// Leave as null for normal usage. |
| 130 | + /// </para> |
| 131 | + /// </remarks> |
| 132 | + public int? RandomSeed { get; set; } |
| 133 | + |
| 134 | + /// <summary> |
| 135 | + /// Gets or sets the maximum acceptable accuracy loss percentage (default: 2.0). |
| 136 | + /// </summary> |
| 137 | + /// <remarks> |
| 138 | + /// <para><b>For Beginners:</b> If compression would cause more than this percentage |
| 139 | + /// of accuracy loss, the system will warn you or use a less aggressive compression. |
| 140 | + /// 2% is acceptable for most applications. Set to 0 for lossless compression only. |
| 141 | + /// </para> |
| 142 | + /// </remarks> |
| 143 | + public double MaxAccuracyLossPercent { get; set; } = 2.0; |
| 144 | +} |
0 commit comments