Skip to content

Bit-width allocation

Given a storage limit and predicted losses at several precisions, choose one bit width per layer. A one-bit upgrade to a large layer can consume enough storage for two smaller upgrades. Compare exhaustive search, greedy upgrades, and exact dynamic programming on the same tables.

Given a storage limit and predicted losses at several precisions, choose one bit width per layer. A one-bit upgrade to a large layer can consume enough storage for two smaller upgrades. Compare exhaustive search, greedy upgrades, and exact dynamic programming on the same tables.

At nine layers, dynamic programming matches exhaustive loss in 0.0714 ms against 3.6329 ms

  • knotkit/prng: Generates seeded pseudorandom numbers and unbiased integers for reproducible simulations and tests
  • knotkit/errors: Provides the base error classes every Knotkit package throws from, discriminated by package, category, and cause
{"v":1,"box":[67.9028,107.6111,733.2082,547.8349],"x":{"s":"linear","lim":[1437.86,3064.14],"inv":false,"g":0,"t":"Storage budget on the 48-layer model (bits)","tk":[[1505,"1,505"],[2000,"2,000"],[2500,"2,500"],[2997,"2,997"]]},"y":{"s":"linear","lim":[-0.0563338301043,1.30819672131],"inv":false,"g":0,"tk":[[0,"0"],[0.5,"0.5"],[1,"1"],[1.25186289121,"1.25"]]},"ti":"Greedy is optimal at 20 of 42 budgets, at most 1.25% worse","a":[{"k":"scatter","c":"#1d5fa6","s":"xy","d":[1505,0,1543,0.136260408781,1581,0,1619,0.235312573535,1657,0,1695,0,1733,0.496560285522,1771,0.484679665738,1809,0.757530211026,1847,0.622648851991,1885,0.356668298482,1923,0.524116976833,1961,0,1999,0.377121307354,2037,0,2064,0.156772575251,2075,0.160909676035,2113,0.174865936116,2151,0.26491737101,2189,0.486552236789,2227,0.261970601077,2265,0,2303,0,2341,0.585002659103,2379,0,2417,0.180469219972,2455,0,2493,0.136955033097,2531,0.440097799511,2569,0,2607,0,2645,1.25186289121,2683,0,2721,0,2759,0,2797,0.651937703731,2835,0,2873,0.930954228084,2911,0,2949,0,2987,0,2997,0],"p":[95.37,529.66,110.92,485.7,126.46,529.66,142.01,453.74,157.55,529.66,173.1,529.66,188.64,369.46,204.19,373.29,219.73,285.27,235.28,328.78,250.83,414.59,266.37,360.57,281.92,529.66,297.46,407.99,313.01,529.66,324.05,479.08,328.55,477.75,344.1,473.25,359.65,444.19,375.19,372.69,390.74,445.14,406.28,529.66,421.83,529.66,437.37,340.93,452.92,529.66,468.47,471.44,484.01,529.66,499.56,485.48,515.1,387.68,530.65,529.66,546.19,529.66,561.74,125.79,577.29,529.66,592.83,529.66,608.38,529.66,623.92,319.33,639.47,529.66,655.01,229.32,670.56,529.66,686.1,529.66,701.65,529.66,705.74,529.66],"r":3.51},{"k":"scatter","c":"#c4501b","s":"xy","d":[2064,0.156772575251],"p":[324.05,479.08],"r":5.67},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#5c5c57"}],"lg":[]} 1,505 2,000 2,500 2,997 Storage budget on the 48-layer model (bits) 0 0.5 1 1.25 worst: +1.25% at 2645 bits fixture: +0.157% Greedy loss above the optimum (%) Greedy is optimal at 20 of 42 budgets, at most 1.25% worse {"v":1,"box":[1006.6946,107.6111,1672,547.8349],"x":{"s":"linear","lim":[0,7.74],"inv":false,"g":1,"t":"Mean ms at the fixture budget (Apple M1 Pro, Bun 1.4.4)","tk":[[0,"0"],[2,"2"],[4,"4"],[6,"6"]]},"y":{"s":"linear","lim":[-0.65,1.65],"inv":false,"g":1,"tk":[[1,"Greedy upgrades"],[0,"Dynamic programming"]]},"ti":"Solve time, 10× apart","a":[{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[0.5601,1],"p":[1054.84,232.02],"r":6.09},{"k":"annotation","c":"#1d5fa6"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#5c5c57","s":"xy","d":[5.5649,0],"p":[1485.04,423.42],"r":6.09},{"k":"annotation","c":"#2b2b28"}],"lg":[]} 0 2 4 6 Mean ms at the fixture budget (Apple M1 Pro, Bun 1.4.4) Greedy upgrades Dynamic programming 560 µs 5.56 ms Solve time, 10× apart
{"v":1,"box":[248.8628,139.8767,677.2357,555.9677],"x":{"s":"linear","lim":[1437.86,3064.14],"inv":false,"g":0,"t":"Storage budget on the 48-layer model (bits)","tk":[[1505,"1,505"],[2000,"2,000"],[2500,"2,500"],[2997,"2,997"]]},"y":{"s":"linear","lim":[-0.0563338301043,1.30819672131],"inv":false,"g":0,"tk":[[0,"0"],[0.5,"0.5"],[1,"1"],[1.25186289121,"1.25"]]},"ti":"Greedy is optimal at 20 of 42 budgets, at\nmost 1.25% worse","a":[{"k":"scatter","c":"#1d5fa6","s":"xy","d":[1505,0,1543,0.136260408781,1581,0,1619,0.235312573535,1657,0,1695,0,1733,0.496560285522,1771,0.484679665738,1809,0.757530211026,1847,0.622648851991,1885,0.356668298482,1923,0.524116976833,1961,0,1999,0.377121307354,2037,0,2064,0.156772575251,2075,0.160909676035,2113,0.174865936116,2151,0.26491737101,2189,0.486552236789,2227,0.261970601077,2265,0,2303,0,2341,0.585002659103,2379,0,2417,0.180469219972,2455,0,2493,0.136955033097,2531,0.440097799511,2569,0,2607,0,2645,1.25186289121,2683,0,2721,0,2759,0,2797,0.651937703731,2835,0,2873,0.930954228084,2911,0,2949,0,2987,0,2997,0],"p":[266.55,538.79,276.56,497.24,286.57,538.79,296.58,467.03,306.59,538.79,316.6,538.79,326.6,387.37,336.61,390.99,346.62,307.79,356.63,348.92,366.64,430.03,376.65,378.97,386.66,538.79,396.67,423.79,406.68,538.79,413.79,490.98,416.69,489.72,426.7,485.47,436.71,458.01,446.72,390.42,456.73,458.91,466.74,538.79,476.75,538.79,486.76,360.4,496.77,538.79,506.77,483.76,516.78,538.79,526.79,497.03,536.8,404.59,546.81,538.79,556.82,538.79,566.83,157.05,576.84,538.79,586.85,538.79,596.86,538.79,606.87,339.99,616.88,538.79,626.89,254.91,636.9,538.79,646.91,538.79,656.92,538.79,659.55,538.79],"r":3.51},{"k":"scatter","c":"#c4501b","s":"xy","d":[2064,0.156772575251],"p":[413.79,490.98],"r":5.67},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#5c5c57"}],"lg":[]} 1,505 2,000 2,500 2,997 Storage budget on the 48-layer model (bits) 0 0.5 1 1.25 worst: +1.25% at 2645 bits fixture: +0.157% Greedy loss above the optimum (%) Greedy is optimal at 20 of 42 budgets, at most 1.25% worse {"v":1,"box":[248.8628,771.7439,677.2357,1187.8349],"x":{"s":"linear","lim":[0,7.74],"inv":false,"g":1,"t":"Mean ms at the fixture budget (Apple M1 Pro, Bun 1.4.4)","tk":[[0,"0"],[2,"2"],[4,"4"],[6,"6"]]},"y":{"s":"linear","lim":[-0.65,1.65],"inv":false,"g":1,"tk":[[1,"Greedy upgrades"],[0,"Dynamic programming"]]},"ti":"Solve time, 10× apart","a":[{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[0.5601,1],"p":[279.86,889.33],"r":6.09},{"k":"annotation","c":"#1d5fa6"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#5c5c57","s":"xy","d":[5.5649,0],"p":[556.85,1070.24],"r":6.09},{"k":"annotation","c":"#2b2b28"}],"lg":[]} 0 2 4 6 Mean ms at the fixture budget (Apple M1 Pro, Bun 1.4.4) Greedy upgrades Dynamic programming 560 µs 5.56 ms Solve time, 10× apart
{"v":1,"box":[44.6024,94.1597,1043,437.3555],"x":{"s":"linear","lim":[24.55,35.45],"inv":false,"g":0,"t":"Storage budget including overhead (bits)","tk":[[25,"25"],[26,"26"],[27,"27"],[28,"28"],[29,"29"],[30,"30"],[31,"31"],[32,"32"],[33,"33"],[34,"34"],[35,"35"]]},"y":{"s":"linear","lim":[-0.945,21.945],"inv":false,"g":0,"tk":[[0,"0"],[6,"6"],[9,"9"],[12,"12"],[15,"15"],[21,"21"]]},"ti":"Greedy ties the optimum at 10 of 11 budgets and loses 3 at 31 bits","a":[{"k":"line","c":"#1d5fa6","s":"x","d":[25,21,26,21,27,21,28,15,29,12,30,12,31,9,32,6,33,6,34,6,35,0],"p":[85.82,108.33,177.42,108.33,269.01,108.33,360.61,198.29,452.21,243.27,543.8,243.27,635.4,288.25,726.99,333.23,818.59,333.23,910.19,333.23,1001.78,423.19],"ds":"post"},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[25,21,26,21,27,21,28,15,29,12,30,12,31,9,32,6,33,6,34,6,35,0],"p":[85.82,108.33,177.42,108.33,269.01,108.33,360.61,198.29,452.21,243.27,543.8,243.27,635.4,288.25,726.99,333.23,818.59,333.23,910.19,333.23,1001.78,423.19],"r":4.96},{"k":"scatter","c":"#c4501b","s":"xy","d":[31,12],"p":[635.4,243.27],"r":4.96},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#5c5c57"}],"lg":[]} 25 26 27 28 29 30 31 32 33 34 35 Storage budget including overhead (bits) 0 6 9 12 15 21 greedy [3, 2, 2]: 29 bits used, loss 12 optimum [2, 3, 3]: 31 bits, loss 9 Predicted loss (lower is better) Greedy ties the optimum at 10 of 11 budgets and loses 3 at 31 bits
{"v":1,"box":[44.6024,122.3921,595,437.3555],"x":{"s":"linear","lim":[24.55,35.45],"inv":false,"g":0,"t":"Storage budget including overhead (bits)","tk":[[25,"25"],[26,"26"],[27,"27"],[28,"28"],[29,"29"],[30,"30"],[31,"31"],[32,"32"],[33,"33"],[34,"34"],[35,"35"]]},"y":{"s":"linear","lim":[-0.945,21.945],"inv":false,"g":0,"tk":[[0,"0"],[6,"6"],[9,"9"],[12,"12"],[15,"15"],[21,"21"]]},"ti":"Greedy ties the optimum at 10 of 11\nbudgets and loses 3 at 31 bits","a":[{"k":"line","c":"#1d5fa6","s":"x","d":[25,21,26,21,27,21,28,15,29,12,30,12,31,9,32,6,33,6,34,6,35,0],"p":[67.33,135.4,117.82,135.4,168.32,135.4,218.81,217.95,269.31,259.23,319.8,259.23,370.3,300.51,420.79,341.79,471.29,341.79,521.78,341.79,572.28,424.35],"ds":"post"},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[25,21,26,21,27,21,28,15,29,12,30,12,31,9,32,6,33,6,34,6,35,0],"p":[67.33,135.4,117.82,135.4,168.32,135.4,218.81,217.95,269.31,259.23,319.8,259.23,370.3,300.51,420.79,341.79,471.29,341.79,521.78,341.79,572.28,424.35],"r":4.96},{"k":"scatter","c":"#c4501b","s":"xy","d":[31,12],"p":[370.3,259.23],"r":4.96},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#5c5c57"}],"lg":[]} 25 26 27 28 29 30 31 32 33 34 35 Storage budget including overhead (bits) 0 6 9 12 15 21 greedy [3, 2, 2]: 29 bits used, loss 12 optimum [2, 3, 3]: 31 bits, loss 9 Predicted loss (lower is better) Greedy ties the optimum at 10 of 11 budgets and loses 3 at 31 bits
  • 48 layers, 2064 bits available. Dynamic programming finds the minimum predicted loss of 9568 using 2064 bits.
  • Greedy uses 2063 bits for loss 9583. Mean solve time: 560 µs greedy, 5.56 ms exact (M1 Pro, Bun 1.4.4).
  • On the three-layer example at 31 bits, greedy chooses widths [3, 2, 2] for loss 12. The optimum is [2, 3, 3] for loss 9.

Hover or focus a chart for exact values. Drawn by demo.ts at build time, from the fixtures the benchmark uses; it reproduces the README figure.

Terminal window
git clone https://github.com/jkomyno/knotkit.git && cd knotkit && bun install
bun run demo:problem:bit-width-allocation

problems/bit-width-allocation/README.md states the fixture, the seed, and the result table.