Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[onert-micro] Revise softmax input grad #14100

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
chunseoklee marked this conversation as resolved.
Show resolved Hide resolved
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