32 {
33 if (!groups || !option_rows || !channels ||
34 groups > SIZE_MAX / channels ||
35 option_rows > SIZE_MAX - groups * channels ||
36 !
span_fits(option_rows, option_stride, channels, option_elements) ||
37 !
span_fits(groups, field_stride, channels, field_elements) ||
38 !
span_fits(groups, output_stride, channels, output_elements) ||
39 count_elements < groups ||
40 scratch_elements < option_rows + groups * channels)
41 return -2;
42 if (!
valid_region(options, option_elements,
sizeof(
float), _Alignof(
float)) ||
43 !
valid_region(fields, field_elements,
sizeof(
float), _Alignof(
float)) ||
44 !
valid_region(group_counts, count_elements,
sizeof(int32_t), _Alignof(int32_t)) ||
45 !
valid_region(output, output_elements,
sizeof(
float), _Alignof(
float)) ||
46 !
valid_region(scratch, scratch_elements,
sizeof(
float), _Alignof(
float)))
47 return -1;
48 const size_t option_bytes = option_elements * sizeof(float);
49 const size_t field_bytes = field_elements * sizeof(float);
50 const size_t count_bytes = count_elements * sizeof(int32_t);
51 const size_t output_bytes = output_elements * sizeof(float);
52 const size_t scratch_bytes = scratch_elements * sizeof(float);
53 if (
overlaps(output, output_bytes, options, option_bytes) ||
54 overlaps(output, output_bytes, fields, field_bytes) ||
55 overlaps(output, output_bytes, group_counts, count_bytes) ||
56 overlaps(output, output_bytes, scratch, scratch_bytes) ||
57 overlaps(scratch, scratch_bytes, options, option_bytes) ||
58 overlaps(scratch, scratch_bytes, fields, field_bytes) ||
59 overlaps(scratch, scratch_bytes, group_counts, count_bytes))
60 return -1;
61
62 size_t counted = 0;
63 for (size_t q = 0; q < groups; ++q) {
64 int32_t
count = group_counts[q];
65 if (
count <= 0 || (
size_t)
count > option_rows - counted)
66 return -2;
67 counted += (size_t)
count;
68 for (size_t c = 0; c < channels; ++c)
69 if (!isfinite(fields[q * field_stride + c])) return -1;
70 }
71 if (counted != option_rows) return -2;
72 for (size_t r = 0; r < option_rows; ++r)
73 for (size_t c = 0; c < channels; ++c)
74 if (!isfinite(options[r * option_stride + c])) return -1;
75
76
77
78 size_t first = 0;
79 const double scale = 1.0 / sqrt((double)channels);
80 for (size_t q = 0; q < groups; ++q) {
81 size_t count = (size_t)group_counts[q];
82 double maximum = -INFINITY;
83 for (
size_t i = 0; i <
count; ++i) {
85 for (size_t c = 0; c < channels; ++c) {
87 (double)options[(first + i) * option_stride + c] *
88 (double)fields[q * field_stride + c];
90 }
91 double logit =
dot * scale;
92 if (!isfinite(logit) || fabs(logit) > FLT_MAX) return -1;
93 scratch[first + i] = (float)logit;
94 if ((double)scratch[first + i] > maximum)
95 maximum = (double)scratch[first + i];
96 }
97 double denominator = 0.0;
98 for (
size_t i = 0; i <
count; ++i) {
99 double value = exp((double)scratch[first + i] - maximum);
100 if (!isfinite(value)) return -1;
101 scratch[first + i] = (float)value;
102 denominator += (double)scratch[first + i];
103 }
104 if (!isfinite(denominator) || denominator <= 0.0) return -1;
105 for (
size_t i = 0; i <
count; ++i)
106 scratch[first + i] = (float)((double)scratch[first + i] /
107 denominator);
108 for (size_t c = 0; c < channels; ++c) {
109 double sum = 0.0;
110 for (
size_t i = 0; i <
count; ++i) {
111 volatile double product = (double)scratch[first + i] *
112 (double)options[(first + i) * option_stride + c];
114 }
115 if (!isfinite(sum) || fabs(sum) > FLT_MAX) return -1;
116 scratch[option_rows + q * channels + c] = (float)sum;
117 }
119 }
120 for (size_t q = 0; q < groups; ++q)
121 for (size_t c = 0; c < channels; ++c)
122 output[q * output_stride + c] =
123 scratch[option_rows + q * channels + c];
124 return 0;
125}
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 int span_fits(size_t rows, size_t stride, size_t width, size_t elements)
static int valid_region(const void *ptr, size_t elements, size_t bytes, size_t alignment)
static int overlaps(const void *left, size_t left_bytes, const void *right, size_t right_bytes)
const uint8_t size_t count