← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
spatial_window_layout_checked.c
Go to the documentation of this file.
1#include <stddef.h>
2#include <stdint.h>
3#include <string.h>
4
6 const uint8_t *input, uint8_t *output, size_t input_elements, size_t output_elements,
7 size_t height, size_t width, size_t window_h, size_t window_w, size_t heads, size_t channels,
8 size_t unpack, size_t item_bytes)
9{
10 if (!height || height > 512 || !width || width > 512 ||
11 !window_h || window_h > 64 || !window_w || window_w > 64 ||
12 !heads || heads > 32 || !channels || channels > 1024 || heads * channels > 4096 ||
13 unpack > 1)
14 return -2;
15 const size_t ny = (height + window_h - 1) / window_h;
16 const size_t nx = (width + window_w - 1) / window_w;
17 const size_t raster = height * width * heads * channels;
18 const size_t packed = ny * nx * heads * window_h * window_w * channels;
19 if (packed > (1u << 26) ||
20 input_elements != (unpack ? packed : raster) || output_elements != (unpack ? raster : packed))
21 return -2;
22
23 const size_t in_bytes = input_elements * item_bytes;
24 const size_t out_bytes = output_elements * item_bytes;
25 const uintptr_t in = (uintptr_t)input, out = (uintptr_t)output;
26 if (!input || !output || in % item_bytes || out % item_bytes ||
27 in > UINTPTR_MAX - in_bytes || out > UINTPTR_MAX - out_bytes ||
28 (in < out + out_bytes && out < in + in_bytes))
29 return -1;
30
31 const size_t row_bytes = channels * item_bytes;
32 for (size_t wy = 0; wy < ny; ++wy) {
33 for (size_t wx = 0; wx < nx; ++wx) {
34 for (size_t h = 0; h < heads; ++h) {
35 const size_t plane = ((wy * nx + wx) * heads + h) * window_h * window_w;
36 for (size_t y = 0; y < window_h; ++y) {
37 const size_t iy = wy * window_h + y;
38 for (size_t x = 0; x < window_w; ++x) {
39 const size_t ix = wx * window_w + x;
40 const size_t p = (plane + y * window_w + x) * channels;
41 if (iy >= height || ix >= width) {
42 if (!unpack)
43 memset(output + p * item_bytes, 0, row_bytes);
44 continue;
45 }
46 const size_t r = ((iy * width + ix) * heads + h) * channels;
47 if (unpack)
48 memcpy(output + r * item_bytes, input + p * item_bytes, row_bytes);
49 else
50 memcpy(output + p * item_bytes, input + r * item_bytes, row_bytes);
51 }
52 }
53 }
54 }
55 }
56 return 0;
57}
58
60 const uint16_t *input, uint16_t *output, size_t input_elements, size_t output_elements,
61 size_t height, size_t width, size_t window_h, size_t window_w, size_t heads, size_t channels,
62 size_t unpack)
63{
64 return spatial_window_layout((const uint8_t *)input, (uint8_t *)output, input_elements, output_elements,
65 height, width, window_h, window_w, heads, channels, unpack, sizeof(*input));
66}
67
69 const float *input, float *output, size_t input_elements, size_t output_elements,
70 size_t height, size_t width, size_t window_h, size_t window_w, size_t heads, size_t channels,
71 size_t unpack)
72{
73 return spatial_window_layout((const uint8_t *)input, (uint8_t *)output, input_elements, output_elements,
74 height, width, window_h, window_w, heads, channels, unpack, sizeof(*input));
75}
int ck_spatial_window_layout_bf16_checked(const uint16_t *input, uint16_t *output, size_t input_elements, size_t output_elements, size_t height, size_t width, size_t window_h, size_t window_w, size_t heads, size_t channels, size_t unpack)
int ck_spatial_window_layout_f32_checked(const float *input, float *output, size_t input_elements, size_t output_elements, size_t height, size_t width, size_t window_h, size_t window_w, size_t heads, size_t channels, size_t unpack)
static int spatial_window_layout(const uint8_t *input, uint8_t *output, size_t input_elements, size_t output_elements, size_t height, size_t width, size_t window_h, size_t window_w, size_t heads, size_t channels, size_t unpack, size_t item_bytes)