| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include <time.h> |
| 8 |
#include "util/u_inlines.h" |
| 9 |
#include "util/u_math.h" |
| 10 |
#include "etnaviv_context.h" |
| 11 |
#include "etnaviv_debug.h" |
| 12 |
#include "etnaviv_ml.h" |
| 13 |
#include "etnaviv_ml_nn.h" |
| 14 |
#include "etnaviv_screen.h" |
| 15 |
|
| 16 |
static void * |
| 17 |
map_resource(struct pipe_resource *resource) |
| 18 |
{ |
| 19 |
return etna_bo_map(etna_buffer_resource(resource)->bo); |
| 20 |
} |
| 21 |
|
| 22 |
#define FIELD(field, bits) uint32_t field : bits; |
| 23 |
|
| 24 |
struct etna_nn_header_v8 { |
| 25 |
FIELD(precode, 1) |
| 26 |
FIELD(bit16, 1) |
| 27 |
FIELD(fp16, 1) |
| 28 |
FIELD(reserved1, 1) |
| 29 |
FIELD(version, 4) |
| 30 |
|
| 31 |
uint8_t run_length_size; |
| 32 |
uint8_t run_length_table[18]; |
| 33 |
uint32_t symbol_map; |
| 34 |
uint16_t avg_bias; |
| 35 |
uint16_t reserved2; |
| 36 |
uint32_t stream_size[0]; |
| 37 |
}; |
| 38 |
|
| 39 |
#define MAX_TILE_WIDTH 64 |
| 40 |
|
| 41 |
static unsigned |
| 42 |
calc_superblocks(struct etna_screen *screen, const struct etna_operation *operation, unsigned tile_x, unsigned tile_y, unsigned interleave_mode) |
| 43 |
{ |
| 44 |
unsigned nn_core_count = etna_ml_get_core_info(screen)->nn_core_count; |
| 45 |
struct etna_core_info *info = etna_gpu_get_core_info(screen->npu); |
| 46 |
unsigned nn_accum_buffer_depth = info->npu.nn_accum_buffer_depth; |
| 47 |
unsigned output_channels = operation->output_channels; |
| 48 |
unsigned kernels_per_core = DIV_ROUND_UP(output_channels, nn_core_count); |
| 49 |
unsigned tiles_per_core; |
| 50 |
|
| 51 |
if (operation->weight_width == 1) |
| 52 |
tiles_per_core = nn_accum_buffer_depth / DIV_ROUND_UP(tile_y, interleave_mode); |
| 53 |
else { |
| 54 |
unsigned tile_size = DIV_ROUND_UP(DIV_ROUND_UP(tile_y * tile_x, operation->stride), 64); |
| 55 |
tiles_per_core = nn_accum_buffer_depth / (tile_size * operation->stride); |
| 56 |
} |
| 57 |
|
| 58 |
tiles_per_core = MIN2(tiles_per_core, (nn_accum_buffer_depth * 6) / 9); |
| 59 |
|
| 60 |
tiles_per_core = MIN2(tiles_per_core, kernels_per_core); |
| 61 |
tiles_per_core = MIN2(tiles_per_core, 127); |
| 62 |
|
| 63 |
kernels_per_core = DIV_ROUND_UP(output_channels, nn_core_count * tiles_per_core); |
| 64 |
unsigned num_kernels = DIV_ROUND_UP(output_channels, kernels_per_core * nn_core_count); |
| 65 |
|
| 66 |
return DIV_ROUND_UP(DIV_ROUND_UP(output_channels, nn_core_count), num_kernels); |
| 67 |
} |
| 68 |
|
| 69 |
static unsigned |
| 70 |
calc_interleave_mode(unsigned tile_width, unsigned weight_height) |
| 71 |
{ |
| 72 |
unsigned mode; |
| 73 |
|
| 74 |
if (weight_height - 1 + tile_width > (MAX_TILE_WIDTH + 8) / 2) |
| 75 |
return 1; |
| 76 |
|
| 77 |
if (tile_width <= MAX_TILE_WIDTH / 2) { |
| 78 |
if (MAX_TILE_WIDTH / 4 < tile_width) |
| 79 |
mode = 2; |
| 80 |
else |
| 81 |
mode = 4; |
| 82 |
} else |
| 83 |
mode = 1; |
| 84 |
|
| 85 |
if (weight_height - 1 + tile_width > (MAX_TILE_WIDTH + 8) / 4) { |
| 86 |
if (mode >= 2) { |
| 87 |
return 2; |
| 88 |
} |
| 89 |
} else { |
| 90 |
if (mode >= 4) { |
| 91 |
return 4; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
if (tile_width <= MAX_TILE_WIDTH / 2) { |
| 96 |
if (MAX_TILE_WIDTH / 4 < tile_width) |
| 97 |
return 2; |
| 98 |
else |
| 99 |
return 4; |
| 100 |
} |
| 101 |
|
| 102 |
return 1; |
| 103 |
} |
| 104 |
|
| 105 |
unsigned |
| 106 |
etna_ml_calculate_tiling_v8(struct etna_screen *screen, const struct etna_operation *operation, unsigned *tile_width_out, unsigned *tile_height_out) |
| 107 |
{ |
| 108 |
unsigned nn_input_buffer_depth = etna_ml_get_core_info(screen)->nn_input_buffer_depth; |
| 109 |
unsigned nn_accum_buffer_depth = etna_ml_get_core_info(screen)->nn_accum_buffer_depth; |
| 110 |
unsigned input_width = operation->input_width; |
| 111 |
unsigned input_height = operation->input_height; |
| 112 |
unsigned input_channels = operation->input_channels; |
| 113 |
unsigned output_width = operation->output_width; |
| 114 |
unsigned output_height = operation->output_height; |
| 115 |
unsigned output_channels = operation->output_channels; |
| 116 |
unsigned tile_width; |
| 117 |
unsigned tile_height; |
| 118 |
unsigned superblocks; |
| 119 |
unsigned interleave_mode; |
| 120 |
|
| 121 |
if (operation->addition) |
| 122 |
etna_ml_calc_addition_sizes(&input_width, &input_height, &input_channels, |
| 123 |
&output_width, &output_height, &output_channels); |
| 124 |
|
| 125 |
if (operation->pooling_first_pixel) { |
| 126 |
output_width *= 2; |
| 127 |
output_height *= 2; |
| 128 |
} |
| 129 |
|
| 130 |
tile_width = MIN2(output_width, 64); |
| 131 |
interleave_mode = calc_interleave_mode(tile_width, operation->weight_height); |
| 132 |
|
| 133 |
tile_height = nn_input_buffer_depth * interleave_mode - operation->weight_height + 1; |
| 134 |
tile_height = MIN2(tile_height, interleave_mode * nn_accum_buffer_depth); |
| 135 |
tile_height = MIN2(tile_height, output_height); |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
tile_height = MIN2(tile_height, 4); |
| 140 |
|
| 141 |
if (operation->stride > 1 && tile_height % 2 > 0) |
| 142 |
tile_height -= 1; |
| 143 |
|
| 144 |
tile_height = MAX2(tile_height, 1); |
| 145 |
|
| 146 |
superblocks = calc_superblocks(screen, operation, tile_width, tile_height, interleave_mode); |
| 147 |
|
| 148 |
if (tile_width_out) |
| 149 |
*tile_width_out = tile_width; |
| 150 |
|
| 151 |
if (tile_height_out) |
| 152 |
*tile_height_out = tile_height; |
| 153 |
|
| 154 |
return superblocks; |
| 155 |
} |
| 156 |
|
| 157 |
struct bitstream { |
| 158 |
unsigned bits_in_buffer; |
| 159 |
uint64_t buffer; |
| 160 |
uint32_t **map; |
| 161 |
bool do_write; |
| 162 |
}; |
| 163 |
|
| 164 |
static uint32_t calculate_bias_correction(struct etna_ml_subgraph *subgraph, const struct etna_operation *operation, uint8_t *weights) |
| 165 |
{ |
| 166 |
unsigned input_channels; |
| 167 |
int32_t input_zero_point = 128 - operation->input_zero_point; |
| 168 |
int32_t correction = 0; |
| 169 |
|
| 170 |
if (operation->depthwise) |
| 171 |
input_channels = 1; |
| 172 |
else if (operation->addition) |
| 173 |
input_channels = 2 * operation->output_channels; |
| 174 |
else |
| 175 |
input_channels = operation->input_channels; |
| 176 |
|
| 177 |
if (operation->weight_signed) { |
| 178 |
|
| 179 |
int8_t weight_zero_point = operation->weight_zero_point - 128; |
| 180 |
|
| 181 |
for (unsigned i = 0; i < operation->weight_width * operation->weight_height * input_channels; i++) { |
| 182 |
correction += (((int8_t *)weights)[i] - weight_zero_point) * input_zero_point; |
| 183 |
} |
| 184 |
} else { |
| 185 |
for (unsigned i = 0; i < operation->weight_width * operation->weight_height * input_channels; i++) { |
| 186 |
correction += (weights[i] - operation->weight_zero_point) * input_zero_point; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
return correction; |
| 191 |
} |
| 192 |
|
| 193 |
static void |
| 194 |
append_bits(uint32_t value, size_t size, struct bitstream *bitstream) |
| 195 |
{ |
| 196 |
assert(value < 1 << size); |
| 197 |
if (!size) |
| 198 |
return; |
| 199 |
bitstream->buffer |= (uint64_t)value << bitstream->bits_in_buffer; |
| 200 |
bitstream->bits_in_buffer += size; |
| 201 |
if (bitstream->bits_in_buffer >= 32) { |
| 202 |
if (bitstream->do_write) |
| 203 |
**bitstream->map = bitstream->buffer & 0xffffffff; |
| 204 |
*bitstream->map += 1; |
| 205 |
bitstream->buffer >>= 32; |
| 206 |
bitstream->bits_in_buffer -= 32; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
static void |
| 211 |
flush_bits(struct bitstream *bitstream) |
| 212 |
{ |
| 213 |
if (bitstream->bits_in_buffer > 0) |
| 214 |
append_bits(0, 32 - bitstream->bits_in_buffer, bitstream); |
| 215 |
} |
| 216 |
|
| 217 |
struct wb_stream { |
| 218 |
struct bitstream bitstream; |
| 219 |
unsigned zero_point; |
| 220 |
unsigned zrl_bits; |
| 221 |
unsigned accum_zeroes; |
| 222 |
}; |
| 223 |
|
| 224 |
static void |
| 225 |
wb_stream_flush_zeroes(struct wb_stream *wb_stream) |
| 226 |
{ |
| 227 |
struct bitstream *bitstream = &wb_stream->bitstream; |
| 228 |
|
| 229 |
if (wb_stream->accum_zeroes == 0) |
| 230 |
return; |
| 231 |
|
| 232 |
append_bits(wb_stream->accum_zeroes - 1, wb_stream->zrl_bits, bitstream); |
| 233 |
wb_stream->accum_zeroes = 0; |
| 234 |
append_bits(wb_stream->zero_point, 8, bitstream); |
| 235 |
} |
| 236 |
|
| 237 |
static void |
| 238 |
wb_stream_write(struct wb_stream *wb_stream, unsigned value) |
| 239 |
{ |
| 240 |
struct bitstream *bitstream = &wb_stream->bitstream; |
| 241 |
unsigned max_zeroes = (1 << wb_stream->zrl_bits) - 1; |
| 242 |
|
| 243 |
if (wb_stream->zrl_bits == 0) { |
| 244 |
append_bits(value, 8, bitstream); |
| 245 |
return; |
| 246 |
} |
| 247 |
|
| 248 |
if (wb_stream->accum_zeroes == max_zeroes) { |
| 249 |
append_bits(max_zeroes, wb_stream->zrl_bits, bitstream); |
| 250 |
wb_stream->accum_zeroes = 0; |
| 251 |
append_bits(value, 8, bitstream); |
| 252 |
return; |
| 253 |
} |
| 254 |
|
| 255 |
if (value == wb_stream->zero_point) { |
| 256 |
wb_stream->accum_zeroes++; |
| 257 |
return; |
| 258 |
} |
| 259 |
|
| 260 |
append_bits(wb_stream->accum_zeroes, wb_stream->zrl_bits, bitstream); |
| 261 |
wb_stream->accum_zeroes = 0; |
| 262 |
append_bits(value, 8, bitstream); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
struct code { |
| 299 |
|
| 300 |
uint8_t part0; |
| 301 |
|
| 302 |
uint8_t part1; |
| 303 |
|
| 304 |
uint8_t part2; |
| 305 |
|
| 306 |
uint8_t part2_len; |
| 307 |
}; |
| 308 |
|
| 309 |
struct encoder { |
| 310 |
|
| 311 |
uint8_t map[9]; |
| 312 |
|
| 313 |
struct code code[6]; |
| 314 |
size_t bytes_read; |
| 315 |
struct bitstream bitstream; |
| 316 |
uint32_t *initial_ptr; |
| 317 |
uint32_t *dest; |
| 318 |
uint8_t accum_zeroes; |
| 319 |
uint8_t avg_bias; |
| 320 |
bool zrl; |
| 321 |
}; |
| 322 |
|
| 323 |
|
| 324 |
static void histogram_accumulate(size_t histogram[9], uint8_t *bytes, size_t len, bool zrl) |
| 325 |
{ |
| 326 |
for (size_t i = 0; i < len; i++) { |
| 327 |
uint8_t num_bits = 0; |
| 328 |
if (bytes[i]) { |
| 329 |
bool sign = bytes[i] >> 7; |
| 330 |
uint8_t value = bytes[i]; |
| 331 |
if (sign) { |
| 332 |
value -= zrl; |
| 333 |
value ^= 0xff; |
| 334 |
} |
| 335 |
num_bits = util_logbase2(value) + 1; |
| 336 |
} |
| 337 |
assert(num_bits <= 8); |
| 338 |
histogram[num_bits]++; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
static struct code huffman_code(uint8_t sym, uint8_t value, uint8_t num_bits) |
| 347 |
{ |
| 348 |
switch (sym) { |
| 349 |
case 0: |
| 350 |
return (struct code){ 0 | ((value & 1) << 2), 0, value >> 1, num_bits - 1 }; |
| 351 |
case 1: |
| 352 |
return (struct code){ 1 | ((value & 1) << 2), 0, value >> 1, num_bits - 1 }; |
| 353 |
case 2: |
| 354 |
return (struct code){ 7, 0, value, num_bits}; |
| 355 |
case 3: |
| 356 |
return (struct code){ 3, 0, value, num_bits}; |
| 357 |
case 4: |
| 358 |
return (struct code){ 6, 0, value, num_bits}; |
| 359 |
case 5: |
| 360 |
return (struct code){ 2, 1 | ((value & 1) << 1), value >> 1, num_bits - 1 }; |
| 361 |
case 6: |
| 362 |
return (struct code){ 2, 2, value, num_bits}; |
| 363 |
case 7: |
| 364 |
return (struct code){ 2, 0, value, num_bits}; |
| 365 |
default: |
| 366 |
return (struct code){}; |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
static void emit_pair(struct encoder *encoder) |
| 371 |
{ |
| 372 |
struct bitstream *bitstream = &encoder->bitstream; |
| 373 |
struct code *code = &encoder->code[(encoder->bytes_read - 2) % 6]; |
| 374 |
|
| 375 |
append_bits(code[0].part0, 3, bitstream); |
| 376 |
append_bits(code[1].part0, 3, bitstream); |
| 377 |
if (encoder->bytes_read > 2) { |
| 378 |
code = &encoder->code[(encoder->bytes_read - 4) % 6]; |
| 379 |
append_bits(code[0].part1, code[0].part0 == 2 ? 2 : 0, bitstream); |
| 380 |
append_bits(code[1].part1, code[1].part0 == 2 ? 2 : 0, bitstream); |
| 381 |
} |
| 382 |
if (encoder->bytes_read > 4) { |
| 383 |
code = &encoder->code[(encoder->bytes_read - 6) % 6]; |
| 384 |
append_bits(code[0].part2, code[0].part2_len, bitstream); |
| 385 |
append_bits(code[1].part2, code[1].part2_len, bitstream); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
static void encode_byte(struct encoder *encoder, uint8_t byte) |
| 391 |
{ |
| 392 |
bool zrl = encoder->zrl; |
| 393 |
bool sign = byte >> 7; |
| 394 |
uint8_t value = byte; |
| 395 |
|
| 396 |
if (sign) { |
| 397 |
value -= zrl; |
| 398 |
value ^= 0xff; |
| 399 |
} |
| 400 |
|
| 401 |
uint8_t msb = util_logbase2(value); |
| 402 |
uint8_t num_bits = value ? (msb + 1) : 0; |
| 403 |
value &= ~(1 << msb); |
| 404 |
uint8_t sym = encoder->map[num_bits]; |
| 405 |
if (zrl && byte == 0) { |
| 406 |
if (encoder->accum_zeroes <= 1) { |
| 407 |
|
| 408 |
sym = encoder->map[7]; |
| 409 |
num_bits = 8; |
| 410 |
} else { |
| 411 |
|
| 412 |
num_bits = 1; |
| 413 |
} |
| 414 |
} |
| 415 |
if (!zrl && num_bits == 0) { |
| 416 |
num_bits = 1; |
| 417 |
} |
| 418 |
if (sym == 255 || (zrl && byte == 128)) { |
| 419 |
|
| 420 |
|
| 421 |
sym = encoder->map[7]; |
| 422 |
value = byte; |
| 423 |
num_bits = 8; |
| 424 |
} else if (zrl && num_bits == 7) { |
| 425 |
value = byte; |
| 426 |
num_bits = 8; |
| 427 |
} else { |
| 428 |
value = (value << 1) | sign; |
| 429 |
} |
| 430 |
unsigned int i = encoder->bytes_read % 6; |
| 431 |
encoder->code[i] = huffman_code(sym, value, num_bits); |
| 432 |
encoder->bytes_read++; |
| 433 |
if ((encoder->bytes_read & 1) == 0) |
| 434 |
emit_pair(encoder); |
| 435 |
} |
| 436 |
|
| 437 |
static void |
| 438 |
encode_value(struct etna_ml_subgraph *subgraph, const struct etna_operation *operation, struct encoder *encoder, uint8_t value) |
| 439 |
{ |
| 440 |
unsigned customer_id = subgraph->screen->info->customer_id; |
| 441 |
uint8_t zero_point = operation->weight_zero_point; |
| 442 |
|
| 443 |
value -= encoder->avg_bias; |
| 444 |
|
| 445 |
if (customer_id == 0x99) { |
| 446 |
if (encoder->zrl) { |
| 447 |
if (encoder->avg_bias > 0) { |
| 448 |
if (value == zero_point) { |
| 449 |
encoder->accum_zeroes++; |
| 450 |
return; |
| 451 |
} else if (encoder->accum_zeroes) { |
| 452 |
encode_byte(encoder, zero_point); |
| 453 |
encoder->accum_zeroes = 0; |
| 454 |
} |
| 455 |
} else { |
| 456 |
if (value == 0x0) { |
| 457 |
encoder->accum_zeroes++; |
| 458 |
return; |
| 459 |
} else if (encoder->accum_zeroes) { |
| 460 |
encode_byte(encoder, 0x80); |
| 461 |
encoder->accum_zeroes = 0; |
| 462 |
} |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
encode_byte(encoder, value); |
| 467 |
} else { |
| 468 |
if (encoder->zrl) { |
| 469 |
if (value == zero_point) { |
| 470 |
encoder->accum_zeroes++; |
| 471 |
return; |
| 472 |
} else if (encoder->accum_zeroes) { |
| 473 |
encode_byte(encoder, 0x00); |
| 474 |
encoder->accum_zeroes = 0; |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
encode_byte(encoder, value - zero_point); |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
static void encoder_init(struct encoder *encoder, uint8_t *map, uint32_t *initial_ptr) |
| 483 |
{ |
| 484 |
memset(encoder, 0, sizeof(*encoder)); |
| 485 |
encoder->initial_ptr = initial_ptr; |
| 486 |
encoder->dest = initial_ptr; |
| 487 |
encoder->bitstream.map = &encoder->dest; |
| 488 |
encoder->bitstream.do_write = initial_ptr != NULL; |
| 489 |
|
| 490 |
for (int i = 0; i < 9; i++) |
| 491 |
encoder->map[i] = 255; |
| 492 |
|
| 493 |
for (int i = 0; i < 8; i++) { |
| 494 |
assert(map[i] < sizeof(encoder->map)); |
| 495 |
encoder->map[map[i]] = i; |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
static void encode_uint32(struct encoder *encoder, uint32_t value) |
| 500 |
{ |
| 501 |
encode_byte(encoder, (value & 0xff) - encoder->avg_bias); |
| 502 |
encode_byte(encoder, ((value >> 8) & 0xff) - encoder->avg_bias); |
| 503 |
encode_byte(encoder, ((value >> 16) & 0xff) - encoder->avg_bias); |
| 504 |
encode_byte(encoder, ((value >> 24) & 0xff) - encoder->avg_bias); |
| 505 |
} |
| 506 |
|
| 507 |
static void encode_uint16(struct encoder *encoder, uint32_t value) |
| 508 |
{ |
| 509 |
encode_byte(encoder, (value & 0xff) - encoder->avg_bias); |
| 510 |
encode_byte(encoder, ((value >> 8) & 0xff) - encoder->avg_bias); |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
static size_t encoder_flush(struct etna_ml_subgraph *subgraph, const struct etna_operation *operation, struct encoder *encoder) |
| 518 |
{ |
| 519 |
struct bitstream *bitstream = &encoder->bitstream; |
| 520 |
size_t total_bits; |
| 521 |
uint8_t flush_val = (encoder->bytes_read & 1) + 4; |
| 522 |
|
| 523 |
struct code code; |
| 524 |
if (encoder->bytes_read & 1) |
| 525 |
encode_byte(encoder, 0x0); |
| 526 |
|
| 527 |
code.part0 = (flush_val & 1) << 2; |
| 528 |
code.part1 = 0x0; |
| 529 |
code.part2 = 0x0; |
| 530 |
code.part2_len = 0x0; |
| 531 |
encoder->code[encoder->bytes_read++ % 6] = code; |
| 532 |
encoder->code[encoder->bytes_read++ % 6] = code; |
| 533 |
emit_pair(encoder); |
| 534 |
encoder->code[encoder->bytes_read++ % 6] = code; |
| 535 |
encoder->code[encoder->bytes_read++ % 6] = code; |
| 536 |
emit_pair(encoder); |
| 537 |
|
| 538 |
total_bits = (*bitstream->map - encoder->initial_ptr) * 32 + |
| 539 |
bitstream->bits_in_buffer; |
| 540 |
|
| 541 |
int padding_bits = 0; |
| 542 |
if (total_bits % (64 * 8) > 0) |
| 543 |
padding_bits = (64 * 8) - total_bits % (64 * 8); |
| 544 |
|
| 545 |
while (padding_bits > 0) { |
| 546 |
unsigned bits = padding_bits >= 32 ? 32 : padding_bits; |
| 547 |
append_bits(0, bits, bitstream); |
| 548 |
padding_bits -= bits; |
| 549 |
} |
| 550 |
|
| 551 |
return total_bits; |
| 552 |
} |
| 553 |
|
| 554 |
static void map_swap(uint8_t *map, int a, int b) |
| 555 |
{ |
| 556 |
uint8_t tmp = map[a]; |
| 557 |
|
| 558 |
map[a] = map[b]; |
| 559 |
map[b] = tmp; |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
static void sort_map(uint8_t *map, size_t *histogram) |
| 568 |
{ |
| 569 |
const uint8_t network[19][2] = { |
| 570 |
{0, 2}, {1, 3}, {4, 6}, {5, 7}, |
| 571 |
{0, 4}, {1, 5}, {2, 6}, {3, 7}, |
| 572 |
{0, 1}, {2, 3}, {4, 5}, {6, 7}, |
| 573 |
{2, 4}, {3, 5}, |
| 574 |
{1, 4}, {3, 6}, |
| 575 |
{1, 2}, {3 ,4}, {5, 6}, |
| 576 |
}; |
| 577 |
|
| 578 |
for (int i = 0; i < 19; i++) { |
| 579 |
int a = network[i][0]; |
| 580 |
int b = network[i][1]; |
| 581 |
|
| 582 |
if (histogram[map[a]] < histogram[map[b]]) |
| 583 |
map_swap(map, a, b); |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
static void encoder_reset(struct etna_ml_subgraph *subgraph, const struct etna_operation *operation, struct encoder *encoder) |
| 588 |
{ |
| 589 |
encoder->initial_ptr = *encoder->bitstream.map; |
| 590 |
encoder->dest = encoder->initial_ptr; |
| 591 |
encoder->bitstream.map = &encoder->dest; |
| 592 |
|
| 593 |
encoder->bitstream.buffer = 0; |
| 594 |
encoder->bitstream.bits_in_buffer = 0; |
| 595 |
encoder->bytes_read = 0; |
| 596 |
memset(encoder->code, 0, sizeof(encoder->code)); |
| 597 |
} |
| 598 |
|
| 599 |
static void encode_superblock(struct etna_ml_subgraph *subgraph, const struct etna_operation *operation, struct encoder *encoder, unsigned kernels_in_superblock, unsigned first_channel) |
| 600 |
{ |
| 601 |
struct etna_screen *screen = subgraph->screen; |
| 602 |
unsigned nn_core_count = etna_ml_get_core_info(screen)->nn_core_count; |
| 603 |
unsigned input_channels = operation->input_channels; |
| 604 |
unsigned output_channels = operation->output_channels; |
| 605 |
unsigned kernel_size; |
| 606 |
uint8_t *weights = operation->weight_tensor; |
| 607 |
unsigned block_size; |
| 608 |
unsigned blocks; |
| 609 |
|
| 610 |
if (operation->depthwise) |
| 611 |
input_channels = 1; |
| 612 |
else if (operation->addition) |
| 613 |
input_channels = 2 * output_channels; |
| 614 |
|
| 615 |
kernel_size = input_channels * operation->weight_height * operation->weight_width; |
| 616 |
|
| 617 |
uint8_t (*weights_map)[kernel_size] = (void *)weights; |
| 618 |
|
| 619 |
if (operation->depthwise) |
| 620 |
block_size = MAX2(operation->weight_height * operation->weight_width, 9); |
| 621 |
else |
| 622 |
block_size = 9; |
| 623 |
|
| 624 |
blocks = DIV_ROUND_UP(kernel_size, block_size); |
| 625 |
|
| 626 |
for (unsigned block = 0; block < blocks; block++) { |
| 627 |
for (unsigned kernel = 0; kernel < kernels_in_superblock; kernel++) { |
| 628 |
unsigned oc; |
| 629 |
|
| 630 |
if (operation->depthwise) { |
| 631 |
oc = first_channel + kernel * nn_core_count; |
| 632 |
|
| 633 |
if (output_channels > 1 && oc >= (output_channels - output_channels % nn_core_count)) |
| 634 |
oc -= nn_core_count - output_channels % nn_core_count; |
| 635 |
} else |
| 636 |
oc = first_channel + kernel; |
| 637 |
|
| 638 |
for (unsigned kernel_idx = 0; kernel_idx < block_size; kernel_idx++) { |
| 639 |
uint8_t weight; |
| 640 |
|
| 641 |
if (kernel_idx + block * block_size >= kernel_size) |
| 642 |
weight = operation->weight_zero_point; |
| 643 |
else if (operation->weight_signed) |
| 644 |
weight = ((int8_t *)(weights_map[oc]))[kernel_idx + block * block_size] + 128; |
| 645 |
else |
| 646 |
weight = weights_map[oc][kernel_idx + block * block_size]; |
| 647 |
|
| 648 |
encode_value(subgraph, operation, encoder, weight); |
| 649 |
} |
| 650 |
|
| 651 |
if (operation->depthwise && block_size % 9) |
| 652 |
for (unsigned i = 0; i < 9 - block_size % 9; i++) |
| 653 |
encode_value(subgraph, operation, encoder, operation->weight_zero_point); |
| 654 |
} |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
static uint32_t pack_symbol_map(uint8_t map[8]) |
| 659 |
{ |
| 660 |
uint32_t ret = 0; |
| 661 |
|
| 662 |
for (int i = 0; i < 8; i++) |
| 663 |
ret |= map[i] << (4 * i); |
| 664 |
|
| 665 |
return ret; |
| 666 |
} |
| 667 |
|
| 668 |
static struct etna_bo * |
| 669 |
create_bo(struct etna_ml_subgraph *subgraph, const struct etna_operation *operation) |
| 670 |
{ |
| 671 |
struct etna_screen *screen = subgraph->screen; |
| 672 |
unsigned nn_core_count = etna_ml_get_core_info(screen)->nn_core_count; |
| 673 |
unsigned input_channels = operation->input_channels; |
| 674 |
unsigned output_channels = operation->output_channels; |
| 675 |
unsigned cores_used = MIN2(output_channels, nn_core_count); |
| 676 |
size_t max_size; |
| 677 |
|
| 678 |
if (operation->depthwise) |
| 679 |
input_channels = 1; |
| 680 |
else if (operation->addition) |
| 681 |
input_channels = 2 * output_channels; |
| 682 |
|
| 683 |
unsigned header_size = 64; |
| 684 |
unsigned body_size = align(DIV_ROUND_UP(output_channels, cores_used) * (input_channels * operation->weight_height * operation->weight_width + 4 + 4), 64) * 2; |
| 685 |
unsigned tail_size = 64; |
| 686 |
max_size = header_size + cores_used * body_size + tail_size; |
| 687 |
|
| 688 |
return etna_ml_create_bo(screen, max_size); |
| 689 |
} |
| 690 |
|
| 691 |
static void |
| 692 |
calculate_symbol_map(struct etna_ml_subgraph *subgraph, const struct etna_operation *operation, uint8_t *symbol_map) |
| 693 |
{ |
| 694 |
unsigned input_channels = operation->input_channels; |
| 695 |
unsigned output_channels = operation->output_channels; |
| 696 |
uint8_t *input = operation->weight_tensor; |
| 697 |
size_t histogram[9] = {}; |
| 698 |
|
| 699 |
if (operation->depthwise) |
| 700 |
input_channels = 1; |
| 701 |
else if (operation->addition) |
| 702 |
input_channels = 2 * output_channels; |
| 703 |
|
| 704 |
uint8_t (*weights_map)[input_channels][operation->weight_height][operation->weight_width] = (void *)input; |
| 705 |
unsigned kernel_size = operation->weight_width * operation->weight_height * input_channels; |
| 706 |
for (unsigned oc = 0; oc < output_channels; oc++) |
| 707 |
histogram_accumulate(histogram, (uint8_t *)weights_map[oc], kernel_size, false); |
| 708 |
|
| 709 |
for (int i = 0; i < 8; i++) |
| 710 |
symbol_map[i] = i; |
| 711 |
sort_map(symbol_map, histogram); |
| 712 |
} |
| 713 |
|
| 714 |
static void |
| 715 |
fill_weights(struct etna_ml_subgraph *subgraph, const struct etna_operation *operation, struct encoder *encoder, struct etna_nn_header_v8 *header) |
| 716 |
{ |
| 717 |
struct etna_screen *screen = subgraph->screen; |
| 718 |
unsigned output_channels = operation->output_channels; |
| 719 |
unsigned nn_core_count = etna_ml_get_core_info(screen)->nn_core_count; |
| 720 |
unsigned cores_used = MIN2(output_channels, nn_core_count); |
| 721 |
unsigned superblocks = etna_ml_calculate_tiling_v8(screen, operation, NULL, NULL); |
| 722 |
unsigned full_superblock = DIV_ROUND_UP(output_channels, nn_core_count * superblocks); |
| 723 |
|
| 724 |
unsigned channel_per_superblock[superblocks]; |
| 725 |
for (unsigned superblock = 0; superblock < superblocks; superblock++) |
| 726 |
channel_per_superblock[superblock] = superblock * full_superblock * cores_used; |
| 727 |
|
| 728 |
for (unsigned core = 0; core < cores_used; core++) { |
| 729 |
unsigned kernels_per_core = output_channels / cores_used; |
| 730 |
if (core < output_channels % cores_used) |
| 731 |
kernels_per_core++; |
| 732 |
|
| 733 |
encoder_reset(subgraph, operation, encoder); |
| 734 |
encode_uint16(encoder, kernels_per_core); |
| 735 |
|
| 736 |
for (unsigned superblock = 0; superblock < superblocks; superblock++) { |
| 737 |
|
| 738 |
unsigned kernels_in_superblock = full_superblock; |
| 739 |
if (superblock == superblocks - 1) { |
| 740 |
unsigned remaining_channels = output_channels - cores_used * (superblocks - 1) * full_superblock; |
| 741 |
kernels_in_superblock = remaining_channels / cores_used; |
| 742 |
if (core < remaining_channels % cores_used) |
| 743 |
kernels_in_superblock += 1; |
| 744 |
} |
| 745 |
|
| 746 |
unsigned first_channel; |
| 747 |
if (operation->depthwise) |
| 748 |
first_channel = cores_used - core - 1 + cores_used * full_superblock * superblock; |
| 749 |
else |
| 750 |
first_channel = channel_per_superblock[superblock]; |
| 751 |
|
| 752 |
encode_superblock(subgraph, operation, encoder, kernels_in_superblock, first_channel); |
| 753 |
|
| 754 |
channel_per_superblock[superblock] += kernels_in_superblock; |
| 755 |
} |
| 756 |
|
| 757 |
unsigned actual_bits = encoder_flush(subgraph, operation, encoder); |
| 758 |
header->stream_size[core] = actual_bits; |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
static uint32_t * |
| 763 |
fill_biases(struct etna_ml_subgraph *subgraph, const struct etna_operation *operation, uint32_t *map) |
| 764 |
{ |
| 765 |
uint8_t *input = operation->weight_tensor; |
| 766 |
uint32_t *biases = (uint32_t *)operation->bias_tensor; |
| 767 |
unsigned input_channels = operation->input_channels; |
| 768 |
unsigned output_channels = operation->output_channels; |
| 769 |
|
| 770 |
if (operation->depthwise) |
| 771 |
input_channels = 1; |
| 772 |
else if (operation->addition) |
| 773 |
input_channels = 2 * output_channels; |
| 774 |
|
| 775 |
uint8_t (*weights_map)[input_channels][operation->weight_height][operation->weight_width] = (void *)input; |
| 776 |
for (unsigned oc = 0; oc < output_channels; oc++) { |
| 777 |
uint32_t corr = calculate_bias_correction(subgraph, operation, (uint8_t *)weights_map[oc]); |
| 778 |
|
| 779 |
*map = biases[oc] + corr; |
| 780 |
map++; |
| 781 |
} |
| 782 |
|
| 783 |
return map; |
| 784 |
} |
| 785 |
|
| 786 |
struct etna_bo * |
| 787 |
etna_ml_create_coeffs_v8(struct etna_ml_subgraph *subgraph, const struct etna_operation *operation, unsigned *cache_size) |
| 788 |
{ |
| 789 |
struct etna_bo *bo = create_bo(subgraph, operation); |
| 790 |
uint32_t *map = etna_bo_map(bo); |
| 791 |
struct etna_nn_header_v8 *header = (struct etna_nn_header_v8 *)map; |
| 792 |
struct encoder encoder; |
| 793 |
uint8_t symbol_map[8]; |
| 794 |
|
| 795 |
etna_bo_cpu_prep(bo, DRM_ETNA_PREP_WRITE); |
| 796 |
|
| 797 |
calculate_symbol_map(subgraph, operation, symbol_map); |
| 798 |
header->symbol_map = pack_symbol_map(symbol_map); |
| 799 |
header->version = 1; |
| 800 |
|
| 801 |
map += align(sizeof(*header), 64) / 4; |
| 802 |
|
| 803 |
encoder_init(&encoder, symbol_map, map); |
| 804 |
|
| 805 |
fill_weights(subgraph, operation, &encoder, header); |
| 806 |
map = fill_biases(subgraph, operation, encoder.dest); |
| 807 |
|
| 808 |
|
| 809 |
*cache_size = (uint8_t*)map - (uint8_t*)etna_bo_map(bo); |
| 810 |
|
| 811 |
etna_bo_cpu_fini(bo); |
| 812 |
|
| 813 |
return bo; |
| 814 |
} |