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

Go to the source code of this file.

Functions

static int all_finite (const uint16_t *ptr, size_t elements)
int ck_channel_l2_scale_bf16_checked (const uint16_t *input, const uint16_t *gamma, uint16_t *output, uint16_t *staged, size_t channels, size_t positions, size_t elements)
int ck_video_block_mean_bf16_checked (const uint16_t *input, uint16_t *output, uint16_t *staged, size_t input_elements, size_t output_elements, size_t channels, size_t out_channels, size_t frames, size_t height, size_t width, size_t out_frames, size_t out_height, size_t out_width, size_t temporal_factor, size_t spatial_factor)
int ck_video_causal_conv3d_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 channels, size_t out_channels, size_t frames, size_t height, size_t width, size_t kernel_t, size_t kernel_h, size_t kernel_w)
int ck_video_mean_patch_rows_bf16_checked (const uint16_t *moments, const uint16_t *mean, const uint16_t *inv_std, uint16_t *output, uint16_t *staged, size_t input_elements, size_t output_elements, size_t channels, size_t moment_channels, size_t frames, size_t height, size_t width, size_t selected_frames, size_t patch, size_t rows, size_t row_width)
int ck_video_space_to_depth_bf16_checked (const uint16_t *input, size_t input_elements, uint16_t *output, size_t output_elements, size_t channels, size_t frames, size_t height, size_t width, size_t patch)
int ck_video_spatial_attention_bf16_checked (const uint16_t *qkv, uint16_t *output, uint16_t *staged, float *scores, size_t input_elements, size_t output_elements, size_t positions, size_t channels, size_t frames, size_t height, size_t width, size_t qkv_channels)
int ck_video_spatial_conv2d_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 channels, size_t out_channels, size_t frames, size_t height, size_t width, size_t kernel_h, size_t kernel_w, size_t out_height, size_t out_width, size_t stride, size_t pad_bottom, size_t pad_right)
int ck_video_temporal_stride_bf16_checked (const uint16_t *first, const uint16_t *filtered, uint16_t *output, size_t input_elements, size_t output_elements, size_t channels, size_t frames, size_t height, size_t width, size_t out_frames, size_t stride)
static int extent_product (const size_t dims[], size_t n, size_t *count)
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)
static void video_conv_planes (size_t begin, size_t end, void *opaque)
static int video_conv_run (struct video_conv_job *job, uint16_t *output, size_t output_count, size_t weight_count, size_t out_channels)

Function Documentation

◆ all_finite()

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

◆ ck_channel_l2_scale_bf16_checked()

int ck_channel_l2_scale_bf16_checked ( const uint16_t * input,
const uint16_t * gamma,
uint16_t * output,
uint16_t * staged,
size_t channels,
size_t positions,
size_t elements )

Definition at line 199 of file video_conv_bf16_checked.c.

202{
203 if (!channels || channels > 65536 || !positions || positions > SIZE_MAX / channels)
204 return -2;
205 const size_t count = channels * positions;
206 if (elements < count || !region_valid(input, count) || !region_valid(gamma, channels) ||
207 !region_valid(output, count) || !region_valid(staged, count) ||
208 overlap(output, count, input, count) || overlap(output, count, gamma, channels) ||
209 overlap(staged, count, input, count) || overlap(staged, count, gamma, channels) ||
210 overlap(staged, count, output, count))
211 return -2;
212 if (!all_finite(input, count) || !all_finite(gamma, channels))
213 return -3;
214
215 const float scale = sqrtf((float)channels);
216 for (size_t p = 0; p < positions; ++p) {
217 double sum = 0.0;
218 for (size_t c = 0; c < channels; ++c) {
219 const double x = bf16_to_float(input[c * positions + p]);
220 sum += x * x;
221 }
222 const float norm = fmaxf(sqrtf((float)sum), 1e-12f);
223 if (!isfinite(norm))
224 return -3;
225 for (size_t c = 0; c < channels; ++c) {
226 const size_t i = c * positions + p;
227 const uint16_t unit = float_to_bf16(bf16_to_float(input[i]) / norm);
228 const uint16_t scaled = float_to_bf16(bf16_to_float(unit) * scale);
229 staged[i] = float_to_bf16(bf16_to_float(scaled) * bf16_to_float(gamma[c]));
230 if (!isfinite(bf16_to_float(staged[i])))
231 return -3;
232 }
233 }
234 memcpy(output, staged, count * sizeof(*output));
235 return 0;
236}
static uint16_t float_to_bf16(float f)
Definition bf16_utils.h:90
const uint8_t size_t count
Definition true_bpe.h:211
static int all_finite(const uint16_t *ptr, size_t elements)
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 all_finite(), bf16_to_float(), count, float_to_bf16(), overlap(), and region_valid().

