Marat Dukhan | 95e8b7a | 2020-06-03 12:46:26 -0700 | [diff] [blame] | 1 | // Copyright 2020 Google LLC |
| 2 | // |
| 3 | // This source code is licensed under the BSD-style license found in the |
| 4 | // LICENSE file in the root directory of this source tree. |
| 5 | |
| 6 | #include <math.h> |
| 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | #include <string.h> |
| 10 | |
| 11 | #include <fp16.h> |
| 12 | |
| 13 | #include <xnnpack.h> |
| 14 | #include <xnnpack/log.h> |
| 15 | #include <xnnpack/params.h> |
| 16 | #include <xnnpack/subgraph.h> |
| 17 | |
| 18 | |
| 19 | enum xnn_status xnn_define_static_constant_pad( |
| 20 | xnn_subgraph_t subgraph, |
| 21 | const size_t* pre_paddings, |
| 22 | const size_t* post_paddings, |
| 23 | float padding_value, |
| 24 | uint32_t input_id, |
| 25 | uint32_t output_id, |
| 26 | uint32_t flags) |
| 27 | { |
Marat Dukhan | 854fb6b | 2020-06-19 12:33:44 -0700 | [diff] [blame] | 28 | if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) { |
Marat Dukhan | 3b59de2 | 2020-06-03 20:15:19 -0700 | [diff] [blame] | 29 | xnn_log_error("failed to define %s operator: XNNPACK is not initialized", |
Marat Dukhan | aff24e2 | 2020-07-23 01:43:58 -0700 | [diff] [blame] | 30 | xnn_node_type_to_string(xnn_node_type_static_constant_pad)); |
Marat Dukhan | 95e8b7a | 2020-06-03 12:46:26 -0700 | [diff] [blame] | 31 | return xnn_status_uninitialized; |
| 32 | } |
| 33 | |
| 34 | if (input_id >= subgraph->num_values) { |
| 35 | xnn_log_error( |
Marat Dukhan | 3b59de2 | 2020-06-03 20:15:19 -0700 | [diff] [blame] | 36 | "failed to define %s operator with input ID #%" PRIu32 ": invalid Value ID", |
Marat Dukhan | aff24e2 | 2020-07-23 01:43:58 -0700 | [diff] [blame] | 37 | xnn_node_type_to_string(xnn_node_type_static_constant_pad), input_id); |
Marat Dukhan | 95e8b7a | 2020-06-03 12:46:26 -0700 | [diff] [blame] | 38 | return xnn_status_invalid_parameter; |
| 39 | } |
| 40 | |
| 41 | if (output_id >= subgraph->num_values) { |
| 42 | xnn_log_error( |
Marat Dukhan | 3b59de2 | 2020-06-03 20:15:19 -0700 | [diff] [blame] | 43 | "failed to define %s operator with output ID #%" PRIu32 ": invalid Value ID", |
Marat Dukhan | aff24e2 | 2020-07-23 01:43:58 -0700 | [diff] [blame] | 44 | xnn_node_type_to_string(xnn_node_type_static_constant_pad), output_id); |
Marat Dukhan | 95e8b7a | 2020-06-03 12:46:26 -0700 | [diff] [blame] | 45 | return xnn_status_invalid_parameter; |
| 46 | } |
| 47 | |
| 48 | struct xnn_node* node = xnn_subgraph_new_node(subgraph); |
| 49 | if (node == NULL) { |
| 50 | return xnn_status_out_of_memory; |
| 51 | } |
| 52 | |
| 53 | const size_t num_dims = subgraph->values[input_id].shape.num_dims; |
| 54 | memcpy(&node->params.static_pad.pre_paddings, pre_paddings, num_dims * sizeof(size_t)); |
| 55 | memcpy(&node->params.static_pad.post_paddings, post_paddings, num_dims * sizeof(size_t)); |
| 56 | node->params.static_pad.padding_value = fp32_to_bits(padding_value); |
| 57 | |
Marat Dukhan | aff24e2 | 2020-07-23 01:43:58 -0700 | [diff] [blame] | 58 | node->type = xnn_node_type_static_constant_pad; |
Marat Dukhan | 95e8b7a | 2020-06-03 12:46:26 -0700 | [diff] [blame] | 59 | node->num_inputs = 1; |
| 60 | node->inputs[0] = input_id; |
| 61 | node->num_outputs = 1; |
| 62 | node->outputs[0] = output_id; |
| 63 | node->flags = flags; |
| 64 | |
| 65 | return xnn_status_success; |
| 66 | } |