← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
linear_rows_bf16_checked.c File Reference
#include <dlfcn.h>
#include <limits.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stddef.h>
#include <stdint.h>
#include <stdatomic.h>
#include "bf16_utils.h"
#include "checked_parallel.h"

Go to the source code of this file.

Macros

#define _GNU_SOURCE

Typedefs

typedef void(* ck_bf16_gemm_fn) (int, int, int, int, int, int, float, const uint16_t *, int, const uint16_t *, int, float, float *, int)
typedef void(* ck_dgemm_fn) (const char *, const char *, const int *, const int *, const int *, const double *, const double *, const int *, const double *, const int *, const double *, double *, const int *)

Functions

static void bind_bf16_gemm (void)
static void bind_dgemm (void)
int ck_indexed_linear_rows_bf16_checked (const uint16_t *input, size_t input_elements, const int32_t *indices, size_t index_elements, const uint16_t *weight, size_t weight_elements, const uint16_t *bias, size_t bias_elements, uint16_t *output, size_t output_elements, uint16_t *staged, size_t staged_elements, size_t rows, size_t input_width, size_t output_width, size_t banks)
int ck_linear_rows_bf16_blas_checked (const uint16_t *input, size_t input_elements, const uint16_t *weight, size_t weight_elements, uint16_t *output, size_t output_elements, uint16_t *staged, size_t staged_elements, double *work, size_t work_elements, size_t rows, size_t input_width, size_t output_width)
int ck_linear_rows_bf16_cblas_checked (const uint16_t *input, size_t input_elements, const uint16_t *weight, size_t weight_elements, uint16_t *output, size_t output_elements, float *work, size_t work_elements, size_t rows, size_t input_width, size_t output_width)
int ck_linear_rows_bf16_checked (const uint16_t *input, size_t input_elements, const uint16_t *weight, size_t weight_elements, uint16_t *output, size_t output_elements, uint16_t *staged, size_t staged_elements, size_t rows, size_t input_width, size_t output_width)
int ck_linear_rows_bf16_packed_checked (const uint16_t *input, size_t input_elements, const uint16_t *weight, size_t weight_elements, uint16_t *output, size_t output_elements, uint16_t *staged, size_t staged_elements, size_t rows, size_t input_width, size_t output_width, size_t pair_chunk)
int ck_linear_rows_bias_bf16_checked (const uint16_t *input, size_t input_elements, const uint16_t *weight, size_t weight_elements, const uint16_t *bias, size_t bias_elements, uint16_t *output, size_t output_elements, uint16_t *staged, size_t staged_elements, size_t rows, size_t input_width, size_t output_width)
static int linear_rows_bf16_checked (const uint16_t *input, size_t input_elements, const uint16_t *weight, size_t weight_elements, const uint16_t *bias, size_t bias_elements, uint16_t *output, size_t output_elements, uint16_t *staged, size_t staged_elements, size_t rows, size_t input_width, size_t output_width)
static int overlap (const uint16_t *a, size_t a_count, const uint16_t *b, size_t b_count)
static void project_ordered_tiles (size_t begin, size_t end, void *opaque)
static int region_valid (const uint16_t *ptr, size_t elements)
static int stage_ordered_row (struct ordered_projection *p, size_t row, size_t start, size_t lanes, const double *sums)

Variables

static ck_bf16_gemm_fn bf16_gemm
static pthread_once_t bf16_gemm_once = PTHREAD_ONCE_INIT
static ck_dgemm_fn dgemm
static pthread_once_t dgemm_once = PTHREAD_ONCE_INIT

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 2 of file linear_rows_bf16_checked.c.

Typedef Documentation

◆ ck_bf16_gemm_fn

typedef void(* ck_bf16_gemm_fn) (int, int, int, int, int, int, float, const uint16_t *, int, const uint16_t *, int, float, float *, int)

Definition at line 579 of file linear_rows_bf16_checked.c.

◆ ck_dgemm_fn

typedef void(* ck_dgemm_fn) (const char *, const char *, const int *, const int *, const int *, const double *, const double *, const int *, const double *, const int *, const double *, double *, const int *)

Definition at line 505 of file linear_rows_bf16_checked.c.

Function Documentation

◆ bind_bf16_gemm()

void bind_bf16_gemm ( void )
static

Definition at line 585 of file linear_rows_bf16_checked.c.