◆ ck_video_block_mean_bf16_checked()

int ck_video_block_mean_bf16_checked ( const uint16_t * input,
uint16_t * output,
uint16_t * staged,
size_t input_elements,
size_t output_elements,
size_t channels,
size_t out_channels,
size_t frames,
size_t height,
size_t width,
size_t out_frames,
size_t out_height,
size_t out_width,
size_t temporal_factor,
size_t spatial_factor )

Definition at line 281 of file video_conv_bf16_checked.c.

286{
287 size_t source_count, output_count, factor;
288 const size_t source_dims[] = {channels, frames, height, width};
289 const size_t output_dims[] = {out_channels, out_frames, out_height, out_width};
290 const size_t factors[] = {temporal_factor, spatial_factor, spatial_factor, channels};
291 if (!extent_product(source_dims, 4, &source_count) ||
292 !extent_product(output_dims, 4, &output_count) ||
293 !extent_product(factors, 4, &factor) || factor % out_channels ||
294 height % spatial_factor || width % spatial_factor ||
295 out_height != height / spatial_factor || out_width != width / spatial_factor ||
296 out_frames != 1 + (frames - 1) / temporal_factor ||
297 input_elements < source_count || output_elements < output_count ||
298 !region_valid(input, source_count) || !region_valid(output, output_count) ||
299 !region_valid(staged, output_count) || overlap(input, source_count, output, output_count) ||
300 overlap(input, source_count, staged, output_count) || overlap(output, output_count, staged, output_count))
301 return -2;
302 if (!all_finite(input, source_count))
303 return -3;
304
305 const size_t area = spatial_factor * spatial_factor;
306 const size_t block = temporal_factor * area;
307 const size_t group = factor / out_channels;
308 const size_t pad = out_frames * temporal_factor - frames;
309 size_t out = 0;
310 for (size_t oc = 0; oc < out_channels; ++oc)
311 for (size_t t = 0; t < out_frames; ++t)
312 for (size_t h = 0; h < out_height; ++h)
313 for (size_t w = 0; w < out_width; ++w) {
314 double sum = 0.0;
315 for (size_t k = oc * group; k < (oc + 1) * group; ++k) {
316 const size_t padded_time = t * temporal_factor + (k % block) / area;
317 if (padded_time < pad)
318 continue;
319 const size_t in_h = h * spatial_factor + (k % area) / spatial_factor;
320 const size_t in_w = w * spatial_factor + k % spatial_factor;
321 const size_t in_t = padded_time - pad;
322 const size_t index = (((k / block) * frames + in_t) * height + in_h) * width + in_w;
323 sum += bf16_to_float(input[index]);
324 }
325 staged[out++] = float_to_bf16((float)(sum / (double)group));
326 }
327 memcpy(output, staged, output_count * sizeof(*output));
328 return 0;
329}
int32_t int32_t int32_t int32_t pad
Definition tokenizer.h:234
static int extent_product(const size_t dims[], size_t n, size_t *count)

References all_finite(), bf16_to_float(), extent_product(), float_to_bf16(), overlap(), pad, and region_valid().

◆ ck_video_causal_conv3d_bf16_checked()

