• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stddef.h>
7 #include <stdint.h>
8 
9 #include <xnnpack.h>
10 #include <xnnpack/allocator.h>
11 #include <xnnpack/operator.h>
12 #include <xnnpack/log.h>
13 #include <xnnpack/params.h>
14 
15 
xnn_create_depth_to_space_nchw2nhwc_x32(size_t output_channels,size_t input_channel_stride,size_t output_channel_stride,uint32_t block_size,uint32_t flags,xnn_operator_t * depth_to_space_op_out)16 enum xnn_status xnn_create_depth_to_space_nchw2nhwc_x32(
17     size_t output_channels,
18     size_t input_channel_stride,
19     size_t output_channel_stride,
20     uint32_t block_size,
21     uint32_t flags,
22     xnn_operator_t* depth_to_space_op_out)
23 {
24   xnn_operator_t depth_to_space_op = NULL;
25   enum xnn_status status = xnn_status_uninitialized;
26 
27   if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) {
28     xnn_log_error("failed to create %s operator: XNNPACK is not initialized",
29       xnn_operator_type_to_string(xnn_operator_type_depth_to_space_nchw2nhwc_x32));
30     goto error;
31   }
32 
33   status = xnn_status_invalid_parameter;
34 
35   if (output_channels == 0) {
36     xnn_log_error("failed to create %s operator with %zu output channels: number of channels must be non-zero",
37       xnn_operator_type_to_string(xnn_operator_type_depth_to_space_nchw2nhwc_x32), output_channels);
38     goto error;
39   }
40 
41   if (output_channel_stride < output_channels) {
42     xnn_log_error(
43       "failed to create %s operator with output channel stride of %zu: "
44       "stride must be at least as large as the number of output channels (%zu)",
45       xnn_operator_type_to_string(xnn_operator_type_depth_to_space_nchw2nhwc_x32),
46       output_channel_stride, output_channels);
47     goto error;
48   }
49 
50   if (block_size <= 1) {
51     xnn_log_error("failed to create %s operator with %" PRIu32 " block size: block size must be greater than 1",
52       xnn_operator_type_to_string(xnn_operator_type_depth_to_space_nchw2nhwc_x32),
53       block_size);
54     goto error;
55   }
56 
57   const size_t input_channels = output_channels * block_size * block_size;
58   if (input_channel_stride < input_channels) {
59     xnn_log_error(
60       "failed to create %s operator with input channel stride of %zu: "
61       "stride must be at least as large as the number of input channels (%" PRIu32 "x%" PRIu32 "x%zu)",
62       xnn_operator_type_to_string(xnn_operator_type_depth_to_space_nchw2nhwc_x32),
63       input_channel_stride, block_size, block_size, input_channels);
64     goto error;
65   }
66 
67   status = xnn_status_out_of_memory;
68 
69   depth_to_space_op = xnn_allocate_zero_simd_memory(sizeof(struct xnn_operator));
70   if (depth_to_space_op == NULL) {
71     xnn_log_error(
72       "failed to allocate %zu bytes for %s operator descriptor",
73       sizeof(struct xnn_operator), xnn_operator_type_to_string(xnn_operator_type_depth_to_space_nchw2nhwc_x32));
74     goto error;
75   }
76 
77   depth_to_space_op->channels = output_channels;
78   depth_to_space_op->input_pixel_stride = input_channel_stride;
79   depth_to_space_op->output_pixel_stride = output_channel_stride;
80   depth_to_space_op->block_size = block_size;
81 
82   depth_to_space_op->type = xnn_operator_type_depth_to_space_nchw2nhwc_x32;
83   depth_to_space_op->flags = flags;
84 
85   depth_to_space_op->state = xnn_run_state_invalid;
86 
87   *depth_to_space_op_out = depth_to_space_op;
88   return xnn_status_success;
89 
90 error:
91   xnn_delete_operator(depth_to_space_op);
92   return status;
93 }
94 
xnn_setup_depth_to_space_nchw2nhwc_x32(xnn_operator_t depth_to_space_op,size_t batch_size,size_t input_height,size_t input_width,const void * input,void * output,pthreadpool_t threadpool)95 enum xnn_status xnn_setup_depth_to_space_nchw2nhwc_x32(
96     xnn_operator_t depth_to_space_op,
97     size_t batch_size,
98     size_t input_height,
99     size_t input_width,
100     const void* input,
101     void* output,
102     pthreadpool_t threadpool)
103 {
104   if (depth_to_space_op->type != xnn_operator_type_depth_to_space_nchw2nhwc_x32) {
105     xnn_log_error("failed to setup operator: operator type mismatch (expected %s, got %s)",
106       xnn_operator_type_to_string(xnn_operator_type_depth_to_space_nchw2nhwc_x32),
107       xnn_operator_type_to_string(depth_to_space_op->type));
108     return xnn_status_invalid_parameter;
109   }
110   depth_to_space_op->state = xnn_run_state_invalid;
111 
112   if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) {
113     xnn_log_error("failed to setup %s operator: XNNPACK is not initialized",
114       xnn_operator_type_to_string(xnn_operator_type_depth_to_space_nchw2nhwc_x32));
115     return xnn_status_uninitialized;
116   }
117 
118   if (input_width == 0 || input_height == 0) {
119     xnn_log_error("failed to setup %s operator with %zux%zu input: input dimensions must be non-zero",
120       xnn_operator_type_to_string(xnn_operator_type_depth_to_space_nchw2nhwc_x32), input_width, input_height);
121     return xnn_status_invalid_parameter;
122   }
123 
124   if (batch_size == 0) {
125     depth_to_space_op->state = xnn_run_state_skip;
126     return xnn_status_success;
127   }
128 
129   const uint32_t block_size = depth_to_space_op->block_size;
130   const size_t output_height = input_height * block_size;
131   const size_t output_width = input_width * block_size;
132   depth_to_space_op->context.depthtospace2d_chw = (struct depthtospace2d_chw2hwc_context) {
133     .output_channels = depth_to_space_op->channels,
134     .input_height = input_height,
135     .input_width = input_width,
136     .block_size = block_size,
137     .input = input,
138     .output = output,
139     .input_batch_stride = depth_to_space_op->input_pixel_stride * input_height * input_width * sizeof(float),
140     .output_batch_stride = depth_to_space_op->output_pixel_stride * output_height * output_width * sizeof(float),
141     .output_channel_stride = depth_to_space_op->output_pixel_stride,
142     .ukernel = xnn_params.x32.depthtospace2d_chw2hwc.ukernel,
143   };
144 
145   depth_to_space_op->compute.type = xnn_parallelization_type_1d;
146   depth_to_space_op->compute.task_1d = (pthreadpool_task_1d_t) xnn_compute_depthtospace2d_chw2hwc;
147   depth_to_space_op->compute.range[0] = batch_size;
148   depth_to_space_op->state = xnn_run_state_ready;
149 
150   return xnn_status_success;
151 }
152