586{
587 const char *path = getenv("CK_BLAS_LIBRARY");
588 void *library = path && *path ? dlopen(path, RTLD_NOW | RTLD_LOCAL) : RTLD_DEFAULT;
589 if (path && *path && !library)
590 return;
591 void *symbol = dlsym(library, "cblas_gemm_bf16bf16f32");
592 _Static_assert(sizeof(symbol) == sizeof(bf16_gemm), "CBLAS function pointer size");
593 memcpy(&bf16_gemm, &symbol, sizeof(bf16_gemm));
594}
#define RTLD_DEFAULT
static ck_bf16_gemm_fn bf16_gemm

References bf16_gemm, and RTLD_DEFAULT.

Referenced by ck_linear_rows_bf16_cblas_checked().

◆ bind_dgemm()

void bind_dgemm ( void )
static

Definition at line 512 of file linear_rows_bf16_checked.c.

513{
514 const char *path = getenv("CK_BLAS_LIBRARY");
515 void *library = path && *path ? dlopen(path, RTLD_NOW | RTLD_LOCAL) : RTLD_DEFAULT;
516 if (path && *path && !library)
517 return;
518 void *symbol = dlsym(library, "dgemm_");
519 _Static_assert(sizeof(symbol) == sizeof(dgemm), "dgemm_ function pointer size");
520 memcpy(&dgemm, &symbol, sizeof(dgemm));
521}
static ck_dgemm_fn dgemm

References dgemm, and RTLD_DEFAULT.

Referenced by ck_linear_rows_bf16_blas_checked().

◆ ck_indexed_linear_rows_bf16_checked()

int ck_indexed_linear_rows_bf16_checked ( const uint16_t * input,
size_t input_elements,
const int32_t * indices,
size_t index_elements,
const uint16_t * weight,
size_t weight_elements,
const uint16_t * bias,
size_t bias_elements,
uint16_t * output,
size_t output_elements,
uint16_t * staged,
size_t staged_elements,
size_t rows,
size_t input_width,
size_t output_width,
size_t banks )

Definition at line 738 of file linear_rows_bf16_checked.c.

744{
745 if (!rows || !input_width || !output_width || !banks ||
746 rows > SIZE_MAX / input_width || rows > SIZE_MAX / output_width ||
747 banks > SIZE_MAX / input_width || banks * input_width > SIZE_MAX / output_width)
748 return -2;
749
750 const size_t in = rows * input_width, out = rows * output_width;
751 const size_t b = banks * output_width, w = b * input_width;
752 if (input_elements < in || index_elements < rows || weight_elements < w ||
753 bias_elements < b || output_elements < out || staged_elements < out)
754 return -2;
755
756 if (!region_valid(input, in) || !region_valid(weight, w) || !region_valid(bias, b) ||
757 !region_valid(output, out) || !region_valid(staged, out) ||
758 !indices || (uintptr_t)indices % _Alignof(int32_t) ||
759 rows > (UINTPTR_MAX - (uintptr_t)indices) / sizeof(*indices))
760 return -2;
761
762 const uint16_t *ids = (const uint16_t *)(const void *)indices;
763 const size_t id_words = rows * (sizeof(*indices) / sizeof(*ids));
764 if (overlap(output, out, staged, out) || overlap(output, out, input, in) ||
765 overlap(output, out, weight, w) || overlap(output, out, bias, b) ||
766 overlap(output, out, ids, id_words) || overlap(staged, out, input, in) ||
767 overlap(staged, out, weight, w) || overlap(staged, out, bias, b) ||
768 overlap(staged, out, ids, id_words))
769 return -2;
770
771 for (size_t r = 0; r < rows; ++r) {
772 if (indices[r] < 0 || (size_t)indices[r] >= banks)
773 return -2;
774 }
775 for (size_t r = 0; r < rows; ++r) {
776 const size_t bank = (size_t)indices[r];
777 const uint16_t *matrix = weight + bank * input_width * output_width;
778 const uint16_t *row = input + r * input_width;
779 for (size_t o = 0; o < output_width; ++o) {
780 double sum = 0.0;
781 for (size_t i = 0; i < input_width; ++i) {
782 volatile double product =
783 (double)bf16_to_float(row[i]) * bf16_to_float(matrix[i * output_width + o]);
784 sum += product;
785 }
786 const float dot = bf16_to_float(float_to_bf16((float)sum));
787 const uint16_t result = float_to_bf16(dot + bf16_to_float(bias[bank * output_width + o]));
788 if (!isfinite(dot) || !isfinite(bf16_to_float(result)))
789 return -3;
790 staged[r * output_width + o] = result;
791 }
792 }
793 memcpy(output, staged, out * sizeof(*output));
794 return 0;
795}
static double dot(const float *a, const float *b, size_t n)
static int product(size_t a, size_t b, size_t *out)
static uint16_t float_to_bf16(float f)
Definition bf16_utils.h:90
static float bf16_to_float(uint16_t v)
Definition bf16_utils.h:38
static int region_valid(const uint16_t *ptr, size_t elements)
static int overlap(const uint16_t *a, size_t a_count, const uint16_t *b, size_t b_count)
const int32_t * ids
Definition tokenizer.h:447

