blob: 70183c7bd1cbd420645928f4875034547e2a66b2 [file] [log] [blame]
Marat Dukhan95e8b7a2020-06-03 12:46:26 -07001// 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
19enum 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 Dukhan854fb6b2020-06-19 12:33:44 -070028 if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) {
Marat Dukhan3b59de22020-06-03 20:15:19 -070029 xnn_log_error("failed to define %s operator: XNNPACK is not initialized",
Marat Dukhanaff24e22020-07-23 01:43:58 -070030 xnn_node_type_to_string(xnn_node_type_static_constant_pad));
Marat Dukhan95e8b7a2020-06-03 12:46:26 -070031 return xnn_status_uninitialized;
32 }
33
34 if (input_id >= subgraph->num_values) {
35 xnn_log_error(
Marat Dukhan3b59de22020-06-03 20:15:19 -070036 "failed to define %s operator with input ID #%" PRIu32 ": invalid Value ID",
Marat Dukhanaff24e22020-07-23 01:43:58 -070037 xnn_node_type_to_string(xnn_node_type_static_constant_pad), input_id);
Marat Dukhan95e8b7a2020-06-03 12:46:26 -070038 return xnn_status_invalid_parameter;
39 }
40
41 if (output_id >= subgraph->num_values) {
42 xnn_log_error(
Marat Dukhan3b59de22020-06-03 20:15:19 -070043 "failed to define %s operator with output ID #%" PRIu32 ": invalid Value ID",
Marat Dukhanaff24e22020-07-23 01:43:58 -070044 xnn_node_type_to_string(xnn_node_type_static_constant_pad), output_id);
Marat Dukhan95e8b7a2020-06-03 12:46:26 -070045 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 Dukhanaff24e22020-07-23 01:43:58 -070058 node->type = xnn_node_type_static_constant_pad;
Marat Dukhan95e8b7a2020-06-03 12:46:26 -070059 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}