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

[Bug] Fix bug when Android build #29

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void CrossEntropySigmoidLossLayer::forwarding(RunLayerContext &context,
.multiply(-1.0)
.apply<float>(static_cast<float (*)(float)>(&std::exp))
.add(1.0)
.apply<float>(logFloat);
.apply<float>(logFloat<float>);
mid_term = mid_term.add(y.apply<float>(ActiFunc::relu<float>));

// y * y2
Expand Down
38 changes: 31 additions & 7 deletions nntrainer/layers/loss/cross_entropy_softmax_loss_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,31 @@ void CrossEntropySoftmaxLossLayer::forwarding(RunLayerContext &context,
Tensor &y = context.getInput(SINGLE_INOUT_IDX);

// fill the output
hidden_ = y.apply(ActiFunc::softmax, hidden_);
auto dataType = y.getDataType();
if (dataType == ml::train::TensorDim::DataType::FP32) {
hidden_ = y.apply(ActiFunc::softmax<float>, hidden_);

if (context.isLabelAvailable(SINGLE_INOUT_IDX)) {
Tensor &y2 = context.getLabel(SINGLE_INOUT_IDX);
l = y2.multiply(hidden_.apply<float>(logFloat)).sum_by_batch().multiply(-1);
if (context.isLabelAvailable(SINGLE_INOUT_IDX)) {
Tensor &y2 = context.getLabel(SINGLE_INOUT_IDX);
l = y2.multiply(hidden_.apply<float>(logFloat<float>))
.sum_by_batch()
.multiply(-1);

// update the loss value
LossLayer::updateLoss(context, l);
// update the loss value
LossLayer::updateLoss(context, l);
}
} else if (dataType == ml::train::TensorDim::DataType::FP16) {
hidden_ = y.apply(ActiFunc::softmax<_FP16>, hidden_);

if (context.isLabelAvailable(SINGLE_INOUT_IDX)) {
Tensor &y2 = context.getLabel(SINGLE_INOUT_IDX);
l = y2.multiply(hidden_.apply<_FP16>(logFloat<_FP16>))
.sum_by_batch()
.multiply(-1);

// update the loss value
LossLayer::updateLoss(context, l);
}
}
}

Expand All @@ -46,13 +63,20 @@ void CrossEntropySoftmaxLossLayer::calcDerivative(RunLayerContext &context) {
const Tensor &y2 = context.getIncomingDerivative(SINGLE_INOUT_IDX);
Tensor &y = context.getInput(SINGLE_INOUT_IDX);

auto dataType = y.getDataType();

Tensor ret;
ret.setDataType(dataType);
if (dataType == ml::train::TensorDim::DataType::FP32) {
y.apply(ActiFunc::softmax<float>, ret);
} else if (dataType == ml::train::TensorDim::DataType::FP16) {
y.apply(ActiFunc::softmax<_FP16>, ret);
}

/// @note y and ret_derivative can be same here, so this has to be out-place
/// operation
// TODO: verify y and ret_derivative must not be same as loss layer is not
// working in-place
y.apply(ActiFunc::softmax, ret);
ret.subtract(y2, ret_derivative);
if (ret_derivative.divide_i(ret.batch()) != ML_ERROR_NONE) {
throw std::runtime_error("[CrossEntropySoftmaxLossLayer::calcDerivative] "
Expand Down
2 changes: 0 additions & 2 deletions nntrainer/utils/util_func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ float sqrtFloat(float x) { return sqrt(x); };

double sqrtDouble(double x) { return sqrt(x); };

float logFloat(float x) { return log(x + 1.0e-20); }

bool isFileExist(std::string file_name) {
std::ifstream infile(file_name);
return infile.good();
Expand Down
4 changes: 4 additions & 0 deletions nntrainer/utils/util_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ double sqrtDouble(double x);
*/
float logFloat(float x);

template <typename T = float> T logFloat(T x) {
return static_cast<T>(log(x + 1.0e-20));
}

/**
* @brief exp function for float type
* @param[in] x float
Expand Down
2 changes: 1 addition & 1 deletion test/unittest/unittest_util_func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ TEST(nntrainer_util_func, logFloat_01_p) {
nntrainer::Tensor input(batch, channel, height, width);
GEN_TEST_INPUT(input, i * (width) + k + 1);

nntrainer::Tensor Results = input.apply<float>(nntrainer::logFloat);
nntrainer::Tensor Results = input.apply<float>(nntrainer::logFloat<float>);

float *data = Results.getData();
ASSERT_NE(nullptr, data);
Expand Down