12 return ptr && (uintptr_t)ptr % _Alignof(uint16_t) == 0 &&
13 elements <= (UINTPTR_MAX - (uintptr_t)ptr) /
sizeof(*ptr);
16static int overlap(
const uint16_t *a,
size_t a_count,
const uint16_t *b,
size_t b_count)
18 return (uintptr_t)a < (uintptr_t)b + b_count *
sizeof(*b) &&
19 (uintptr_t)b < (uintptr_t)a + a_count *
sizeof(*a);
22static int all_finite(
const uint16_t *ptr,
size_t elements)
24 for (
size_t i = 0; i < elements; ++i)
33 for (
size_t i = 0; i < n; ++i) {
34 if (!dims[i] || dims[i] > 65536 || *
count > SIZE_MAX / dims[i])
43 const uint16_t *input,
size_t input_elements, uint16_t *output,
size_t output_elements,
44 size_t channels,
size_t frames,
size_t height,
size_t width,
size_t patch)
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 ||
51 overlap(input, elements, output, elements))
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];
67struct video_conv_job {
68 const uint16_t *input, *weight, *bias;
70 size_t channels, frames, height, width, kernel_t, kernel_h, kernel_w;
71 size_t out_height, out_width, stride, pad_h, pad_w;
80#if defined(__x86_64__) && defined(__GNUC__) && defined(__ELF__)
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;
92 for (
size_t oc = begin; oc <
end; ++oc) {
93 const uint16_t *filter = job->weight + oc * channels * kernel_t * kernel_h * kernel_w;
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) {
117 if (interior && stride == 1) {
118 const uint16_t *pixels = row + (x + kw - pad_w);
119 for (
size_t lane = 0; lane < TILE; ++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)
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)
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));
138 atomic_store_explicit(&job->failed, 1, memory_order_relaxed);
141 job->staged[position + lane] = value;
150 size_t output_count,
size_t weight_count,
size_t out_channels)
152 if (output_count >= 1024 && weight_count >= 32768)
156 if (atomic_load_explicit(&job->failed, memory_order_relaxed))
158 memcpy(output, job->staged, output_count *
sizeof(*output));
164 const uint16_t *input,
size_t input_elements,
const uint16_t *weight,
size_t weight_elements,
165 const uint16_t *bias,
size_t bias_elements, uint16_t *output,
size_t output_elements,
166 uint16_t *staged,
size_t staged_elements,
size_t channels,
size_t out_channels,
167 size_t frames,
size_t height,
size_t width,
size_t kernel_t,
size_t kernel_h,
size_t kernel_w)
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};
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)
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) {
185 for (
size_t j = 0; j < i; ++j)
189 for (
size_t i = 0; i < 3; ++i)
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);
200 uint16_t *output, uint16_t *staged,
201 size_t channels,
size_t positions,
size_t elements)
203 if (!channels || channels > 65536 || !positions || positions > SIZE_MAX / channels)
205 const size_t count = channels * positions;
215 const float scale = sqrtf((
float)channels);
216 for (
size_t p = 0; p < positions; ++p) {
218 for (
size_t c = 0; c < channels; ++c) {
222 const float norm = fmaxf(sqrtf((
float)sum), 1e-12f);
225 for (
size_t c = 0; c < channels; ++c) {
226 const size_t i = c * positions + p;
234 memcpy(output, staged,
count *
sizeof(*output));
240 const uint16_t *input,
size_t input_elements,
const uint16_t *weight,
size_t weight_elements,
241 const uint16_t *bias,
size_t bias_elements, uint16_t *output,
size_t output_elements,
242 uint16_t *staged,
size_t staged_elements,
size_t channels,
size_t out_channels,
243 size_t frames,
size_t height,
size_t width,
size_t kernel_h,
size_t kernel_w,
244 size_t out_height,
size_t out_width,
size_t stride,
size_t pad_bottom,
size_t pad_right)
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};
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)
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) {
267 for (
size_t j = 0; j < i; ++j)
271 for (
size_t i = 0; i < 3; ++i)
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);
282 const uint16_t *input, uint16_t *output, uint16_t *staged,
283 size_t input_elements,
size_t output_elements,
size_t channels,
size_t out_channels,
284 size_t frames,
size_t height,
size_t width,
size_t out_frames,
size_t out_height,
285 size_t out_width,
size_t temporal_factor,
size_t spatial_factor)
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};
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 ||
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))
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;
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) {
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)
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;
327 memcpy(output, staged, output_count *
sizeof(*output));
333 const uint16_t *first,
const uint16_t *filtered, uint16_t *output,
334 size_t input_elements,
size_t output_elements,
size_t channels,
size_t frames,
335 size_t height,
size_t width,
size_t out_frames,
size_t stride)
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};
342 !stride || out_frames != 1 + (frames - 1) / stride ||
343 input_elements < source_count || output_elements < output_count ||
345 !
region_valid(output, output_count) ||
overlap(output, output_count, first, source_count) ||
346 overlap(output, output_count, filtered, source_count))
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))
353 for (
size_t t = 1; t < out_frames; ++t)
354 if (!
all_finite(filtered + (c * frames + t * stride) * area, area))
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));
368 const uint16_t *qkv, uint16_t *output, uint16_t *staged,
float *scores,
369 size_t input_elements,
size_t output_elements,
size_t positions,
370 size_t channels,
size_t frames,
size_t height,
size_t width,
size_t qkv_channels)
373 const size_t dims[] = {channels, frames, height, width};
374 const size_t spatial_dims[] = {height, width};
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) ||
381 const uint16_t *
buffers[] = {qkv, output, staged, (
const uint16_t *)scores};
383 for (
size_t i = 0; i < 4; ++i) {
386 for (
size_t j = 0; j < i; ++j)
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) {
399 for (
size_t c = 0; c < channels; ++c) {
400 const size_t base = (c * frames + t) * area;
403 scores[k] = (float)
dot * scale;
404 if (!isfinite(scores[k]))
406 max_score = fmaxf(max_score, scores[k]);
409 for (
size_t k = 0; k < area; ++k) {
410 scores[k] = expf(scores[k] - max_score);
413 for (
size_t c = 0; c < channels; ++c) {
414 const size_t base = (c * frames + t) * area;
416 for (
size_t k = 0; k < area; ++k)
421 staged[base + q] = value;
425 memcpy(output, staged,
count *
sizeof(*output));
431 const uint16_t *moments,
const uint16_t *mean,
const uint16_t *inv_std,
432 uint16_t *output, uint16_t *staged,
size_t input_elements,
size_t output_elements,
433 size_t channels,
size_t moment_channels,
size_t frames,
size_t height,
size_t width,
434 size_t selected_frames,
size_t patch,
size_t rows,
size_t row_width)
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)
440 const size_t input_dims[] = {moment_channels, frames, height, width};
441 const size_t output_dims[] = {rows, row_width};
443 input_elements < source_count || output_elements < output_count)
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};
449 rows != expected_rows || row_width != expected_width)
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) {
458 for (
size_t j = 0; j < i; ++j)
459 if (
overlap(p[i], sizes[i], p[j], sizes[j]))
462 for (
size_t c = 0; c < channels; ++c)
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));
479 for (
size_t c = 0; c < channels; ++c) {
480 const float x =
bf16_to_float(moments[((c * frames + t) * height + h) * width + w]);
485 staged[offset++] = value;
488 memcpy(output, staged, output_count *
sizeof(*output));
static float centered(float value, int zero)
static double dot(const float *a, const float *b, size_t n)
static uint16_t float_to_bf16(float f)
static float bf16_to_float(uint16_t v)
static void ck_checked_parallel_for(size_t count, int grain, ck_checked_range_fn fn, void *args)
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)
__attribute__((visibility("default"))) CKTokenizer *ck_tokenizer_create(CKTokenizerType type)
int32_t int32_t int32_t int32_t pad
const uint8_t size_t count
static int extent_product(const size_t dims[], size_t n, size_t *count)
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)
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)
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_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)
static void video_conv_planes(size_t begin, size_t end, void *opaque)
static int all_finite(const uint16_t *ptr, size_t elements)
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)
static int region_valid(const uint16_t *ptr, size_t elements)
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_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)
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)
static int overlap(const uint16_t *a, size_t a_count, const uint16_t *b, size_t b_count)