|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { |
| 4 | + MIN_SOLE_DEFAULT_CURVES, |
| 5 | + countCurvesByPrecision, |
| 6 | + pickDefaultPrecisions, |
| 7 | + resolveEffectivePrecisions, |
| 8 | +} from './default-precisions'; |
| 9 | + |
| 10 | +function row(precision: string, hardware: string, framework = 'vllm', disagg = false) { |
| 11 | + return { precision, hardware, framework, spec_method: 'none', disagg }; |
| 12 | +} |
| 13 | + |
| 14 | +describe('countCurvesByPrecision', () => { |
| 15 | + it('counts distinct (hardware, framework, spec_method, disagg) per precision', () => { |
| 16 | + const counts = countCurvesByPrecision([ |
| 17 | + row('fp8', 'b200'), |
| 18 | + row('fp8', 'b300'), |
| 19 | + row('fp8', 'b200'), // dup curve, not counted again |
| 20 | + row('fp4', 'mi355x', 'atom'), |
| 21 | + ]); |
| 22 | + expect(counts).toEqual({ fp8: 2, fp4: 1 }); |
| 23 | + }); |
| 24 | + |
| 25 | + it('treats disagg and non-disagg of the same hw as distinct curves', () => { |
| 26 | + const counts = countCurvesByPrecision([ |
| 27 | + row('fp8', 'b200', 'vllm', false), |
| 28 | + row('fp8', 'b200', 'vllm', true), |
| 29 | + ]); |
| 30 | + expect(counts).toEqual({ fp8: 2 }); |
| 31 | + }); |
| 32 | + |
| 33 | + it('returns {} for no rows', () => { |
| 34 | + expect(countCurvesByPrecision([])).toEqual({}); |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +describe('pickDefaultPrecisions', () => { |
| 39 | + it('picks the single densest precision only when every precision clears the threshold', () => { |
| 40 | + // dsr1 shape: both dense. |
| 41 | + expect(pickDefaultPrecisions({ fp4: 23, fp8: 38 })).toEqual(['fp8']); |
| 42 | + }); |
| 43 | + |
| 44 | + it('shows both when one precision is below the threshold (MiniMax M3 shape)', () => { |
| 45 | + // fp4 barren (1 curve) next to a dense fp8 → surface both, not just fp8. |
| 46 | + expect(pickDefaultPrecisions({ fp4: 1, fp8: 14 })).toEqual(['fp4', 'fp8']); |
| 47 | + }); |
| 48 | + |
| 49 | + it('keeps fp4 when it is the densest and both clear the threshold (dsv4 shape)', () => { |
| 50 | + expect(pickDefaultPrecisions({ fp4: 28, fp8: 5 })).toEqual(['fp4']); |
| 51 | + }); |
| 52 | + |
| 53 | + it('breaks ties in favor of fp4 when both clear the threshold', () => { |
| 54 | + expect(pickDefaultPrecisions({ fp4: 8, fp8: 8 })).toEqual(['fp4']); |
| 55 | + }); |
| 56 | + |
| 57 | + it('breaks non-fp4 ties by canonical enum order', () => { |
| 58 | + // fp8 precedes bf16 in PRECISION_OPTIONS. |
| 59 | + expect(pickDefaultPrecisions({ bf16: 6, fp8: 6 })).toEqual(['fp8']); |
| 60 | + }); |
| 61 | + |
| 62 | + it('surfaces all precisions (sorted) when any is below threshold', () => { |
| 63 | + expect(pickDefaultPrecisions({ fp8: 3, fp4: 2 })).toEqual(['fp4', 'fp8']); |
| 64 | + // llama70b shape: fp4 sparse (3), fp8 dense (8) → both. |
| 65 | + expect(pickDefaultPrecisions({ fp4: 3, fp8: 8 })).toEqual(['fp4', 'fp8']); |
| 66 | + expect(MIN_SOLE_DEFAULT_CURVES).toBe(4); |
| 67 | + }); |
| 68 | + |
| 69 | + it('returns the lone precision for a single-precision model regardless of count', () => { |
| 70 | + expect(pickDefaultPrecisions({ fp4: 2 })).toEqual(['fp4']); |
| 71 | + expect(pickDefaultPrecisions({ fp4: 10 })).toEqual(['fp4']); |
| 72 | + }); |
| 73 | + |
| 74 | + it('returns [] when there are no precisions', () => { |
| 75 | + expect(pickDefaultPrecisions({})).toEqual([]); |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +describe('resolveEffectivePrecisions', () => { |
| 80 | + const M3_COUNTS = { fp4: 1, fp8: 14 }; |
| 81 | + const M3_AVAIL = ['fp4', 'fp8']; |
| 82 | + |
| 83 | + it('auto-defaults to both when one precision is sparse (M3 → fp4 + fp8)', () => { |
| 84 | + expect( |
| 85 | + resolveEffectivePrecisions({ |
| 86 | + selectedPrecisions: ['fp4'], |
| 87 | + availablePrecisions: M3_AVAIL, |
| 88 | + curveCounts: M3_COUNTS, |
| 89 | + explicit: false, |
| 90 | + }), |
| 91 | + ).toEqual(['fp4', 'fp8']); |
| 92 | + }); |
| 93 | + |
| 94 | + it('auto-defaults to the densest precision when every precision is dense (dsr1 → fp8)', () => { |
| 95 | + expect( |
| 96 | + resolveEffectivePrecisions({ |
| 97 | + selectedPrecisions: ['fp4'], |
| 98 | + availablePrecisions: ['fp4', 'fp8'], |
| 99 | + curveCounts: { fp4: 23, fp8: 38 }, |
| 100 | + explicit: false, |
| 101 | + }), |
| 102 | + ).toEqual(['fp8']); |
| 103 | + }); |
| 104 | + |
| 105 | + it('leaves an unchanged model on fp4 when fp4 is densest and both are dense', () => { |
| 106 | + expect( |
| 107 | + resolveEffectivePrecisions({ |
| 108 | + selectedPrecisions: ['fp4'], |
| 109 | + availablePrecisions: ['fp4', 'fp8'], |
| 110 | + curveCounts: { fp4: 28, fp8: 5 }, |
| 111 | + explicit: false, |
| 112 | + }), |
| 113 | + ).toEqual(['fp4']); |
| 114 | + }); |
| 115 | + |
| 116 | + it('honors an explicit selection even when it is the sparse precision', () => { |
| 117 | + expect( |
| 118 | + resolveEffectivePrecisions({ |
| 119 | + selectedPrecisions: ['fp4'], |
| 120 | + availablePrecisions: M3_AVAIL, |
| 121 | + curveCounts: M3_COUNTS, |
| 122 | + explicit: true, |
| 123 | + }), |
| 124 | + ).toEqual(['fp4']); |
| 125 | + }); |
| 126 | + |
| 127 | + it('honors an explicit multi-precision selection (e.g. a preset)', () => { |
| 128 | + expect( |
| 129 | + resolveEffectivePrecisions({ |
| 130 | + selectedPrecisions: ['fp4', 'fp8'], |
| 131 | + availablePrecisions: M3_AVAIL, |
| 132 | + curveCounts: M3_COUNTS, |
| 133 | + explicit: true, |
| 134 | + }), |
| 135 | + ).toEqual(['fp4', 'fp8']); |
| 136 | + }); |
| 137 | + |
| 138 | + it('drops explicitly-selected precisions that are unavailable, falling back to first available', () => { |
| 139 | + expect( |
| 140 | + resolveEffectivePrecisions({ |
| 141 | + selectedPrecisions: ['fp8'], |
| 142 | + availablePrecisions: ['fp4'], |
| 143 | + curveCounts: { fp4: 10 }, |
| 144 | + explicit: true, |
| 145 | + }), |
| 146 | + ).toEqual(['fp4']); |
| 147 | + }); |
| 148 | + |
| 149 | + it('includes a loaded unofficial run precision so the overlay is visible by default', () => { |
| 150 | + // Both official precisions dense → base is the sole densest (fp8); the user |
| 151 | + // opened an fp4 unofficial run, so fp4 must be merged in. |
| 152 | + expect( |
| 153 | + resolveEffectivePrecisions({ |
| 154 | + selectedPrecisions: ['fp4'], |
| 155 | + availablePrecisions: ['fp4', 'fp8'], |
| 156 | + curveCounts: { fp4: 23, fp8: 38 }, |
| 157 | + unofficialPrecisions: ['fp4'], |
| 158 | + explicit: false, |
| 159 | + }), |
| 160 | + ).toEqual(['fp4', 'fp8']); |
| 161 | + }); |
| 162 | + |
| 163 | + it('ignores unofficial precisions that have no available data', () => { |
| 164 | + expect( |
| 165 | + resolveEffectivePrecisions({ |
| 166 | + selectedPrecisions: ['fp4'], |
| 167 | + availablePrecisions: ['fp4', 'fp8'], |
| 168 | + curveCounts: { fp4: 23, fp8: 38 }, |
| 169 | + unofficialPrecisions: ['int4'], |
| 170 | + explicit: false, |
| 171 | + }), |
| 172 | + ).toEqual(['fp8']); |
| 173 | + }); |
| 174 | + |
| 175 | + it('falls back to the first available precision when curve data is missing (still loading)', () => { |
| 176 | + expect( |
| 177 | + resolveEffectivePrecisions({ |
| 178 | + selectedPrecisions: ['fp4'], |
| 179 | + availablePrecisions: ['fp4'], |
| 180 | + curveCounts: {}, |
| 181 | + explicit: false, |
| 182 | + }), |
| 183 | + ).toEqual(['fp4']); |
| 184 | + }); |
| 185 | +}); |
0 commit comments