int ck_video_causal_conv3d_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 channels,
size_t out_channels,
size_t frames,
size_t height,
size_t width,
size_t kernel_t,
size_t kernel_h,
size_t kernel_w )

Definition at line 163 of file video_conv_bf16_checked.c.

168{
169 size_t source_count, weight_count, output_count;
170 const size_t source_dims[] = {channels, frames, height, width};
171 const size_t weight_dims[] = {out_channels, channels, kernel_t, kernel_h, kernel_w};
172 const size_t output_dims[] = {out_channels, frames, height, width};
173 if (!extent_product(source_dims, 4, &source_count) || !extent_product(weight_dims, 5, &weight_count) ||
174 !extent_product(output_dims, 4, &output_count) || !(kernel_h & 1) || !(kernel_w & 1) ||
175 input_elements < source_count || weight_elements < weight_count || bias_elements < out_channels ||
176 output_elements < output_count || staged_elements < output_count)
177 return -2;
178
179 const uint16_t *p[] = {input, weight, bias, output, staged};
180 const size_t count[] = {source_count, weight_count, out_channels, output_count, output_count};
181 for (size_t i = 0; i < 5; ++i) {
182 if (!region_valid(p[i], count[i]))
183 return -2;
184 if (i >= 3)
185 for (size_t j = 0; j < i; ++j)
186 if (overlap(p[i], count[i], p[j], count[j]))
187 return -2;
188 }
189 for (size_t i = 0; i < 3; ++i)
190 if (!all_finite(p[i], count[i]))
191 return -3;
192
193 struct video_conv_job job = {input, weight, bias, staged, channels, frames, height, width,
194 kernel_t, kernel_h, kernel_w, height, width, 1, kernel_h / 2, kernel_w / 2, 0};
195 return video_conv_run(&job, output, output_count, weight_count, out_channels);
196}
static int video_conv_run(struct video_conv_job *job, uint16_t *output, size_t output_count, size_t weight_count, size_t out_channels)

References all_finite(), count, extent_product(), overlap(), region_valid(), and video_conv_run().

◆ ck_video_mean_patch_rows_bf16_checked()

int ck_video_mean_patch_rows_bf16_checked ( const uint16_t * moments,
const uint16_t * mean,
const uint16_t * inv_std,
uint16_t * output,
uint16_t * staged,
size_t input_elements,
size_t output_elements,
size_t channels,
size_t moment_channels,
size_t frames,
size_t height,
size_t width,
size_t selected_frames,
size_t patch,
size_t rows,
size_t row_width )

Definition at line 430 of file video_conv_bf16_checked.c.

435{
436 size_t source_count, output_count, expected_rows, expected_width;
437 if (!patch || patch > 65536 || !channels || channels > 32768 ||
438 moment_channels != 2 * channels || !selected_frames || selected_frames > frames)
439 return -2;
440 const size_t input_dims[] = {moment_channels, frames, height, width};
441 const size_t output_dims[] = {rows, row_width};
442 if (!extent_product(input_dims, 4, &source_count) || !extent_product(output_dims, 2, &output_count) ||
443 input_elements < source_count || output_elements < output_count)
444 return -2;
445 const size_t hp = 1 + (height - 1) / patch, wp = 1 + (width - 1) / patch;
446 const size_t row_dims[] = {selected_frames, hp, wp};
447 const size_t column_dims[] = {patch, patch, channels};
448 if (!extent_product(row_dims, 3, &expected_rows) || !extent_product(column_dims, 3, &expected_width) ||
449 rows != expected_rows || row_width != expected_width)
450 return -2;
451
452 const uint16_t *p[] = {moments, mean, inv_std, output, staged};
453 const size_t sizes[] = {source_count, channels, channels, output_count, output_count};
454 for (size_t i = 0; i < 5; ++i) {
455 if (!region_valid(p[i], sizes[i]))
456 return -2;
457 if (i >= 3)
458 for (size_t j = 0; j < i; ++j)
459 if (overlap(p[i], sizes[i], p[j], sizes[j]))
460 return -2;
461 }
462 for (size_t c = 0; c < channels; ++c)
463 if (!isfinite(bf16_to_float(mean[c])) || !isfinite(bf16_to_float(inv_std[c])) ||
464 bf16_to_float(inv_std[c]) <= 0.0f)
465 return -3;
466
467 size_t offset = 0;
468 for (size_t t = 0; t < selected_frames; ++t)
469 for (size_t ph = 0; ph < hp; ++ph)
470 for (size_t pw = 0; pw < wp; ++pw)
471 for (size_t dh = 0; dh < patch; ++dh)
472 for (size_t dw = 0; dw < patch; ++dw) {
473 const size_t h = ph * patch + dh, w = pw * patch + dw;
474 if (h >= height || w >= width) {
475 memset(staged + offset, 0, channels * sizeof(*staged));
476 offset += channels;
477 continue;
478 }
479 for (size_t c = 0; c < channels; ++c) {
480 const float x = bf16_to_float(moments[((c * frames + t) * height + h) * width + w]);
481 const float centered = bf16_to_float(float_to_bf16(x - bf16_to_float(mean[c])));
482 const uint16_t value = float_to_bf16(centered * bf16_to_float(inv_std[c]));
483 if (!isfinite(x) || !isfinite(bf16_to_float(value)))
484 return -3;
485 staged[offset++] = value;
486 }
487 }
488 memcpy(output, staged, output_count * sizeof(*output));
489 return 0;
490}
static float centered(float value, int zero)

