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

Refactored and referenced optimized implmentation. #27131

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
17 changes: 5 additions & 12 deletions tensorflow/lite/kernels/activations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,8 @@ TfLiteStatus ReluEval(TfLiteContext* context, TfLiteNode* node) {
TfLiteTensor* output = GetOutput(context, node, 0);
switch (input->type) {
case kTfLiteFloat32: {
size_t elements = input->bytes / sizeof(float);
float* in = input->data.f;
float* in_end = in + elements;
float* out = output->data.f;
for (; in < in_end; in++, out++) *out = std::max(0.f, *in);
optimized_ops::Relu(GetTensorShape(input), GetTensorData<float>(input),
GetTensorShape(output), GetTensorData<float>(output));
amitsrivastava78 marked this conversation as resolved.
Show resolved Hide resolved
return kTfLiteOk;
} break;
default:
Expand All @@ -352,13 +349,9 @@ TfLiteStatus Relu1Eval(TfLiteContext* context, TfLiteNode* node) {
TfLiteTensor* output = GetOutput(context, node, 0);
switch (input->type) {
case kTfLiteFloat32: {
size_t elements = input->bytes / sizeof(float);
float* in = input->data.f;
float* in_end = in + elements;
float* out = output->data.f;
for (; in < in_end; in++, out++) {
*out = std::min(std::max(-1.f, *in), 1.f);
}
optimized_ops::Relu1(GetTensorShape(input), GetTensorData<float>(input),
GetTensorShape(output),
GetTensorData<float>(output));
return kTfLiteOk;
} break;
default:
Expand Down
24 changes: 13 additions & 11 deletions tensorflow/lite/kernels/internal/reference/reference_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,26 +246,28 @@ inline void Elu(const RuntimeShape& input_shape, const float* input_data,
}
}

inline void Relu(const RuntimeShape& input_shape, const float* input_data,
const RuntimeShape& output_shape, float* output_data) {
template <typename T>
inline void Relu(const RuntimeShape& input_shape, const T* input_data,
const RuntimeShape& output_shape, T* output_data) {
const int flat_size = MatchingFlatSize(input_shape, output_shape);
for (int i = 0; i < flat_size; ++i) {
const float val = input_data[i];
const float lower = 0;
const float clamped = val < lower ? lower : val;
const T val = input_data[i];
const T lower = 0;
const T clamped = val < lower ? lower : val;
output_data[i] = clamped;
}
}

inline void Relu1(const RuntimeShape& input_shape, const float* input_data,
const RuntimeShape& output_shape, float* output_data) {
template <typename T>
inline void Relu1(const RuntimeShape& input_shape, const T* input_data,
const RuntimeShape& output_shape, T* output_data) {
gemmlowp::ScopedProfilingLabel label("Relu1 (not fused)");
const int flat_size = MatchingFlatSize(input_shape, output_shape);
for (int i = 0; i < flat_size; ++i) {
const float val = input_data[i];
const float upper = 1;
const float lower = -1;
const float clamped = val > upper ? upper : val < lower ? lower : val;
const T val = input_data[i];
const T upper = 1;
const T lower = -1;
const T clamped = val > upper ? upper : val < lower ? lower : val;
output_data[i] = clamped;
}
}
Expand Down