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

Draft: [onert-micro] Bring up SparseCrossEntropy Loss #13431

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions onert-micro/onert-micro/include/OMConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ enum OMMetrics
MAE_METRICS,
CROSS_ENTROPY_METRICS,
ACCURACY,
SPARSE_CROSS_ENTROPY_METRICS,
SPARSE_CROSS_ENTROPY_ACCURACY,
};

/*
Expand All @@ -50,6 +52,7 @@ enum OMLoss
{
BINARY_CROSS_ENTROPY,
CROSS_ENTROPY,
SPARSE_CROSS_ENTROPY,
MSE,
MAE,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

/*
* Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef ONERT_MICRO_TRAIN_LOSSES_FUNCTIONS_SPARSE_CROSS_ENTROPY_H
#define ONERT_MICRO_TRAIN_LOSSES_FUNCTIONS_SPARSE_CROSS_ENTROPY_H

#include "OMStatus.h"

#include <cstdint>

namespace onert_micro
{
namespace train
{
namespace losses_functions
{

// Cross Entropy
struct SparseCrossEntropy
{
// Calculate sparse cross entropy error backpropagation between calculated and target data
static void calculateErrorBackpropagation(const uint32_t flat_size, const float *calculated_data,
const float *target_data, float *output_grad);
};

} // namespace losses_functions
} // namespace train
} // namespace onert_micro

#endif // ONERT_MICRO_TRAIN_LOSSES_FUNCTIONS_SPARSE_CROSS_ENTROPY_H
43 changes: 43 additions & 0 deletions onert-micro/onert-micro/include/train/metrics/SparseCrossEntropy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

/*
* Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef ONERT_MICRO_TRAIN_METRICS_SPARSE_CROSS_ENTROPY_H
#define ONERT_MICRO_TRAIN_METRICS_SPARSE_CROSS_ENTROPY_H

#include "OMStatus.h"

#include <cstdint>

namespace onert_micro
{
namespace train
{
namespace metrics
{

struct SparseCrossEntropy
{
// Calculate sparse cross entropy metric between calculated and target data
static float calculateValue(const uint32_t flat_size, const float *calculated_data,
const float *target_data);
};

} // namespace metrics
} // namespace train
} // namespace onert_micro

#endif // ONERT_MICRO_TRAIN_METRICS_SPARSE_CROSS_ENTROPY_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef ONERT_MICRO_TRAIN_METRICS_SPARSE_CROSS_ENTROPY_ACCURACY_H
#define ONERT_MICRO_TRAIN_METRICS_SPARSE_CROSS_ENTROPY_ACCURACY_H

#include "OMStatus.h"

#include <cstdint>

namespace onert_micro
{
namespace train
{
namespace metrics
{

// Accuracy metric
struct SparseCrossEntropyAccuracy
{
// Calculate accuracy metric between calculated and target data
static float calculateValue(const uint32_t flat_size, const float *calculated_data,
const float *target_data);
};

} // namespace metrics
} // namespace train
} // namespace onert_micro

#endif // ONERT_MICRO_TRAIN_METRICS_SPARSE_CROSS_ENTROPY_ACCURACY_H
16 changes: 14 additions & 2 deletions onert-micro/onert-micro/include/train/tests/OMTestUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ OMStatus train(OMTrainingInterpreter &train_interpreter, OMConfig &config,
const uint32_t num_train_data_samples = test_base.getTrainNumSamples();
const uint32_t batch_size = config.training_context.batch_size;
const uint32_t input_size = train_interpreter.getInputSizeAt(0);
const uint32_t target_size = train_interpreter.getOutputSizeAt(0);
uint32_t target_size = train_interpreter.getOutputSizeAt(0);

// TODO: Need to revisit this to make getOuputSize can get proper output number
// when 'all' target number and output numbers are different
if (config.training_context.loss == SPARSE_CROSS_ENTROPY)
target_size = 1;

for (uint32_t e = 0; e < training_epochs; ++e)
{
config.training_context.num_epoch = e + 1;
Expand Down Expand Up @@ -92,7 +98,13 @@ OMStatus evaluate(OMTrainingInterpreter &train_interpreter, OMConfig &config,
const uint32_t num_test_data_samples = test_base.getTestNumSamples();
const uint32_t batch_size = 1;
const uint32_t input_size = train_interpreter.getInputSizeAt(0);
const uint32_t target_size = train_interpreter.getOutputSizeAt(0);
uint32_t target_size = train_interpreter.getOutputSizeAt(0);

// TODO: Need to revisit this to make getOuputSize can get proper output number
// when 'all' target number and output numbers are different
if (config.training_context.loss == SPARSE_CROSS_ENTROPY)
target_size = 1;

for (int i = 0; i < num_test_data_samples; ++i)
{
// Read current input and target data
Expand Down
Loading
Loading