References bf16_to_float(), dot(), float_to_bf16(), ids, overlap(), product(), and region_valid().

◆ ck_linear_rows_bf16_blas_checked()

int ck_linear_rows_bf16_blas_checked ( const uint16_t * input,
size_t input_elements,
const uint16_t * weight,
size_t weight_elements,
uint16_t * output,
size_t output_elements,
uint16_t * staged,
size_t staged_elements,
double * work,
size_t work_elements,
size_t rows,
size_t input_width,
size_t output_width )

Definition at line 523 of file linear_rows_bf16_checked.c.

527{
528 if (!rows || !input_width || !output_width || rows > INT_MAX ||
529 input_width > INT_MAX || output_width > INT_MAX ||
530 rows > SIZE_MAX / input_width || output_width > SIZE_MAX / input_width ||
531 rows > SIZE_MAX / output_width)
532 return -2;
533 const size_t a = rows * input_width, b = output_width * input_width, c = rows * output_width;
534 if (a > SIZE_MAX - b || a + b > SIZE_MAX - c)
535 return -2;
536 const size_t work_count = a + b + c;
537 if (a > input_elements || b > weight_elements || c > output_elements ||
538 c > staged_elements || work_count > work_elements || !work ||
539 (uintptr_t)work % _Alignof(double) || work_count > SIZE_MAX / sizeof(double))
540 return -2;
541 const size_t work_bits = work_count * (sizeof(double) / sizeof(uint16_t));
542 const uint16_t *work_region = (const uint16_t*)work;
543 if (!region_valid(input, a) || !region_valid(weight, b) ||
544 !region_valid(output, c) || !region_valid(staged, c) ||
545 !region_valid(work_region, work_bits) ||
546 overlap(output, c, input, a) || overlap(output, c, weight, b) ||
547 overlap(staged, c, input, a) || overlap(staged, c, weight, b) ||
548 overlap(output, c, staged, c) ||
549 overlap(work_region, work_bits, input, a) || overlap(work_region, work_bits, weight, b) ||
550 overlap(work_region, work_bits, staged, c) || overlap(work_region, work_bits, output, c))
551 return -1;
552 pthread_once(&dgemm_once, bind_dgemm);
553 if (!dgemm)
554 return -4;
555 double *matrix_a = work, *matrix_b = work + a, *matrix_c = work + a + b;
556 for (size_t i = 0; i < a; ++i) {
557 matrix_a[i] = bf16_to_float(input[i]);
558 if (!isfinite(matrix_a[i]))
559 return -3;
560 }
561 for (size_t i = 0; i < b; ++i) {
562 matrix_b[i] = bf16_to_float(weight[i]);
563 if (!isfinite(matrix_b[i]))
564 return -3;
565 }
566 const char trans = 'T', no_trans = 'N';
567 const int m = (int)rows, n = (int)output_width, k = (int)input_width;
568 const double one = 1.0, zero = 0.0;
569 dgemm(&trans, &no_trans, &n, &m, &k, &one, matrix_b, &k, matrix_a, &k, &zero, matrix_c, &n);
570 for (size_t i = 0; i < c; ++i) {
571 staged[i] = float_to_bf16((float)matrix_c[i]);
572 if (!isfinite(bf16_to_float(staged[i])))
573 return -3;
574 }
575 memcpy(output, staged, c * sizeof(*output));
576 return 0;
577}
static pthread_once_t dgemm_once
static void bind_dgemm(void)

References bf16_to_float(), bind_dgemm(), dgemm, dgemm_once, float_to_bf16(), overlap(), and region_valid().

