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 <xnnpack.h>
12 #include <xnnpack/log.h>
13 #include <xnnpack/params.h>
14 #include <xnnpack/subgraph.h>
15
16
xnn_define_static_reshape(xnn_subgraph_t subgraph,size_t num_dims,const size_t * new_shape,uint32_t input_id,uint32_t output_id,uint32_t flags)17 enum xnn_status xnn_define_static_reshape(
18 xnn_subgraph_t subgraph,
19 size_t num_dims,
20 const size_t* new_shape,
21 uint32_t input_id,
22 uint32_t output_id,
23 uint32_t flags)
24 {
25 if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) {
26 xnn_log_error("failed to define %s operator: XNNPACK is not initialized",
27 xnn_node_type_to_string(xnn_node_type_static_reshape));
28 return xnn_status_uninitialized;
29 }
30
31 if (input_id >= subgraph->num_values) {
32 xnn_log_error(
33 "failed to define %s operator with input ID #%" PRIu32 ": invalid Value ID",
34 xnn_node_type_to_string(xnn_node_type_static_reshape), input_id);
35 return xnn_status_invalid_parameter;
36 }
37
38 if (output_id >= subgraph->num_values) {
39 xnn_log_error(
40 "failed to define %s operator with output ID #%" PRIu32 ": invalid Value ID",
41 xnn_node_type_to_string(xnn_node_type_static_reshape), output_id);
42 return xnn_status_invalid_parameter;
43 }
44
45 if (num_dims > XNN_MAX_TENSOR_DIMS) {
46 xnn_log_error(
47 "failed to define %s operator with %zu-dimensional output shape: at most %zu dimensions are supported",
48 xnn_node_type_to_string(xnn_node_type_static_reshape), num_dims, (size_t) XNN_MAX_TENSOR_DIMS);
49 return xnn_status_unsupported_parameter;
50 }
51
52 struct xnn_node* node = xnn_subgraph_new_node(subgraph);
53 if (node == NULL) {
54 return xnn_status_out_of_memory;
55 }
56
57 node->params.static_reshape.new_shape.num_dims = num_dims;
58 memcpy(&node->params.static_reshape.new_shape.dim, new_shape, num_dims * sizeof(size_t));
59
60 node->type = xnn_node_type_static_reshape;
61 node->num_inputs = 1;
62 node->inputs[0] = input_id;
63 node->num_outputs = 1;
64 node->outputs[0] = output_id;
65 node->flags = flags;
66
67 return xnn_status_success;
68 }
69