References bf16_to_float(), centered(), extent_product(), float_to_bf16(), overlap(), and region_valid().

◆ ck_video_space_to_depth_bf16_checked()

int ck_video_space_to_depth_bf16_checked ( const uint16_t * input,
size_t input_elements,
uint16_t * output,
size_t output_elements,
size_t channels,
size_t frames,
size_t height,
size_t width,
size_t patch )

Definition at line 42 of file video_conv_bf16_checked.c.

45{
46 size_t elements;
47 const size_t dims[] = {channels, frames, height, width};
48 if (!extent_product(dims, 4, &elements) || !patch || height % patch || width % patch ||
49 input_elements < elements || output_elements < elements ||
50 !region_valid(input, elements) || !region_valid(output, elements) ||
51 overlap(input, elements, output, elements))
52 return -2;
53 if (!all_finite(input, elements))
54 return -3;
55
56 size_t index = 0;
57 for (size_t c = 0; c < channels; ++c)
58 for (size_t x = 0; x < patch; ++x)
59 for (size_t y = 0; y < patch; ++y)
60 for (size_t t = 0; t < frames; ++t)
61 for (size_t h = y; h < height; h += patch)
62 for (size_t w = x; w < width; w += patch)
63 output[index++] = input[((c * frames + t) * height + h) * width + w];
64 return 0;
65}

References all_finite(), extent_product(), overlap(), and region_valid().

◆ ck_video_spatial_attention_bf16_checked()

int ck_video_spatial_attention_bf16_checked ( const uint16_t * qkv,
uint16_t * output,
uint16_t * staged,
float * scores,
size_t input_elements,
size_t output_elements,
size_t positions,
size_t channels,
size_t frames,
size_t height,
size_t width,
size_t qkv_channels )

Definition at line 367 of file video_conv_bf16_checked.c.

