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
10 #include <xnnpack.h>
11 #include <xnnpack/log.h>
12 #include <xnnpack/params.h>
13 #include <xnnpack/subgraph.h>
14
15
xnn_define_static_resize_bilinear_2d(xnn_subgraph_t subgraph,size_t new_height,size_t new_width,uint32_t input_id,uint32_t output_id,uint32_t flags)16 enum xnn_status xnn_define_static_resize_bilinear_2d(
17 xnn_subgraph_t subgraph,
18 size_t new_height,
19 size_t new_width,
20 uint32_t input_id,
21 uint32_t output_id,
22 uint32_t flags)
23 {
24 if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) {
25 xnn_log_error("failed to define %s operator: XNNPACK is not initialized",
26 xnn_node_type_to_string(xnn_node_type_static_resize_bilinear_2d));
27 return xnn_status_uninitialized;
28 }
29
30 if (new_width == 0 || new_height == 0) {
31 xnn_log_error(
32 "failed to define %s operator with %zux%zu output: output dimensions must be non-zero",
33 xnn_node_type_to_string(xnn_node_type_static_resize_bilinear_2d), new_width, new_height);
34 return xnn_status_invalid_parameter;
35 }
36
37 if (max(new_width, new_height) >= 16777216) {
38 xnn_log_error(
39 "failed to define %s operator with %zux%zu output: output dimensions must be below 2**24",
40 xnn_node_type_to_string(xnn_node_type_static_resize_bilinear_2d), new_width, new_height);
41 return xnn_status_unsupported_parameter;
42 }
43
44 const uint32_t supported_flags = XNN_FLAG_TENSORFLOW_LEGACY_MODE | XNN_FLAG_ALIGN_CORNERS;
45 const uint32_t invalid_flags = flags & ~supported_flags;
46 if (invalid_flags != 0) {
47 xnn_log_error(
48 "failed to define %s operator with 0x%08" PRIx32 " flags: invalid flags 0x%08" PRIx32,
49 xnn_node_type_to_string(xnn_node_type_static_resize_bilinear_2d), flags, invalid_flags);
50 return xnn_status_invalid_parameter;
51 }
52
53 const uint32_t exclusive_flags = XNN_FLAG_TENSORFLOW_LEGACY_MODE | XNN_FLAG_ALIGN_CORNERS;
54 if ((flags & exclusive_flags) == exclusive_flags) {
55 xnn_log_error(
56 "failed to define %s operator with both XNN_FLAG_TENSORFLOW_LEGACY_MODE and XNN_FLAG_ALIGN_CORNERS flags: "
57 "the two flags are mutually exclusive",
58 xnn_node_type_to_string(xnn_node_type_static_resize_bilinear_2d));
59 return xnn_status_invalid_parameter;
60 }
61
62 if (input_id >= subgraph->num_values) {
63 xnn_log_error(
64 "failed to define %s operator with input ID #%" PRIu32 ": invalid Value ID",
65 xnn_node_type_to_string(xnn_node_type_static_resize_bilinear_2d), input_id);
66 return xnn_status_invalid_parameter;
67 }
68
69 if (output_id >= subgraph->num_values) {
70 xnn_log_error(
71 "failed to define %s operator with output ID #%" PRIu32 ": invalid Value ID",
72 xnn_node_type_to_string(xnn_node_type_static_resize_bilinear_2d), output_id);
73 return xnn_status_invalid_parameter;
74 }
75
76 struct xnn_node* node = xnn_subgraph_new_node(subgraph);
77 if (node == NULL) {
78 return xnn_status_out_of_memory;
79 }
80
81 node->params.static_resize.new_height = new_height;
82 node->params.static_resize.new_width = new_width;
83
84 node->type = xnn_node_type_static_resize_bilinear_2d;
85 node->num_inputs = 1;
86 node->inputs[0] = input_id;
87 node->num_outputs = 1;
88 node->outputs[0] = output_id;
89 node->flags = flags;
90
91 return xnn_status_success;
92 }
93