38 const int axis = (params.
axis < 0) ? params.
axis + rank : params.
axis;
39 const double beta = params.
beta;
43 for (
int i = 0; i < axis; ++i)
45 outer_size *= input_shape.
Dims(i);
49 for (
int i = axis + 1; i < rank; ++i)
51 inner_size *= input_shape.
Dims(i);
54 for (
int i = 0; i < outer_size; ++i)
56 for (
int j = 0; j < inner_size; ++j)
58 float max = std::numeric_limits<float>::lowest();
59 for (
int c = 0; c < depth; ++c)
61 max = std::max(max, input_data[(i * depth + c) * inner_size]);
65 for (
int c = 0; c < depth; ++c)
67 sum += std::exp((input_data[(i * depth + c) * inner_size + j] - max) * beta);
70 const float log_sum = std::log(sum);
71 for (
int c = 0; c < depth; ++c)
73 output_data[(i * depth + c) * inner_size + j] =
74 (input_data[(i * depth + c) * inner_size + j] - max) * beta - log_sum;
84 const int axis = (params.
axis < 0) ? params.
axis + rank : params.
axis;
85 const double beta = params.
beta;
88 const int32_t clamp_max = std::numeric_limits<uint8_t>::max();
89 const int32_t clamp_min = std::numeric_limits<uint8_t>::min();
92 for (
int i = 0; i < axis; ++i)
94 outer_size *= input_shape.
Dims(i);
98 for (
int i = axis + 1; i < rank; ++i)
100 inner_size *= input_shape.
Dims(i);
103 for (
int i = 0; i < outer_size; ++i)
105 for (
int j = 0; j < inner_size; ++j)
107 uint8_t max_val = std::numeric_limits<uint8_t>::min();
108 for (
int c = 0; c < depth; ++c)
110 max_val = std::max(max_val, input_data[(i * depth + c) * inner_size]);
113 float sum_exp = 0.0f;
114 const int32_t max_uint8 = std::numeric_limits<uint8_t>::max();
115 const float *table_offset = ¶ms.
table[max_uint8 - max_val];
116 for (
int c = 0; c < depth; ++c)
118 sum_exp += table_offset[input_data[(i * depth + c) * inner_size]];
120 const float log_sum_exp = std::log(sum_exp);
122 const float scale = input_scale / params.
scale;
123 const float precomputed = (input_scale * max_val * beta + log_sum_exp) / params.
scale;
124 for (
int c = 0; c < depth; ++c)
126 const float log_prob =
127 scale * input_data[(i * depth + c) * inner_size] * beta - precomputed;
128 const int32_t prob_quantized = std::rint(log_prob) + params.
zero_point;
129 output_data[(i * depth + c) * inner_size] =
130 static_cast<uint8_t
>(std::max(std::min(clamp_max, prob_quantized), clamp_min));
void LogSoftmax(const SoftmaxParams ¶ms, const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)