371{
372 size_t count, area;
373 const size_t dims[] = {channels, frames, height, width};
374 const size_t spatial_dims[] = {height, width};
375 if (!extent_product(dims, 4, &count) || !extent_product(spatial_dims, 2, &area) ||
376 count > SIZE_MAX / 3 || area > SIZE_MAX / 2 || positions != area || qkv_channels != 3 * channels ||
377 input_elements < count * 3 || output_elements < count ||
378 (uintptr_t)scores % _Alignof(float) ||
379 !region_valid((const uint16_t *)scores, area * 2))
380 return -2;
381 const uint16_t *buffers[] = {qkv, output, staged, (const uint16_t *)scores};
382 const size_t sizes[] = {count * 3, count, count, area * 2};
383 for (size_t i = 0; i < 4; ++i) {
384 if (!region_valid(buffers[i], sizes[i]))
385 return -2;
386 for (size_t j = 0; j < i; ++j)
387 if (overlap(buffers[i], sizes[i], buffers[j], sizes[j]))
388 return -2;
389 }
390 if (!all_finite(qkv, count * 3))
391 return -3;
392
393 const float scale = 1.0f / sqrtf((float)channels);
394 for (size_t t = 0; t < frames; ++t) {
395 for (size_t q = 0; q < area; ++q) {
396 float max_score = -INFINITY;
397 for (size_t k = 0; k < area; ++k) {
398 double dot = 0.0;
399 for (size_t c = 0; c < channels; ++c) {
400 const size_t base = (c * frames + t) * area;
401 dot += (double)bf16_to_float(qkv[base + q]) * bf16_to_float(qkv[count + base + k]);
402 }
403 scores[k] = (float)dot * scale;
404 if (!isfinite(scores[k]))
405 return -3;
406 max_score = fmaxf(max_score, scores[k]);
407 }
408 double total = 0.0;
409 for (size_t k = 0; k < area; ++k) {
410 scores[k] = expf(scores[k] - max_score);
411 total += scores[k];
412 }
413 for (size_t c = 0; c < channels; ++c) {
414 const size_t base = (c * frames + t) * area;
415 double sum = 0.0;
416 for (size_t k = 0; k < area; ++k)
417 sum += (double)scores[k] * bf16_to_float(qkv[2 * count + base + k]);
418 const uint16_t value = float_to_bf16((float)(sum / total));
419 if (!isfinite(bf16_to_float(value)))
420 return -3;
421 staged[base + q] = value;
422 }
423 }
424 }
425 memcpy(output, staged, count * sizeof(*output));
426 return 0;
427}
static double dot(const float *a, const float *b, size_t n)
static int buffers(const void **inputs, const size_t *sizes, size_t count, float *out, size_t on, float *scratch, size_t sn, size_t needed)

References all_finite(), bf16_to_float(), buffers(), count, dot(), extent_product(), float_to_bf16(), overlap(), and region_valid().

◆ ck_video_spatial_conv2d_bf16_checked()

int ck_video_spatial_conv2d_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 channels,
size_t out_channels,
size_t frames,
size_t height,
size_t width,
size_t kernel_h,
size_t kernel_w,
size_t out_height,
size_t out_width,
size_t stride,
size_t pad_bottom,
size_t pad_right )

Definition at line 239 of file video_conv_bf16_checked.c.

245{
246 size_t source_count, weight_count, output_count;
247 const size_t source_dims[] = {channels, frames, height, width};
248 const size_t weight_dims[] = {out_channels, channels, kernel_h, kernel_w};
249 const size_t output_dims[] = {out_channels, frames, out_height, out_width};
250 if (!extent_product(source_dims, 4, &source_count) ||
251 !extent_product(weight_dims, 4, &weight_count) ||
252 !extent_product(output_dims, 4, &output_count) ||
253 !stride || stride > 65536 || pad_bottom > 65536 || pad_right > 65536 ||
254 height + pad_bottom < kernel_h || width + pad_right < kernel_w ||
255 out_height != 1 + (height + pad_bottom - kernel_h) / stride ||
256 out_width != 1 + (width + pad_right - kernel_w) / stride ||
257 input_elements < source_count || weight_elements < weight_count || bias_elements < out_channels ||
258 output_elements < output_count || staged_elements < output_count)
259 return -2;
260
261 const uint16_t *p[] = {input, weight, bias, output, staged};
262 const size_t count[] = {source_count, weight_count, out_channels, output_count, output_count};
263 for (size_t i = 0; i < 5; ++i) {
264 if (!region_valid(p[i], count[i]))
265 return -2;
266 if (i >= 3)
267 for (size_t j = 0; j < i; ++j)
268 if (overlap(p[i], count[i], p[j], count[j]))
269 return -2;
270 }
271 for (size_t i = 0; i < 3; ++i)
272 if (!all_finite(p[i], count[i]))
273 return -3;
274
275 struct video_conv_job job = {input, weight, bias, staged, channels, frames, height, width,
276 1, kernel_h, kernel_w, out_height, out_width, stride, 0, 0, 0};
277 return video_conv_run(&job, output, output_count, weight_count, out_channels);
278}

