Skip to content

Commit

Permalink
[onert-micro] Revise softmax input grad (#14100)
Browse files Browse the repository at this point in the history
- Fix softmax input grad computation

ONE-DCO-1.0-Signed-off-by: Chunseok Lee <[email protected]>
  • Loading branch information
chunseoklee authored Sep 27, 2024
1 parent eb72364 commit a163a66
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions onert-micro/onert-micro/include/pal/common/PALSoftmaxInputGrad.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,25 @@ void inline SoftmaxInputGrad(const float *dloss_doutput_data,
{
assert(dloss_doutput_shape.dimensionsCount() == 2);
assert(dloss_doutput_shape.dims(0) == 1);
const uint32_t output_dim = dloss_doutput_shape.dims(dloss_doutput_shape.dimensionsCount() - 1);
for (int i = 0; i < output_dim; ++i)
const uint32_t width = dloss_doutput_shape.dims(dloss_doutput_shape.dimensionsCount() - 1);
for (int w1 = 0; w1 < width; ++w1)
{
for (int j = 0; j < output_dim; ++j)
float sum = 0.0f;
for (int w2 = 0; w2 < width; ++w2)
{
jacobian_row_data[j] = -calculated_data[i] * calculated_data[j];
float val;
if (w1 == w2)
{
val = calculated_data[w2] * (1.f - calculated_data[w2]);
}
else
{
val = -calculated_data[w2] * calculated_data[w1];
}
val *= dloss_doutput_data[w2];
sum += val;
}
jacobian_row_data[i] += calculated_data[i];
float total = 0.f;
for (int j = 0; j < output_dim; ++j)
{
total += jacobian_row_data[j] * dloss_doutput_data[j];
}
dloss_dinput_data[i] = total;
dloss_dinput_data[w1] = sum;
}
}

Expand Down

0 comments on commit a163a66

Please sign in to comment.