Referenced by project().

◆ ck_linear_rows_bf16_cblas_checked()

int ck_linear_rows_bf16_cblas_checked ( const uint16_t * input,
size_t input_elements,
const uint16_t * weight,
size_t weight_elements,
uint16_t * output,
size_t output_elements,
float * work,
size_t work_elements,
size_t rows,
size_t input_width,
size_t output_width )

Definition at line 596 of file linear_rows_bf16_checked.c.

600{
601 if (!rows || !input_width || !output_width || rows > INT_MAX ||
602 input_width > INT_MAX || output_width > INT_MAX ||
603 rows > SIZE_MAX / input_width || output_width > SIZE_MAX / input_width ||
604 rows > SIZE_MAX / output_width)
605 return -2;
606 const size_t a = rows * input_width, b = output_width * input_width, c = rows * output_width;
607 if (a > input_elements || b > weight_elements || c > output_elements || c > work_elements ||
608 !work || (uintptr_t)work % _Alignof(float) || c > SIZE_MAX / sizeof(float))
609 return -2;
610 const uint16_t *work_region = (const uint16_t *)work;
611 const size_t work_bits = c * (sizeof(float) / sizeof(uint16_t));
612 if (!region_valid(input, a) || !region_valid(weight, b) || !region_valid(output, c) ||
613 !region_valid(work_region, work_bits) || overlap(output, c, input, a) ||
614 overlap(output, c, weight, b) || overlap(work_region, work_bits, input, a) ||
615 overlap(work_region, work_bits, weight, b) || overlap(work_region, work_bits, output, c))
616 return -1;
617 for (size_t i = 0; i < a; ++i)
618 if (!isfinite(bf16_to_float(input[i]))) return -3;
619 for (size_t i = 0; i < b; ++i)
620 if (!isfinite(bf16_to_float(weight[i]))) return -3;
621 pthread_once(&bf16_gemm_once, bind_bf16_gemm);
622 if (!bf16_gemm)
623 return -4;
624
625 enum { ROW_MAJOR = 101, NO_TRANSPOSE = 111, TRANSPOSE = 112 };
626 const int m = (int)rows, n = (int)output_width, k = (int)input_width;
627 bf16_gemm(ROW_MAJOR, NO_TRANSPOSE, TRANSPOSE, m, n, k,
628 1.0f, input, k, weight, k, 0.0f, work, n);
629 for (size_t i = 0; i < c; ++i)
630 if (!isfinite(bf16_to_float(float_to_bf16(work[i])))) return -3;
631 for (size_t i = 0; i < c; ++i)
632 output[i] = float_to_bf16(work[i]);
633 return 0;
634}
static pthread_once_t bf16_gemm_once
static void bind_bf16_gemm(void)

References bf16_gemm, bf16_gemm_once, bf16_to_float(), bind_bf16_gemm(), float_to_bf16(), overlap(), and region_valid().

Referenced by project().

◆ ck_linear_rows_bf16_checked()

int ck_linear_rows_bf16_checked ( const uint16_t * input,
size_t input_elements,
const uint16_t * weight,
size_t weight_elements,
uint16_t * output,
size_t output_elements,
uint16_t * staged,
size_t staged_elements,
size_t rows,
size_t input_width,
size_t output_width )

Definition at line 479 of file linear_rows_bf16_checked.c.

485{
486 return linear_rows_bf16_checked(input, input_elements, weight, weight_elements,
487 NULL, 0, output, output_elements, staged, staged_elements,
488 rows, input_width, output_width);
489}
static int linear_rows_bf16_checked(const uint16_t *input, size_t input_elements, const uint16_t *weight, size_t weight_elements, const uint16_t *bias, size_t bias_elements, uint16_t *output, size_t output_elements, uint16_t *staged, size_t staged_elements, size_t rows, size_t input_width, size_t output_width)

References linear_rows_bf16_checked().

Referenced by project().

◆ ck_linear_rows_bf16_packed_checked()

int ck_linear_rows_bf16_packed_checked ( const uint16_t * input,
size_t input_elements,
const uint16_t * weight,
size_t weight_elements,
uint16_t * output,
size_t output_elements,
uint16_t * staged,
size_t staged_elements,
size_t rows,
size_t input_width,
size_t output_width,
size_t pair_chunk )