References all_finite(), count, extent_product(), overlap(), region_valid(), and video_conv_run().

◆ ck_video_temporal_stride_bf16_checked()

int ck_video_temporal_stride_bf16_checked ( const uint16_t * first,
const uint16_t * filtered,
uint16_t * output,
size_t input_elements,
size_t output_elements,
size_t channels,
size_t frames,
size_t height,
size_t width,
size_t out_frames,
size_t stride )

Definition at line 332 of file video_conv_bf16_checked.c.

336{
337 size_t source_count, output_count;
338 const size_t source_dims[] = {channels, frames, height, width};
339 const size_t output_dims[] = {channels, out_frames, height, width};
340 if (!extent_product(source_dims, 4, &source_count) ||
341 !extent_product(output_dims, 4, &output_count) ||
342 !stride || out_frames != 1 + (frames - 1) / stride ||
343 input_elements < source_count || output_elements < output_count ||
344 !region_valid(first, source_count) || !region_valid(filtered, source_count) ||
345 !region_valid(output, output_count) || overlap(output, output_count, first, source_count) ||
346 overlap(output, output_count, filtered, source_count))
347 return -2;
348
349 const size_t area = height * width;
350 for (size_t c = 0; c < channels; ++c) {
351 if (!all_finite(first + c * frames * area, area))
352 return -3;
353 for (size_t t = 1; t < out_frames; ++t)
354 if (!all_finite(filtered + (c * frames + t * stride) * area, area))
355 return -3;
356 }
357 for (size_t c = 0; c < channels; ++c) {
358 memcpy(output + c * out_frames * area, first + c * frames * area, area * sizeof(*output));
359 for (size_t t = 1; t < out_frames; ++t)
360 memcpy(output + (c * out_frames + t) * area,
361 filtered + (c * frames + t * stride) * area, area * sizeof(*output));
362 }
363 return 0;
364}

References all_finite(), extent_product(), overlap(), and region_valid().

◆ extent_product()

int extent_product ( const size_t dims[],
size_t n,
size_t * count )
static

Definition at line 30 of file video_conv_bf16_checked.c.

31{
32 *count = 1;
33 for (size_t i = 0; i < n; ++i) {
34 if (!dims[i] || dims[i] > 65536 || *count > SIZE_MAX / dims[i])
35 return 0;
36 *count *= dims[i];
37 }
38 return 1;
39}

References count.

Referenced by ck_video_block_mean_bf16_checked(), ck_video_causal_conv3d_bf16_checked(), ck_video_mean_patch_rows_bf16_checked(), ck_video_space_to_depth_bf16_checked(), ck_video_spatial_attention_bf16_checked(), ck_video_spatial_conv2d_bf16_checked(), and ck_video_temporal_stride_bf16_checked().

◆ overlap()

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

◆ region_valid()

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

◆ video_conv_planes()

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

Definition at line 83 of file video_conv_bf16_checked.c.

