Skip to content

Commit 38c657a

Browse files
authoredNov 7, 2023
Automated sync from github.com/tensorflow/tensorflow (#2297)
BUG=automated sync from upstream NO_CHECK_TFLITE_FILES=automated sync from upstream
1 parent c845961 commit 38c657a

File tree

6 files changed

+306
-162
lines changed

6 files changed

+306
-162
lines changed
 

‎tensorflow/lite/core/api/flatbuffer_conversions.cc

+87-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License.
1515

1616
#include "tensorflow/lite/core/api/flatbuffer_conversions.h"
1717

18+
#include <algorithm>
1819
#include <cstddef>
1920
#include <cstdint>
2021
#include <memory>
@@ -881,6 +882,10 @@ TfLiteStatus ParseOpDataTfLite(const Operator* op, BuiltinOperator op_type,
881882
case BuiltinOperator_STABLEHLO_GATHER: {
882883
return ParseStablehloGather(op, error_reporter, allocator, builtin_data);
883884
}
885+
case BuiltinOperator_STABLEHLO_REDUCE_WINDOW: {
886+
return ParseStablehloReduceWindow(op, error_reporter, allocator,
887+
builtin_data);
888+
}
884889
case BuiltinOperator_REDUCE_WINDOW: {
885890
auto params = safe_allocator.Allocate<TfLiteReduceWindowParams>();
886891
TF_LITE_ENSURE(error_reporter, params != nullptr);
@@ -949,7 +954,6 @@ TfLiteStatus ParseOpDataTfLite(const Operator* op, BuiltinOperator op_type,
949954
case BuiltinOperator_STABLEHLO_CONVERT:
950955
case BuiltinOperator_STABLEHLO_PAD:
951956
case BuiltinOperator_STABLEHLO_DOT_GENERAL:
952-
case BuiltinOperator_STABLEHLO_REDUCE_WINDOW:
953957
case BuiltinOperator_STABLEHLO_SORT:
954958
case BuiltinOperator_STABLEHLO_WHILE:
955959
case BuiltinOperator_STABLEHLO_TRANSPOSE:
@@ -2096,6 +2100,88 @@ TfLiteStatus ParseResizeNearestNeighbor(const Operator* op,
20962100
return kTfLiteOk;
20972101
}
20982102

2103+
TfLiteStatus ParseStablehloReduceWindow(const Operator* op,
2104+
ErrorReporter* error_reporter,
2105+
BuiltinDataAllocator* allocator,
2106+
void** builtin_data) {
2107+
CheckParsePointerParams(op, error_reporter, allocator, builtin_data);
2108+
2109+
SafeBuiltinDataAllocator safe_allocator(allocator);
2110+
auto params = safe_allocator.Allocate<TfLiteStablehloReduceWindowParams>();
2111+
2112+
const StablehloReduceWindowOptions* schema_params =
2113+
op->builtin_options_2_as_StablehloReduceWindowOptions();
2114+
if (schema_params) {
2115+
if (!schema_params->window_dimensions() ||
2116+
schema_params->window_dimensions()->size() == 0) {
2117+
TF_LITE_REPORT_ERROR(error_reporter,
2118+
"'window_dimensions' attribute is not optional for "
2119+
"'stablehlo.reduce_window' and cannot be empty.");
2120+
return kTfLiteError;
2121+
}
2122+
2123+
const size_t rank = schema_params->window_dimensions()->size();
2124+
2125+
auto LoadAttr = [&error_reporter](
2126+
int64_t* params_array, size_t params_array_size_bytes,
2127+
const flatbuffers::Vector<int64_t>* flatbuffer_vector,
2128+
const char* attr_name, const size_t expected_size,
2129+
const int64_t fill_value) -> TfLiteStatus {
2130+
if (flatbuffer_vector && flatbuffer_vector->size()) {
2131+
if (expected_size != 0 && flatbuffer_vector->size() != expected_size) {
2132+
TF_LITE_REPORT_ERROR(
2133+
error_reporter,
2134+
"'%s' attribute of 'stablehlo.reduce_window' does not have the "
2135+
"expected size (%llu != %llu).",
2136+
attr_name, flatbuffer_vector->size(), expected_size);
2137+
return kTfLiteError;
2138+
}
2139+
TfLiteStatus status = FlatBufferIntVectorToArray(
2140+
params_array_size_bytes, flatbuffer_vector, params_array,
2141+
error_reporter, "stablehlo.reduce_window");
2142+
if (status != kTfLiteOk) {
2143+
TF_LITE_REPORT_ERROR(error_reporter, "Check the '%s' attribute.",
2144+
attr_name);
2145+
return status;
2146+
}
2147+
} else {
2148+
std::fill_n(params_array, params_array_size_bytes / sizeof(int64_t),
2149+
fill_value);
2150+
}
2151+
return kTfLiteOk;
2152+
};
2153+
2154+
TF_LITE_ENSURE_STATUS(
2155+
LoadAttr(params->window_dimensions, sizeof(params->window_dimensions),
2156+
schema_params->window_dimensions(), "window_dimensions",
2157+
/*expected_size=*/rank, /*fill_value=*/1));
2158+
TF_LITE_ENSURE_STATUS(
2159+
LoadAttr(params->window_strides, sizeof(params->window_strides),
2160+
schema_params->window_strides(), "window_strides",
2161+
/*expected_size=*/rank, /*fill_value=*/1));
2162+
TF_LITE_ENSURE_STATUS(
2163+
LoadAttr(params->base_dilations, sizeof(params->base_dilations),
2164+
schema_params->base_dilations(), "base_dilations",
2165+
/*expected_size=*/rank, /*fill_value=*/1));
2166+
TF_LITE_ENSURE_STATUS(
2167+
LoadAttr(params->window_dilations, sizeof(params->window_dilations),
2168+
schema_params->window_dilations(), "window_dilations",
2169+
/*expected_size=*/rank, /*fill_value=*/1));
2170+
TF_LITE_ENSURE_STATUS(LoadAttr(params->padding, sizeof(params->padding),
2171+
schema_params->padding(), "padding",
2172+
/*expected_size=*/2 * rank,
2173+
/*fill_value=*/0));
2174+
2175+
params->body_subgraph_index = schema_params->body_subgraph_index();
2176+
*builtin_data = params.release();
2177+
return kTfLiteOk;
2178+
}
2179+
TF_LITE_REPORT_ERROR(
2180+
error_reporter,
2181+
"Could not get 'stablehlo.reduce_window' operation parameters.");
2182+
return kTfLiteError;
2183+
}
2184+
20992185
TfLiteStatus ParseStablehloScatter(const Operator* op,
21002186
ErrorReporter* error_reporter,
21012187
BuiltinDataAllocator* allocator,

‎tensorflow/lite/core/api/flatbuffer_conversions.h

+5
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,11 @@ TfLiteStatus ParseStablehloGather(const Operator* op,
435435
BuiltinDataAllocator* allocator,
436436
void** builtin_data);
437437

438+
TfLiteStatus ParseStablehloReduceWindow(const Operator* op,
439+
ErrorReporter* error_reporter,
440+
BuiltinDataAllocator* allocator,
441+
void** builtin_data);
442+
438443
} // namespace tflite
439444

440445
#endif // TENSORFLOW_LITE_CORE_API_FLATBUFFER_CONVERSIONS_H_

‎tensorflow/lite/core/c/builtin_op_data.h

+17
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ extern "C" {
3434
#define TFLITE_RESHAPE_PARAMS_MAX_DIMENSION_COUNT 8
3535
#define TFLITE_STABLEHLO_SCATTER_PARAMS_MAX_DIMENSION_COUNT 8
3636
#define TFLITE_STABLEHLO_GATHER_PARAMS_MAX_DIMENSION_COUNT 8
37+
#define TFLITE_STABLEHLO_REDUCE_WINDOW_PARAMS_MAX_DIMENSION_COUNT 8
3738

3839
// TODO(aselle): Consider using "if this then that" for testing.
3940

@@ -605,6 +606,22 @@ typedef struct {
605606
bool indices_are_sorted;
606607
} TfLiteStablehloGatherParams;
607608

609+
typedef struct {
610+
// See the stablehlo spec for the explanation of the attributes:
611+
// https://github.com/openxla/stablehlo/blob/main/docs/spec.md#reduce_window
612+
int64_t window_dimensions
613+
[TFLITE_STABLEHLO_REDUCE_WINDOW_PARAMS_MAX_DIMENSION_COUNT];
614+
int64_t
615+
window_strides[TFLITE_STABLEHLO_REDUCE_WINDOW_PARAMS_MAX_DIMENSION_COUNT];
616+
int64_t
617+
base_dilations[TFLITE_STABLEHLO_REDUCE_WINDOW_PARAMS_MAX_DIMENSION_COUNT];
618+
int64_t window_dilations
619+
[TFLITE_STABLEHLO_REDUCE_WINDOW_PARAMS_MAX_DIMENSION_COUNT];
620+
int64_t
621+
padding[2 * TFLITE_STABLEHLO_REDUCE_WINDOW_PARAMS_MAX_DIMENSION_COUNT];
622+
int body_subgraph_index;
623+
} TfLiteStablehloReduceWindowParams;
624+
608625
enum TfLiteReduceWindowFunction {
609626
TfLiteReduceWindowFunctionUnsupported,
610627
TfLiteReduceWindowFunctionAdd,

‎tensorflow/lite/core/c/c_api_types.h

+10-5
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ limitations under the License.
3434
extern "C" {
3535
#endif
3636

37-
/** \addtogroup c_api_types tensorflow/lite/c/c_api_types.h
37+
// clang-format off
38+
// NOLINTBEGIN(whitespace/line_length)
39+
/** \defgroup c_api_types tensorflow/lite/c/c_api_types.h
3840
* @{
3941
*/
42+
// NOLINTEND(whitespace/line_length)
43+
// clang-format on
4044

4145
// Define TFL_CAPI_EXPORT macro to export a function properly with a shared
4246
// library.
@@ -123,12 +127,11 @@ typedef enum {
123127
kTfLiteInt4 = 18,
124128
} TfLiteType;
125129

126-
/// Legacy. Will be deprecated in favor of TfLiteAffineQuantization.
130+
/// Legacy. Will be deprecated in favor of `TfLiteAffineQuantization`.
127131
/// If per-layer quantization is specified this field will still be populated in
128-
/// addition to TfLiteAffineQuantization.
132+
/// addition to `TfLiteAffineQuantization`.
129133
/// Parameters for asymmetric quantization. Quantized values can be converted
130-
/// back to float using:
131-
/// real_value = scale * (quantized_value - zero_point)
134+
/// back to float using: `real_value = scale * (quantized_value - zero_point)`
132135
typedef struct TfLiteQuantizationParams {
133136
float scale;
134137
int32_t zero_point;
@@ -156,13 +159,15 @@ typedef struct TfLiteDelegate TfLiteDelegate;
156159
/// This is an abstract type that is intended to have the same
157160
/// role as TfLiteDelegate, but without exposing the implementation
158161
/// details of how delegates are implemented.
162+
///
159163
/// WARNING: This is an experimental type and subject to change.
160164
typedef struct TfLiteOpaqueDelegateStruct TfLiteOpaqueDelegateStruct;
161165

162166
/// TfLiteOpaqueDelegate: conditionally opaque version of
163167
/// TfLiteDelegate; allows delegation of nodes to alternative backends.
164168
/// For TF Lite in Play Services, this is an opaque type,
165169
/// but for regular TF Lite, this is just a typedef for TfLiteDelegate.
170+
///
166171
/// WARNING: This is an experimental type and subject to change.
167172
#if TFLITE_WITH_STABLE_ABI || TFLITE_USE_OPAQUE_DELEGATE
168173
typedef TfLiteOpaqueDelegateStruct TfLiteOpaqueDelegate;

0 commit comments

Comments
 (0)
Please sign in to comment.