Definition at line 693 of file linear_rows_bf16_checked.c.

698{
699 if (!rows || !input_width || !output_width || input_width % 2 ||
700 output_width % 16 || !pair_chunk || pair_chunk > input_width / 2 ||
701 rows > SIZE_MAX / input_width || output_width > SIZE_MAX / input_width ||
702 rows > SIZE_MAX / output_width)
703 return -2;
704 const size_t in = rows * input_width, w = output_width * input_width, out = rows * output_width;
705 if (input_elements < in || weight_elements < w || output_elements < out || staged_elements < out ||
706 !region_valid(input, in) || !region_valid(weight, w) ||
707 !region_valid(output, out) || !region_valid(staged, out) ||
708 overlap(output, out, input, in) || overlap(output, out, weight, w) ||
709 overlap(output, out, staged, out) || overlap(staged, out, input, in) ||
710 overlap(staged, out, weight, w))
711 return -2;
712#if defined(__x86_64__) && defined(__GNUC__)
713 if (!__builtin_cpu_supports("avx512bf16"))
714 return -4;
715#else
716 return -4;
717#endif
718 for (size_t i = 0; i < in; ++i)
719 if (!isfinite(bf16_to_float(input[i]))) return -3;
720#if defined(__x86_64__) && defined(__GNUC__)
721 struct packed_projection work = {input, weight, staged, rows, output_width, input_width, pair_chunk, 0};
722 const size_t tiles = output_width / 16;
723 if (w >= (1u << 19))
724 ck_checked_parallel_for(tiles, 4, project_packed_bf16, &work);
725 else
726 project_packed_bf16(0, tiles, &work);
727 if (atomic_load_explicit(&work.failed, memory_order_relaxed)) return -3;
728#endif
729 for (size_t i = 0; i < out; ++i)
730 if (!isfinite(bf16_to_float(staged[i]))) return -3;
731 memcpy(output, staged, out * sizeof(*output));
732 return 0;
733}
static void ck_checked_parallel_for(size_t count, int grain, ck_checked_range_fn fn, void *args)

References bf16_to_float(), ck_checked_parallel_for(), overlap(), and region_valid().

◆ ck_linear_rows_bias_bf16_checked()

int ck_linear_rows_bias_bf16_checked ( const uint16_t * input,
size_t input_elements,
const uint16_t * weight,
size_t weight_elements,
const uint16_t * bias,
size_t bias_elements,
uint16_t * output,
size_t output_elements,
uint16_t * staged,
size_t staged_elements,
size_t rows,
size_t input_width,
size_t output_width )

Definition at line 491 of file linear_rows_bf16_checked.c.

498{
499 if (!bias) return -1;
500 return linear_rows_bf16_checked(input, input_elements, weight, weight_elements,
501 bias, bias_elements, output, output_elements, staged, staged_elements,
502 rows, input_width, output_width);
503}

References linear_rows_bf16_checked().

◆ linear_rows_bf16_checked()

int linear_rows_bf16_checked ( const uint16_t * input,
size_t input_elements,
const uint16_t * weight,
size_t weight_elements,
const uint16_t * bias,
size_t bias_elements,
uint16_t * output,
size_t output_elements,
uint16_t * staged,
size_t staged_elements,
size_t rows,
size_t input_width,
size_t output_width )
static

Definition at line 414 of file linear_rows_bf16_checked.c.

