64 lines
2.2 KiB
C++
64 lines
2.2 KiB
C++
/* Copyright 2017 The TensorFlow Authors. 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 TENSORFLOW_COMPILER_MLIR_KERNELS_PADDING_H_
|
|
#define TENSORFLOW_COMPILER_MLIR_KERNELS_PADDING_H_
|
|
|
|
typedef enum {
|
|
kTfLitePaddingUnknown = 0,
|
|
kTfLitePaddingSame,
|
|
kTfLitePaddingValid,
|
|
} TfLitePadding;
|
|
|
|
// LINT.IfChange
|
|
namespace tflite_migration {
|
|
|
|
// Matching GetWindowedOutputSize in TensorFlow.
|
|
inline int ComputeOutSize(TfLitePadding padding, int image_size,
|
|
int filter_size, int stride, int dilation_rate = 1) {
|
|
int effective_filter_size = (filter_size - 1) * dilation_rate + 1;
|
|
|
|
// TODO(b/186448822): This uses 0 since the function has no other way to
|
|
// report error case
|
|
if (stride == 0) return 0;
|
|
|
|
switch (padding) {
|
|
case kTfLitePaddingSame:
|
|
return (image_size + stride - 1) / stride;
|
|
case kTfLitePaddingValid:
|
|
return (image_size + stride - effective_filter_size) / stride;
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
// It's not guaranteed that padding is symmetric. It's important to keep
|
|
// offset for algorithms need all paddings.
|
|
inline int ComputePaddingWithOffset(int stride, int dilation_rate, int in_size,
|
|
int filter_size, int out_size,
|
|
int* offset) {
|
|
int effective_filter_size = (filter_size - 1) * dilation_rate + 1;
|
|
int total_padding =
|
|
((out_size - 1) * stride + effective_filter_size - in_size);
|
|
total_padding = total_padding > 0 ? total_padding : 0;
|
|
*offset = total_padding % 2;
|
|
return total_padding / 2;
|
|
}
|
|
|
|
} // namespace tflite_migration
|
|
|
|
// LINT.ThenChange(//tensorflow/lite/kernels/padding.h)
|
|
|
|
#endif // TENSORFLOW_COMPILER_MLIR_KERNELS_PADDING_H_
|