astcenc_weight_align.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. // SPDX-License-Identifier: Apache-2.0
  2. // ----------------------------------------------------------------------------
  3. // Copyright 2011-2023 Arm Limited
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  6. // use this file except in compliance with the License. You may obtain a copy
  7. // of the License at:
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. // License for the specific language governing permissions and limitations
  15. // under the License.
  16. // ----------------------------------------------------------------------------
  17. #if !defined(ASTCENC_DECOMPRESS_ONLY)
  18. /**
  19. * @brief Functions for angular-sum algorithm for weight alignment.
  20. *
  21. * This algorithm works as follows:
  22. * - we compute a complex number P as (cos s*i, sin s*i) for each weight,
  23. * where i is the input value and s is a scaling factor based on the spacing between the weights.
  24. * - we then add together complex numbers for all the weights.
  25. * - we then compute the length and angle of the resulting sum.
  26. *
  27. * This should produce the following results:
  28. * - perfect alignment results in a vector whose length is equal to the sum of lengths of all inputs
  29. * - even distribution results in a vector of length 0.
  30. * - all samples identical results in perfect alignment for every scaling.
  31. *
  32. * For each scaling factor within a given set, we compute an alignment factor from 0 to 1. This
  33. * should then result in some scalings standing out as having particularly good alignment factors;
  34. * we can use this to produce a set of candidate scale/shift values for various quantization levels;
  35. * we should then actually try them and see what happens.
  36. */
  37. #include "astcenc_internal.h"
  38. #include "astcenc_vecmathlib.h"
  39. #include <stdio.h>
  40. #include <cassert>
  41. #include <cstring>
  42. static constexpr unsigned int ANGULAR_STEPS { 32 };
  43. static_assert((ANGULAR_STEPS % ASTCENC_SIMD_WIDTH) == 0,
  44. "ANGULAR_STEPS must be multiple of ASTCENC_SIMD_WIDTH");
  45. static_assert(ANGULAR_STEPS >= 32,
  46. "ANGULAR_STEPS must be at least max(steps_for_quant_level)");
  47. // Store a reduced sin/cos table for 64 possible weight values; this causes
  48. // slight quality loss compared to using sin() and cos() directly. Must be 2^N.
  49. static constexpr unsigned int SINCOS_STEPS { 64 };
  50. static const uint8_t steps_for_quant_level[12] {
  51. 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 24, 32
  52. };
  53. alignas(ASTCENC_VECALIGN) static float sin_table[SINCOS_STEPS][ANGULAR_STEPS];
  54. alignas(ASTCENC_VECALIGN) static float cos_table[SINCOS_STEPS][ANGULAR_STEPS];
  55. #if defined(ASTCENC_DIAGNOSTICS)
  56. static bool print_once { true };
  57. #endif
  58. /* See header for documentation. */
  59. void prepare_angular_tables()
  60. {
  61. for (unsigned int i = 0; i < ANGULAR_STEPS; i++)
  62. {
  63. float angle_step = static_cast<float>(i + 1);
  64. for (unsigned int j = 0; j < SINCOS_STEPS; j++)
  65. {
  66. sin_table[j][i] = static_cast<float>(sinf((2.0f * astc::PI / (SINCOS_STEPS - 1.0f)) * angle_step * static_cast<float>(j)));
  67. cos_table[j][i] = static_cast<float>(cosf((2.0f * astc::PI / (SINCOS_STEPS - 1.0f)) * angle_step * static_cast<float>(j)));
  68. }
  69. }
  70. }
  71. /**
  72. * @brief Compute the angular alignment factors and offsets.
  73. *
  74. * @param weight_count The number of (decimated) weights.
  75. * @param dec_weight_ideal_value The ideal decimated unquantized weight values.
  76. * @param max_angular_steps The maximum number of steps to be tested.
  77. * @param[out] offsets The output angular offsets array.
  78. */
  79. static void compute_angular_offsets(
  80. unsigned int weight_count,
  81. const float* dec_weight_ideal_value,
  82. unsigned int max_angular_steps,
  83. float* offsets
  84. ) {
  85. promise(weight_count > 0);
  86. promise(max_angular_steps > 0);
  87. alignas(ASTCENC_VECALIGN) int isamplev[BLOCK_MAX_WEIGHTS];
  88. // Precompute isample; arrays are always allocated 64 elements long
  89. for (unsigned int i = 0; i < weight_count; i += ASTCENC_SIMD_WIDTH)
  90. {
  91. // Add 2^23 and interpreting bits extracts round-to-nearest int
  92. vfloat sample = loada(dec_weight_ideal_value + i) * (SINCOS_STEPS - 1.0f) + vfloat(12582912.0f);
  93. vint isample = float_as_int(sample) & vint((SINCOS_STEPS - 1));
  94. storea(isample, isamplev + i);
  95. }
  96. // Arrays are multiple of SIMD width (ANGULAR_STEPS), safe to overshoot max
  97. vfloat mult = vfloat(1.0f / (2.0f * astc::PI));
  98. for (unsigned int i = 0; i < max_angular_steps; i += ASTCENC_SIMD_WIDTH)
  99. {
  100. vfloat anglesum_x = vfloat::zero();
  101. vfloat anglesum_y = vfloat::zero();
  102. for (unsigned int j = 0; j < weight_count; j++)
  103. {
  104. int isample = isamplev[j];
  105. anglesum_x += loada(cos_table[isample] + i);
  106. anglesum_y += loada(sin_table[isample] + i);
  107. }
  108. vfloat angle = atan2(anglesum_y, anglesum_x);
  109. vfloat ofs = angle * mult;
  110. storea(ofs, offsets + i);
  111. }
  112. }
  113. /**
  114. * @brief For a given step size compute the lowest and highest weight.
  115. *
  116. * Compute the lowest and highest weight that results from quantizing using the given stepsize and
  117. * offset, and then compute the resulting error. The cut errors indicate the error that results from
  118. * forcing samples that should have had one weight value one step up or down.
  119. *
  120. * @param weight_count The number of (decimated) weights.
  121. * @param dec_weight_ideal_value The ideal decimated unquantized weight values.
  122. * @param max_angular_steps The maximum number of steps to be tested.
  123. * @param max_quant_steps The maximum quantization level to be tested.
  124. * @param offsets The angular offsets array.
  125. * @param[out] lowest_weight Per angular step, the lowest weight.
  126. * @param[out] weight_span Per angular step, the span between lowest and highest weight.
  127. * @param[out] error Per angular step, the error.
  128. * @param[out] cut_low_weight_error Per angular step, the low weight cut error.
  129. * @param[out] cut_high_weight_error Per angular step, the high weight cut error.
  130. */
  131. static void compute_lowest_and_highest_weight(
  132. unsigned int weight_count,
  133. const float* dec_weight_ideal_value,
  134. unsigned int max_angular_steps,
  135. unsigned int max_quant_steps,
  136. const float* offsets,
  137. float* lowest_weight,
  138. int* weight_span,
  139. float* error,
  140. float* cut_low_weight_error,
  141. float* cut_high_weight_error
  142. ) {
  143. promise(weight_count > 0);
  144. promise(max_angular_steps > 0);
  145. vfloat rcp_stepsize = vfloat::lane_id() + vfloat(1.0f);
  146. // Arrays are ANGULAR_STEPS long, so always safe to run full vectors
  147. for (unsigned int sp = 0; sp < max_angular_steps; sp += ASTCENC_SIMD_WIDTH)
  148. {
  149. vfloat minidx(128.0f);
  150. vfloat maxidx(-128.0f);
  151. vfloat errval = vfloat::zero();
  152. vfloat cut_low_weight_err = vfloat::zero();
  153. vfloat cut_high_weight_err = vfloat::zero();
  154. vfloat offset = loada(offsets + sp);
  155. for (unsigned int j = 0; j < weight_count; j++)
  156. {
  157. vfloat sval = load1(dec_weight_ideal_value + j) * rcp_stepsize - offset;
  158. vfloat svalrte = round(sval);
  159. vfloat diff = sval - svalrte;
  160. errval += diff * diff;
  161. // Reset tracker on min hit
  162. vmask mask = svalrte < minidx;
  163. minidx = select(minidx, svalrte, mask);
  164. cut_low_weight_err = select(cut_low_weight_err, vfloat::zero(), mask);
  165. // Accumulate on min hit
  166. mask = svalrte == minidx;
  167. vfloat accum = cut_low_weight_err + vfloat(1.0f) - vfloat(2.0f) * diff;
  168. cut_low_weight_err = select(cut_low_weight_err, accum, mask);
  169. // Reset tracker on max hit
  170. mask = svalrte > maxidx;
  171. maxidx = select(maxidx, svalrte, mask);
  172. cut_high_weight_err = select(cut_high_weight_err, vfloat::zero(), mask);
  173. // Accumulate on max hit
  174. mask = svalrte == maxidx;
  175. accum = cut_high_weight_err + vfloat(1.0f) + vfloat(2.0f) * diff;
  176. cut_high_weight_err = select(cut_high_weight_err, accum, mask);
  177. }
  178. // Write out min weight and weight span; clamp span to a usable range
  179. vint span = float_to_int(maxidx - minidx + vfloat(1));
  180. span = min(span, vint(max_quant_steps + 3));
  181. span = max(span, vint(2));
  182. storea(minidx, lowest_weight + sp);
  183. storea(span, weight_span + sp);
  184. // The cut_(lowest/highest)_weight_error indicate the error that results from forcing
  185. // samples that should have had the weight value one step (up/down).
  186. vfloat ssize = 1.0f / rcp_stepsize;
  187. vfloat errscale = ssize * ssize;
  188. storea(errval * errscale, error + sp);
  189. storea(cut_low_weight_err * errscale, cut_low_weight_error + sp);
  190. storea(cut_high_weight_err * errscale, cut_high_weight_error + sp);
  191. rcp_stepsize = rcp_stepsize + vfloat(ASTCENC_SIMD_WIDTH);
  192. }
  193. }
  194. /**
  195. * @brief The main function for the angular algorithm.
  196. *
  197. * @param weight_count The number of (decimated) weights.
  198. * @param dec_weight_ideal_value The ideal decimated unquantized weight values.
  199. * @param max_quant_level The maximum quantization level to be tested.
  200. * @param[out] low_value Per angular step, the lowest weight value.
  201. * @param[out] high_value Per angular step, the highest weight value.
  202. */
  203. static void compute_angular_endpoints_for_quant_levels(
  204. unsigned int weight_count,
  205. const float* dec_weight_ideal_value,
  206. unsigned int max_quant_level,
  207. float low_value[TUNE_MAX_ANGULAR_QUANT + 1],
  208. float high_value[TUNE_MAX_ANGULAR_QUANT + 1]
  209. ) {
  210. unsigned int max_quant_steps = steps_for_quant_level[max_quant_level];
  211. unsigned int max_angular_steps = steps_for_quant_level[max_quant_level];
  212. alignas(ASTCENC_VECALIGN) float angular_offsets[ANGULAR_STEPS];
  213. compute_angular_offsets(weight_count, dec_weight_ideal_value,
  214. max_angular_steps, angular_offsets);
  215. alignas(ASTCENC_VECALIGN) float lowest_weight[ANGULAR_STEPS];
  216. alignas(ASTCENC_VECALIGN) int32_t weight_span[ANGULAR_STEPS];
  217. alignas(ASTCENC_VECALIGN) float error[ANGULAR_STEPS];
  218. alignas(ASTCENC_VECALIGN) float cut_low_weight_error[ANGULAR_STEPS];
  219. alignas(ASTCENC_VECALIGN) float cut_high_weight_error[ANGULAR_STEPS];
  220. compute_lowest_and_highest_weight(weight_count, dec_weight_ideal_value,
  221. max_angular_steps, max_quant_steps,
  222. angular_offsets, lowest_weight, weight_span, error,
  223. cut_low_weight_error, cut_high_weight_error);
  224. // For each quantization level, find the best error terms. Use packed vectors so data-dependent
  225. // branches can become selects. This involves some integer to float casts, but the values are
  226. // small enough so they never round the wrong way.
  227. vfloat4 best_results[36];
  228. // Initialize the array to some safe defaults
  229. promise(max_quant_steps > 0);
  230. for (unsigned int i = 0; i < (max_quant_steps + 4); i++)
  231. {
  232. // Lane<0> = Best error
  233. // Lane<1> = Best scale; -1 indicates no solution found
  234. // Lane<2> = Cut low weight
  235. best_results[i] = vfloat4(ERROR_CALC_DEFAULT, -1.0f, 0.0f, 0.0f);
  236. }
  237. promise(max_angular_steps > 0);
  238. for (unsigned int i = 0; i < max_angular_steps; i++)
  239. {
  240. float i_flt = static_cast<float>(i);
  241. int idx_span = weight_span[i];
  242. float error_cut_low = error[i] + cut_low_weight_error[i];
  243. float error_cut_high = error[i] + cut_high_weight_error[i];
  244. float error_cut_low_high = error[i] + cut_low_weight_error[i] + cut_high_weight_error[i];
  245. // Check best error against record N
  246. vfloat4 best_result = best_results[idx_span];
  247. vfloat4 new_result = vfloat4(error[i], i_flt, 0.0f, 0.0f);
  248. vmask4 mask = vfloat4(best_result.lane<0>()) > vfloat4(error[i]);
  249. best_results[idx_span] = select(best_result, new_result, mask);
  250. // Check best error against record N-1 with either cut low or cut high
  251. best_result = best_results[idx_span - 1];
  252. new_result = vfloat4(error_cut_low, i_flt, 1.0f, 0.0f);
  253. mask = vfloat4(best_result.lane<0>()) > vfloat4(error_cut_low);
  254. best_result = select(best_result, new_result, mask);
  255. new_result = vfloat4(error_cut_high, i_flt, 0.0f, 0.0f);
  256. mask = vfloat4(best_result.lane<0>()) > vfloat4(error_cut_high);
  257. best_results[idx_span - 1] = select(best_result, new_result, mask);
  258. // Check best error against record N-2 with both cut low and high
  259. best_result = best_results[idx_span - 2];
  260. new_result = vfloat4(error_cut_low_high, i_flt, 1.0f, 0.0f);
  261. mask = vfloat4(best_result.lane<0>()) > vfloat4(error_cut_low_high);
  262. best_results[idx_span - 2] = select(best_result, new_result, mask);
  263. }
  264. for (unsigned int i = 0; i <= max_quant_level; i++)
  265. {
  266. unsigned int q = steps_for_quant_level[i];
  267. int bsi = static_cast<int>(best_results[q].lane<1>());
  268. // Did we find anything?
  269. #if defined(ASTCENC_DIAGNOSTICS)
  270. if ((bsi < 0) && print_once)
  271. {
  272. print_once = false;
  273. printf("INFO: Unable to find full encoding within search error limit.\n\n");
  274. }
  275. #endif
  276. bsi = astc::max(0, bsi);
  277. float lwi = lowest_weight[bsi] + best_results[q].lane<2>();
  278. float hwi = lwi + static_cast<float>(q) - 1.0f;
  279. float stepsize = 1.0f / (1.0f + static_cast<float>(bsi));
  280. low_value[i] = (angular_offsets[bsi] + lwi) * stepsize;
  281. high_value[i] = (angular_offsets[bsi] + hwi) * stepsize;
  282. }
  283. }
  284. /* See header for documentation. */
  285. void compute_angular_endpoints_1plane(
  286. bool only_always,
  287. const block_size_descriptor& bsd,
  288. const float* dec_weight_ideal_value,
  289. unsigned int max_weight_quant,
  290. compression_working_buffers& tmpbuf
  291. ) {
  292. float (&low_value)[WEIGHTS_MAX_BLOCK_MODES] = tmpbuf.weight_low_value1;
  293. float (&high_value)[WEIGHTS_MAX_BLOCK_MODES] = tmpbuf.weight_high_value1;
  294. float (&low_values)[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1] = tmpbuf.weight_low_values1;
  295. float (&high_values)[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1] = tmpbuf.weight_high_values1;
  296. unsigned int max_decimation_modes = only_always ? bsd.decimation_mode_count_always
  297. : bsd.decimation_mode_count_selected;
  298. promise(max_decimation_modes > 0);
  299. for (unsigned int i = 0; i < max_decimation_modes; i++)
  300. {
  301. const decimation_mode& dm = bsd.decimation_modes[i];
  302. if (!dm.is_ref_1plane(static_cast<quant_method>(max_weight_quant)))
  303. {
  304. continue;
  305. }
  306. unsigned int weight_count = bsd.get_decimation_info(i).weight_count;
  307. unsigned int max_precision = dm.maxprec_1plane;
  308. if (max_precision > TUNE_MAX_ANGULAR_QUANT)
  309. {
  310. max_precision = TUNE_MAX_ANGULAR_QUANT;
  311. }
  312. if (max_precision > max_weight_quant)
  313. {
  314. max_precision = max_weight_quant;
  315. }
  316. compute_angular_endpoints_for_quant_levels(
  317. weight_count,
  318. dec_weight_ideal_value + i * BLOCK_MAX_WEIGHTS,
  319. max_precision, low_values[i], high_values[i]);
  320. }
  321. unsigned int max_block_modes = only_always ? bsd.block_mode_count_1plane_always
  322. : bsd.block_mode_count_1plane_selected;
  323. promise(max_block_modes > 0);
  324. for (unsigned int i = 0; i < max_block_modes; i++)
  325. {
  326. const block_mode& bm = bsd.block_modes[i];
  327. assert(!bm.is_dual_plane);
  328. unsigned int quant_mode = bm.quant_mode;
  329. unsigned int decim_mode = bm.decimation_mode;
  330. if (quant_mode <= TUNE_MAX_ANGULAR_QUANT)
  331. {
  332. low_value[i] = low_values[decim_mode][quant_mode];
  333. high_value[i] = high_values[decim_mode][quant_mode];
  334. }
  335. else
  336. {
  337. low_value[i] = 0.0f;
  338. high_value[i] = 1.0f;
  339. }
  340. }
  341. }
  342. /* See header for documentation. */
  343. void compute_angular_endpoints_2planes(
  344. const block_size_descriptor& bsd,
  345. const float* dec_weight_ideal_value,
  346. unsigned int max_weight_quant,
  347. compression_working_buffers& tmpbuf
  348. ) {
  349. float (&low_value1)[WEIGHTS_MAX_BLOCK_MODES] = tmpbuf.weight_low_value1;
  350. float (&high_value1)[WEIGHTS_MAX_BLOCK_MODES] = tmpbuf.weight_high_value1;
  351. float (&low_value2)[WEIGHTS_MAX_BLOCK_MODES] = tmpbuf.weight_low_value2;
  352. float (&high_value2)[WEIGHTS_MAX_BLOCK_MODES] = tmpbuf.weight_high_value2;
  353. float (&low_values1)[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1] = tmpbuf.weight_low_values1;
  354. float (&high_values1)[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1] = tmpbuf.weight_high_values1;
  355. float (&low_values2)[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1] = tmpbuf.weight_low_values2;
  356. float (&high_values2)[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1] = tmpbuf.weight_high_values2;
  357. promise(bsd.decimation_mode_count_selected > 0);
  358. for (unsigned int i = 0; i < bsd.decimation_mode_count_selected; i++)
  359. {
  360. const decimation_mode& dm = bsd.decimation_modes[i];
  361. if (!dm.is_ref_2plane(static_cast<quant_method>(max_weight_quant)))
  362. {
  363. continue;
  364. }
  365. unsigned int weight_count = bsd.get_decimation_info(i).weight_count;
  366. unsigned int max_precision = dm.maxprec_2planes;
  367. if (max_precision > TUNE_MAX_ANGULAR_QUANT)
  368. {
  369. max_precision = TUNE_MAX_ANGULAR_QUANT;
  370. }
  371. if (max_precision > max_weight_quant)
  372. {
  373. max_precision = max_weight_quant;
  374. }
  375. compute_angular_endpoints_for_quant_levels(
  376. weight_count,
  377. dec_weight_ideal_value + i * BLOCK_MAX_WEIGHTS,
  378. max_precision, low_values1[i], high_values1[i]);
  379. compute_angular_endpoints_for_quant_levels(
  380. weight_count,
  381. dec_weight_ideal_value + i * BLOCK_MAX_WEIGHTS + WEIGHTS_PLANE2_OFFSET,
  382. max_precision, low_values2[i], high_values2[i]);
  383. }
  384. unsigned int start = bsd.block_mode_count_1plane_selected;
  385. unsigned int end = bsd.block_mode_count_1plane_2plane_selected;
  386. for (unsigned int i = start; i < end; i++)
  387. {
  388. const block_mode& bm = bsd.block_modes[i];
  389. unsigned int quant_mode = bm.quant_mode;
  390. unsigned int decim_mode = bm.decimation_mode;
  391. if (quant_mode <= TUNE_MAX_ANGULAR_QUANT)
  392. {
  393. low_value1[i] = low_values1[decim_mode][quant_mode];
  394. high_value1[i] = high_values1[decim_mode][quant_mode];
  395. low_value2[i] = low_values2[decim_mode][quant_mode];
  396. high_value2[i] = high_values2[decim_mode][quant_mode];
  397. }
  398. else
  399. {
  400. low_value1[i] = 0.0f;
  401. high_value1[i] = 1.0f;
  402. low_value2[i] = 0.0f;
  403. high_value2[i] = 1.0f;
  404. }
  405. }
  406. }
  407. #endif