421{
422 if (!rows || !input_width || !output_width ||
423 rows > SIZE_MAX / input_width || output_width > SIZE_MAX / input_width ||
424 rows > SIZE_MAX / output_width) return -2;
425 const size_t in_count = rows * input_width;
426 const size_t w_count = output_width * input_width;
427 const size_t out_count = rows * output_width;
428 if (input_elements < in_count || weight_elements < w_count ||
429 output_elements < out_count || staged_elements < out_count ||
430 (bias && bias_elements < output_width)) return -2;
431 if (!region_valid(input, input_elements) || !region_valid(weight, weight_elements) ||
432 !region_valid(output, output_elements) || !region_valid(staged, staged_elements) ||
433 overlap(output, output_elements, input, input_elements) ||
434 overlap(output, output_elements, weight, weight_elements) ||
435 overlap(output, output_elements, staged, staged_elements) ||
436 overlap(staged, staged_elements, input, input_elements) ||
437 overlap(staged, staged_elements, weight, weight_elements)) return -1;
438 if (bias) {
439 if (!region_valid(bias, bias_elements) ||
440 overlap(output, output_elements, bias, bias_elements) ||
441 overlap(staged, staged_elements, bias, bias_elements)) return -1;
442 for (size_t o = 0; o < output_width; ++o)
443 if (!isfinite(bf16_to_float(bias[o]))) return -1;
444 }
445 for (size_t i = 0; i < in_count; ++i)
446 if (!isfinite(bf16_to_float(input[i]))) return -1;
447 int avx2 = 0;
448#ifdef CK_ORDERED_AVX2
449 avx2 = input_width >= 8 && __builtin_cpu_supports("avx2") && __builtin_cpu_supports("fma");
450#endif
451 struct ordered_projection work = {input, weight, bias, NULL, staged, rows, input_width, output_width, avx2, 0};
452 size_t tiles = (output_width - 1) / 4 + 1;
454 int grain = 4;
455#ifdef CK_ORDERED_AVX2
456 if (avx2 && rows == 1 && output_width >= 8) {
457 tiles = (output_width - 1) / 8 + 1;
458 project = project_avx2_single_pairs;
459 grain = 2;
460 } else if (avx2 && rows >= 16 && input_width >= 128 && output_width >= 256) {
461 tiles = (output_width-1)/ORDERED_PANEL_N+1;
462 project = project_avx2_panels;
463 grain = 1;
464 }
465 if (project == project_avx2_single_pairs && w_count >= (1u << 19) &&
466 input_width <= ORDERED_WIDE_ROW_MAX)
467 project_avx2_widened_row(&work, tiles, grain);
468 else
469#endif
470 if (w_count >= (1u << 19))
471 ck_checked_parallel_for(tiles, grain, project, &work);
472 else
473 project(0, tiles, &work);
474 if (atomic_load_explicit(&work.failed, memory_order_relaxed)) return -1;
475 memcpy(output, staged, out_count * sizeof(*output));
476 return 0;
477}
void(* ck_checked_range_fn)(size_t begin, size_t end, void *args)
static void project_ordered_tiles(size_t begin, size_t end, void *opaque)
static int project(const uint16_t *input, const uint16_t *weight, uint16_t *out, size_t rows, size_t in_width, size_t out_width, projection_workspace work)

References bf16_to_float(), ck_checked_parallel_for(), overlap(), project(), project_ordered_tiles(), and region_valid().

Referenced by ck_linear_rows_bf16_checked(), and ck_linear_rows_bias_bf16_checked().

◆ overlap()

int overlap ( const uint16_t * a,
size_t a_count,
const uint16_t * b,
size_t b_count )
static

Definition at line 30 of file linear_rows_bf16_checked.c.

32{
33 return (uintptr_t)a < (uintptr_t)b + b_count * sizeof(uint16_t) &&
34 (uintptr_t)b < (uintptr_t)a + a_count * sizeof(uint16_t);
35}

Referenced by ck_indexed_linear_rows_bf16_checked(), ck_linear_rows_bf16_blas_checked(), ck_linear_rows_bf16_cblas_checked(), ck_linear_rows_bf16_packed_checked(), and linear_rows_bf16_checked().

◆ project_ordered_tiles()

void project_ordered_tiles ( size_t begin,
size_t end,
void * opaque )
static

Definition at line 205 of file linear_rows_bf16_checked.c.

