| // Copyright 2020 Google LLC |
| // |
| // This source code is licensed under the BSD-style license found in the |
| // LICENSE file in the root directory of this source tree. |
| |
| #include <math.h> |
| #include <stddef.h> |
| #include <stdint.h> |
| |
| #include <xnnpack.h> |
| #include <xnnpack/log.h> |
| #include <xnnpack/params.h> |
| #include <xnnpack/subgraph.h> |
| |
| |
| enum xnn_status xnn_define_max_pooling_2d( |
| xnn_subgraph_t subgraph, |
| uint32_t input_padding_top, |
| uint32_t input_padding_right, |
| uint32_t input_padding_bottom, |
| uint32_t input_padding_left, |
| uint32_t pooling_height, |
| uint32_t pooling_width, |
| uint32_t stride_height, |
| uint32_t stride_width, |
| uint32_t dilation_height, |
| uint32_t dilation_width, |
| float output_min, |
| float output_max, |
| uint32_t input_id, |
| uint32_t output_id, |
| uint32_t flags) |
| { |
| if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) { |
| xnn_log_error("failed to define %s operator: XNNPACK is not initialized", |
| xnn_node_type_to_string(xnn_node_type_max_pooling_2d)); |
| return xnn_status_uninitialized; |
| } |
| |
| const uint32_t pooling_size = pooling_height * pooling_width; |
| if (pooling_size == 0) { |
| xnn_log_error( |
| "failed to define %s operator with %" PRIu32 "x%" PRIu32 " pooling size: " |
| "pooling size dimensions must be non-zero", |
| xnn_node_type_to_string(xnn_node_type_max_pooling_2d), pooling_width, pooling_height); |
| return xnn_status_invalid_parameter; |
| } |
| |
| if (pooling_size == 1) { |
| xnn_log_error( |
| "failed to define %s operator with 1 pooling element: 1x1 pooling is meaningless", |
| xnn_node_type_to_string(xnn_node_type_max_pooling_2d)); |
| return xnn_status_invalid_parameter; |
| } |
| |
| if (stride_height == 0 || stride_width == 0) { |
| xnn_log_error( |
| "failed to define %s operator with %" PRIu32 "x%" PRIu32 " stride: stride dimensions must be non-zero", |
| xnn_node_type_to_string(xnn_node_type_max_pooling_2d), stride_width, stride_height); |
| return xnn_status_invalid_parameter; |
| } |
| |
| if (dilation_height == 0 || dilation_width == 0) { |
| xnn_log_error( |
| "failed to define %s operator with %" PRIu32 "x%" PRIu32 " dilation: dilation dimensions must be non-zero", |
| xnn_node_type_to_string(xnn_node_type_max_pooling_2d), dilation_width, dilation_height); |
| return xnn_status_invalid_parameter; |
| } |
| |
| if (isnan(output_min)) { |
| xnn_log_error( |
| "failed to define %s with NaN output lower bound: lower bound must be non-NaN", |
| xnn_node_type_to_string(xnn_node_type_max_pooling_2d)); |
| return xnn_status_invalid_parameter; |
| } |
| |
| if (isnan(output_max)) { |
| xnn_log_error( |
| "failed to define %s with NaN output upper bound: upper bound must be non-NaN", |
| xnn_node_type_to_string(xnn_node_type_max_pooling_2d)); |
| return xnn_status_invalid_parameter; |
| } |
| |
| if (output_min >= output_max) { |
| xnn_log_error( |
| "failed to define %s with [%.7g, %.7g] output range: lower bound must be below upper bound", |
| xnn_node_type_to_string(xnn_node_type_max_pooling_2d), output_min, output_max); |
| return xnn_status_invalid_parameter; |
| } |
| |
| const bool any_padding = (input_padding_left | input_padding_top | input_padding_right | input_padding_bottom) != 0; |
| if ((flags & XNN_FLAG_TENSORFLOW_SAME_PADDING) != 0) { |
| if (any_padding) { |
| xnn_log_error( |
| "failed to define %s operator with %" PRIu32 "+%" PRIu32 "x%" PRIu32 "+%" PRIu32" padding: " |
| "TensorFlow SAME padding can't be combined with explicit padding specification", |
| xnn_node_type_to_string(xnn_node_type_max_pooling_2d), |
| input_padding_top, input_padding_left, input_padding_bottom, input_padding_right); |
| return xnn_status_invalid_parameter; |
| } |
| } |
| |
| if (input_id >= subgraph->num_values) { |
| xnn_log_error( |
| "failed to define %s operator with input ID #%" PRIu32 ": invalid Value ID", |
| xnn_node_type_to_string(xnn_node_type_max_pooling_2d), input_id); |
| return xnn_status_invalid_parameter; |
| } |
| |
| const struct xnn_value* input_value = &subgraph->values[input_id]; |
| if (input_value->type != xnn_value_type_dense_tensor) { |
| xnn_log_error( |
| "failed to define %s operator with input ID #%" PRIu32 ": unsupported Value type %d (expected dense tensor)", |
| xnn_node_type_to_string(xnn_node_type_max_pooling_2d), input_id, input_value->type); |
| return xnn_status_invalid_parameter; |
| } |
| |
| switch (input_value->datatype) { |
| case xnn_datatype_fp32: |
| #ifndef XNN_NO_S8_OPERATORS |
| case xnn_datatype_qint8: |
| #endif // !defined(XNN_NO_S8_OPERATORS) |
| #ifndef XNN_NO_U8_OPERATORS |
| case xnn_datatype_quint8: |
| #endif // !defined(XNN_NO_U8_OPERATORS) |
| break; |
| default: |
| xnn_log_error( |
| "failed to define %s operator with input ID #%" PRIu32 ": unsupported Value datatype %s (%d)", |
| xnn_node_type_to_string(xnn_node_type_max_pooling_2d), input_id, |
| xnn_datatype_to_string(input_value->datatype), input_value->datatype); |
| return xnn_status_invalid_parameter; |
| } |
| |
| if (output_id >= subgraph->num_values) { |
| xnn_log_error( |
| "failed to define %s operator with output ID #%" PRIu32 ": invalid Value ID", |
| xnn_node_type_to_string(xnn_node_type_max_pooling_2d), output_id); |
| return xnn_status_invalid_parameter; |
| } |
| |
| const struct xnn_value* output_value = &subgraph->values[output_id]; |
| if (output_value->type != xnn_value_type_dense_tensor) { |
| xnn_log_error( |
| "failed to define %s operator with output ID #%" PRIu32 ": unsupported Value type %d (expected dense tensor)", |
| xnn_node_type_to_string(xnn_node_type_max_pooling_2d), output_id, output_value->type); |
| return xnn_status_invalid_parameter; |
| } |
| |
| switch (output_value->datatype) { |
| case xnn_datatype_fp32: |
| #ifndef XNN_NO_S8_OPERATORS |
| case xnn_datatype_qint8: |
| #endif // !defined(XNN_NO_S8_OPERATORS) |
| #ifndef XNN_NO_U8_OPERATORS |
| case xnn_datatype_quint8: |
| #endif // !defined(XNN_NO_U8_OPERATORS) |
| break; |
| default: |
| xnn_log_error( |
| "failed to define %s operator with output ID #%" PRIu32 ": unsupported Value datatype %s (%d)", |
| xnn_node_type_to_string(xnn_node_type_max_pooling_2d), output_id, |
| xnn_datatype_to_string(output_value->datatype), output_value->datatype); |
| return xnn_status_invalid_parameter; |
| } |
| |
| if (input_value->datatype != output_value->datatype) { |
| xnn_log_error( |
| "failed to define %s operator with input ID #%" PRIu32 " and output ID #%" PRIu32 |
| ": mismatching datatypes across input (%s) and output (%s)", |
| xnn_node_type_to_string(xnn_node_type_max_pooling_2d), input_id, output_id, |
| xnn_datatype_to_string(input_value->datatype), |
| xnn_datatype_to_string(output_value->datatype)); |
| return xnn_status_invalid_parameter; |
| } |
| |
| #if !defined(XNN_NO_S8_OPERATORS) || !defined(XNN_NO_U8_OPERATORS) |
| if (output_value->datatype == xnn_datatype_qint8 || output_value->datatype == xnn_datatype_quint8) { |
| if (input_value->quantization.zero_point != output_value->quantization.zero_point) { |
| xnn_log_error( |
| "failed to define %s operator with input ID #%" PRIu32 " and output ID #%" PRIu32 |
| ": mismatching zero point quantization parameter across input (%"PRId32") and output (%"PRId32")", |
| xnn_node_type_to_string(xnn_node_type_max_pooling_2d), input_id, output_id, |
| input_value->quantization.zero_point, output_value->quantization.zero_point); |
| return xnn_status_invalid_parameter; |
| } |
| if (input_value->quantization.scale != output_value->quantization.scale) { |
| xnn_log_error( |
| "failed to define %s operator with input ID #%" PRIu32 " and output ID #%" PRIu32 |
| ": mismatching zero point quantization parameter across input (%.7g) and output (%.7g)", |
| xnn_node_type_to_string(xnn_node_type_max_pooling_2d), input_id, output_id, |
| input_value->quantization.scale, output_value->quantization.scale); |
| return xnn_status_invalid_parameter; |
| } |
| } |
| #endif // !defined(XNN_NO_S8_OPERATORS) || !defined(XNN_NO_U8_OPERATORS) |
| |
| struct xnn_node* node = xnn_subgraph_new_node(subgraph); |
| if (node == NULL) { |
| return xnn_status_out_of_memory; |
| } |
| |
| node->type = xnn_node_type_max_pooling_2d; |
| node->params.pooling_2d.padding_top = input_padding_top; |
| node->params.pooling_2d.padding_right = input_padding_right; |
| node->params.pooling_2d.padding_bottom = input_padding_bottom; |
| node->params.pooling_2d.padding_left = input_padding_left; |
| node->params.pooling_2d.pooling_height = pooling_height; |
| node->params.pooling_2d.pooling_width = pooling_width; |
| node->params.pooling_2d.stride_height = stride_height; |
| node->params.pooling_2d.stride_width = stride_width; |
| node->params.pooling_2d.dilation_height = dilation_height; |
| node->params.pooling_2d.dilation_width = dilation_width; |
| node->activation.output_min = output_min; |
| node->activation.output_max = output_max; |
| node->num_inputs = 1; |
| node->inputs[0] = input_id; |
| node->num_outputs = 1; |
| node->outputs[0] = output_id; |
| node->flags = flags; |
| |
| return xnn_status_success; |
| } |