84{
85 struct video_conv_job *job = opaque;
86 const size_t channels = job->channels, frames = job->frames;
87 const size_t height = job->height, width = job->width;
88 const size_t kernel_t = job->kernel_t, kernel_h = job->kernel_h, kernel_w = job->kernel_w;
89 const size_t out_height = job->out_height, out_width = job->out_width;
90 const size_t stride = job->stride, pad_h = job->pad_h, pad_w = job->pad_w;
91 enum { TILE = 16 };
92 for (size_t oc = begin; oc < end; ++oc) {
93 const uint16_t *filter = job->weight + oc * channels * kernel_t * kernel_h * kernel_w;
94 const double offset = bf16_to_float(job->bias[oc]);
95 for (size_t t = 0; t < frames; ++t) {
96 const size_t first_tap_t = kernel_t - 1 > t ? kernel_t - 1 - t : 0;
97 for (size_t h = 0; h < out_height; ++h) {
98 const size_t y = h * stride;
99 const size_t first_tap_h = pad_h > y ? pad_h - y : 0;
100 const size_t limit_h = height + pad_h > y ? height + pad_h - y : 0;
101 const size_t end_tap_h = limit_h < kernel_h ? limit_h : kernel_h;
102 for (size_t w = 0; w < out_width; w += TILE) {
103 const size_t count = out_width - w < TILE ? out_width - w : TILE;
104 const size_t x = w * stride;
105 const int interior = count == TILE && x >= pad_w &&
106 x - pad_w + (TILE - 1) * stride + kernel_w <= width;
107 double sums[TILE] = {0.0};
108 for (size_t ic = 0; ic < channels; ++ic)
109 for (size_t kt = first_tap_t; kt < kernel_t; ++kt)
110 for (size_t kh = first_tap_h; kh < end_tap_h; ++kh) {
111 const size_t row_index = ((ic * frames + (t + kt + 1 - kernel_t)) * height +
112 y + kh - pad_h) * width;
113 const uint16_t *row = job->input + row_index;
114 const uint16_t *taps = filter + ((ic * kernel_t + kt) * kernel_h + kh) * kernel_w;
115 for (size_t kw = 0; kw < kernel_w; ++kw) {
116 const double wt = bf16_to_float(taps[kw]);
117 if (interior && stride == 1) {
118 const uint16_t *pixels = row + (x + kw - pad_w);
119 for (size_t lane = 0; lane < TILE; ++lane)
120 sums[lane] += wt * (double)bf16_to_float(pixels[lane]);
121 } else if (interior && stride == 2) {
122 const uint16_t *pixels = row + (x + kw - pad_w);
123 for (size_t lane = 0; lane < TILE; ++lane)
124 sums[lane] += wt * (double)bf16_to_float(pixels[lane * 2]);
125 } else {
126 for (size_t lane = 0; lane < count; ++lane) {
127 const size_t pos = x + kw + lane * stride;
128 if (pos >= pad_w && pos - pad_w < width)
129 sums[lane] += wt * (double)bf16_to_float(row[pos - pad_w]);
130 }
131 }
132 }
133 }
134 const size_t position = ((oc * frames + t) * out_height + h) * out_width + w;
135 for (size_t lane = 0; lane < count; ++lane) {
136 const uint16_t value = float_to_bf16((float)(sums[lane] + offset));
137 if (!isfinite(bf16_to_float(value))) {
138 atomic_store_explicit(&job->failed, 1, memory_order_relaxed);
139 return;
140 }
141 job->staged[position + lane] = value;
142 }
143 }
144 }
145 }
146 }
147}
uint32_t end
Definition utf8.c:215

References bf16_to_float(), count, end, and float_to_bf16().

Referenced by video_conv_run().

◆ video_conv_run()

int video_conv_run ( struct video_conv_job * job,
uint16_t * output,
size_t output_count,
size_t weight_count,
size_t out_channels )
static

Definition at line 149 of file video_conv_bf16_checked.c.

151{
152 if (output_count >= 1024 && weight_count >= 32768)
153 ck_checked_parallel_for(out_channels, 1, video_conv_planes, job);
154 else
155 video_conv_planes(0, out_channels, job);
156 if (atomic_load_explicit(&job->failed, memory_order_relaxed))
157 return -3;
158 memcpy(output, job->staged, output_count * sizeof(*output));
159 return 0;
160}
static void ck_checked_parallel_for(size_t count, int grain, ck_checked_range_fn fn, void *args)
static void video_conv_planes(size_t begin, size_t end, void *opaque)

References ck_checked_parallel_for(), and video_conv_planes().

Referenced by ck_video_causal_conv3d_bf16_checked(), and ck_video_spatial_conv2d_bf16_checked().