206{
207 struct ordered_projection *p = opaque;
208 const size_t k_count = p->input_width, n = p->output_width;
209 for (size_t tile = begin; tile < end; ++tile) {
210 const size_t start = tile * 4;
211 const size_t lanes = n - start >= 4 ? 4 : n - start;
212 const uint16_t *w = p->weight + start * k_count;
213#ifdef CK_ORDERED_AVX2
214 if (p->avx2 && lanes == 4) {
215 if (!project_four_avx2(p,start)) {
216 atomic_store_explicit(&p->failed, 1, memory_order_relaxed);
217 return;
218 }
219 continue;
220 }
221#endif
222 for (size_t i = 0; i < lanes * k_count; ++i)
223 if ((w[i] & 0x7f80u) == 0x7f80u) {
224 atomic_store_explicit(&p->failed, 1, memory_order_relaxed);
225 return;
226 }
227 size_t row = 0;
228 if (lanes == 4) {
229 for (; row + 1 < p->rows; row += 2) {
230 const uint16_t *first = p->input + row * k_count;
231 const uint16_t *second = first + k_count;
232 double first_sums[4] = {0}, second_sums[4] = {0};
233 // Reuse decoded weights across two rows while preserving ascending K order.
234 for (size_t k = 0; k < k_count; ++k) {
235 const double a = bf16_to_float(first[k]);
236 const double b = bf16_to_float(second[k]);
237 for (size_t j = 0; j < 4; ++j) {
238 const double weight = bf16_to_float(w[j * k_count + k]);
239 first_sums[j] += a * weight;
240 second_sums[j] += b * weight;
241 }
242 }
243 if (stage_ordered_row(p, row, start, 4, first_sums) ||
244 stage_ordered_row(p, row + 1, start, 4, second_sums)) return;
245 }
246 }
247 for (; row < p->rows; ++row) {
248 const uint16_t *x = p->input + row * k_count;
249 double sums[4] = {0};
250 if (lanes == 4) {
251 // Products are exact in FP64. Each lane still accumulates in ascending K order.
252 for (size_t k = 0; k < k_count; ++k) {
253 const double value = bf16_to_float(x[k]);
254 for (size_t j = 0; j < 4; ++j)
255 sums[j] += value * (double)bf16_to_float(w[j * k_count + k]);
256 }
257 } else {
258 for (size_t j = 0; j < lanes; ++j)
259 for (size_t k = 0; k < k_count; ++k)
260 sums[j] += (double)bf16_to_float(x[k]) * bf16_to_float(w[j * k_count + k]);
261 }
262 if (stage_ordered_row(p, row, start, lanes, sums)) return;
263 }
264 }
265}
static int stage_ordered_row(struct ordered_projection *p, size_t row, size_t start, size_t lanes, const double *sums)
uint32_t end
Definition utf8.c:215
uint32_t start
Definition utf8.c:214

References bf16_to_float(), end, stage_ordered_row(), and start.

Referenced by linear_rows_bf16_checked().

◆ region_valid()

int region_valid ( const uint16_t * ptr,
size_t elements )
static

Definition at line 23 of file linear_rows_bf16_checked.c.

24{
25 return ptr && (uintptr_t)ptr % _Alignof(uint16_t) == 0 &&
26 elements <= SIZE_MAX / sizeof(uint16_t) &&
27 (uintptr_t)ptr <= UINTPTR_MAX - elements * sizeof(uint16_t);
28}

Referenced by ck_indexed_linear_rows_bf16_checked(), ck_linear_rows_bf16_blas_checked(), ck_linear_rows_bf16_cblas_checked(), ck_linear_rows_bf16_packed_checked(), and linear_rows_bf16_checked().

◆ stage_ordered_row()

int stage_ordered_row ( struct ordered_projection * p,
size_t row,
size_t start,
size_t lanes,
const double * sums )
static

Definition at line 46 of file linear_rows_bf16_checked.c.

48{
49 for (size_t j = 0; j < lanes; ++j) {
50 double sum = sums[j];
51 if (p->bias) sum += bf16_to_float(p->bias[start + j]);
52 const uint16_t value = float_to_bf16((float)sum);
53 if ((value & 0x7f80u) == 0x7f80u) {
54 atomic_store_explicit(&p->failed, 1, memory_order_relaxed);
55 return -1;
56 }
57 p->stage[row * p->output_width + start + j] = value;
58 }
59 return 0;
60}

References bf16_to_float(), float_to_bf16(), and start.

Referenced by project_ordered_tiles().

Variable Documentation

◆ bf16_gemm

ck_bf16_gemm_fn bf16_gemm
static

◆ bf16_gemm_once

pthread_once_t bf16_gemm_once = PTHREAD_ONCE_INIT
static

Definition at line 582 of file linear_rows_bf16_checked.c.

Referenced by ck_linear_rows_bf16_cblas_checked().

◆ dgemm

ck_dgemm_fn dgemm
static

Definition at line 510 of file linear_rows_bf16_checked.c.

Referenced by bind_dgemm(), and ck_linear_rows_bf16_blas_checked().

◆ dgemm_once

pthread_once_t dgemm_once = PTHREAD_ONCE_INIT
static

Definition at line 509 of file linear_rows_bf16_checked.c.

Referenced by ck_linear_rows_bf16_blas_checked().