17 const int32_t *word_ids,
const int32_t *type_ids,
size_t id_elements,
18 const float *word,
size_t word_elements,
size_t vocab_size,
19 const float *position,
size_t position_elements,
size_t position_count,
20 const float *token_type,
size_t type_elements,
size_t type_count,
21 const float *gamma,
const float *beta,
size_t norm_elements,
22 float *output,
size_t output_elements,
size_t tokens,
size_t channels,
23 size_t weight_stride,
size_t output_stride,
float epsilon)
25 if (!word_ids || !type_ids || !word || !position || !token_type ||
26 !gamma || !beta || !output || !tokens || !channels ||
27 id_elements < tokens || norm_elements < channels ||
28 !isfinite(epsilon) || epsilon <= 0.0f ||
30 !
span_fits(position_count, weight_stride, channels, position_elements) ||
31 !
span_fits(type_count, weight_stride, channels, type_elements) ||
32 !
span_fits(tokens, output_stride, channels, output_elements) ||
33 tokens > position_count)
37 for (
size_t c = 0; c < channels; ++c)
38 if (!isfinite(gamma[c]) || !isfinite(beta[c]))
return -1;
39 for (
size_t t = 0; t < tokens; ++t) {
40 if (word_ids[t] < 0 || (
size_t)word_ids[t] >=
vocab_size ||
41 type_ids[t] < 0 || (
size_t)type_ids[t] >= type_count)
43 const size_t wi = (size_t)word_ids[t] * weight_stride;
44 const size_t ti = (size_t)type_ids[t] * weight_stride;
45 const size_t pi = t * weight_stride;
46 for (
size_t c = 0; c < channels; ++c)
47 if (!isfinite(word[wi + c]) || !isfinite(token_type[ti + c]) ||
48 !isfinite(position[pi + c]))
return -1;
51 for (
size_t t = 0; t < tokens; ++t) {
52 const float *w = word + (size_t)word_ids[t] * weight_stride;
53 const float *ty = token_type + (size_t)type_ids[t] * weight_stride;
54 const float *p = position + t * weight_stride;
55 double sum = 0.0, square_sum = 0.0;
56 for (
size_t c = 0; c < channels; ++c) {
57 const float a = w[c] + ty[c];
58 const float x = a + p[c];
60 square_sum += (double)x * (
double)x;
62 const double mean = sum / (double)channels;
63 const double variance = fmax(0.0, square_sum / (
double)channels - mean * mean);
64 const float inv_std = (float)(1.0 / sqrt(variance + (
double)epsilon));
65 float *dst = output + t * output_stride;
66 for (
size_t c = 0; c < channels; ++c) {
67 const float a = w[c] + ty[c];
68 const float x = a + p[c];
69 dst[c] = ((x - (float)mean) * inv_std) * gamma[c] + beta[c];
int embedding_three_table_layer_norm_f32(const int32_t *word_ids, const int32_t *type_ids, size_t id_elements, const float *word, size_t word_elements, size_t vocab_size, const float *position, size_t position_elements, size_t position_count, const float *token_type, size_t type_elements, size_t type_count, const float *gamma, const float *beta, size_t norm_elements, float *output, size_t output_elements, size_t tokens, size_t channels, size_t weight_stride, size_t output_stride, float epsilon)