← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
linear_rows_bf16_interleaved64_checked.c File Reference
#include <math.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "bf16_utils.h"

Go to the source code of this file.

Functions

int ck_linear_rows_bf16_interleaved64_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)
static int overlap (const uint16_t *a, size_t a_count, const uint16_t *b, size_t b_count)
static int region_valid (const uint16_t *ptr, size_t elements)

Function Documentation

◆ ck_linear_rows_bf16_interleaved64_checked()

int ck_linear_rows_bf16_interleaved64_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 72 of file linear_rows_bf16_interleaved64_checked.c.

77{
78 if (!rows || rows > 2 || !input_width || !output_width || input_width % 2 ||
79 output_width % 64 || !pair_chunk || pair_chunk > input_width / 2 ||
80 rows > SIZE_MAX / input_width || output_width > SIZE_MAX / input_width ||
81 rows > SIZE_MAX / output_width)
82 return -2;
83 const size_t in = rows * input_width, w = output_width * input_width, out = rows * output_width;
84 if (input_elements < in || weight_elements < w || output_elements < out || staged_elements < out ||
85 !region_valid(input, in) || !region_valid(weight, w) ||
86 !region_valid(output, out) || !region_valid(staged, out) ||
87 overlap(output, out, input, in) || overlap(output, out, weight, w) ||
88 overlap(output, out, staged, out) || overlap(staged, out, input, in) ||
89 overlap(staged, out, weight, w))
90 return -2;
91#if defined(__x86_64__) && defined(__GNUC__)
92 if (!__builtin_cpu_supports("avx512bf16") || !__builtin_cpu_supports("avx512bw"))
93 return -4;
94#else
95 return -4;
96#endif
97 for (size_t i = 0; i < in; ++i)
98 if (!isfinite(bf16_to_float(input[i]))) return -3;
99#if defined(__x86_64__) && defined(__GNUC__)
100 if (project_interleaved64(input, weight, staged, rows, output_width, input_width, pair_chunk))
101 return -3;
102#endif
103 for (size_t i = 0; i < out; ++i)
104 if (!isfinite(bf16_to_float(staged[i]))) return -3;
105 memcpy(output, staged, out * sizeof(*output));
106 return 0;
107}
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)

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

◆ overlap()

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

Definition at line 15 of file linear_rows_bf16_interleaved64_checked.c.

17{
18 return (uintptr_t)a < (uintptr_t)b + b_count * sizeof(uint16_t) &&
19 (uintptr_t)b < (uintptr_t)a + a_count * sizeof(uint16_t);
20}

Referenced by ck_linear_rows_bf16_interleaved64_checked().

◆ region_valid()

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

Definition at line 8 of file linear_rows_bf16_interleaved64_checked.c.

9{
10 return ptr && (uintptr_t)ptr % _Alignof(uint16_t) == 0 &&
11 elements <= SIZE_MAX / sizeof(uint16_t) &&
12 (uintptr_t)ptr <= UINTPTR_MAX - elements * sizeof(uint16_t);
13}

Referenced by ck_linear_rows_bf16_interleaved64_checked().