diff options
author | Zhi An Ng <zhin@google.com> | 2022-08-16 13:03:33 -0700 |
---|---|---|
committer | XNNPACK Team <xnnpack-github-robot@google.com> | 2022-08-16 13:04:33 -0700 |
commit | 2b53a2d7bc10063941ce802d7efa4c2ce797f3f0 (patch) | |
tree | 0225ffdcbc23b7a48c4559e76acc647875a92cc5 /src | |
parent | c59833bf0ef1d2af88be0f327959be0be22d9a19 (diff) | |
download | XNNPACK-2b53a2d7bc10063941ce802d7efa4c2ce797f3f0.tar.gz |
Move post-operation structs into separate file and lib
This ensures that our microkernel tests (to be added) can depend on post-operation without depending on :operators.
PiperOrigin-RevId: 468002526
Diffstat (limited to 'src')
-rw-r--r-- | src/operators/convolution-nhwc.c | 37 | ||||
-rw-r--r-- | src/operators/post-operation.c | 52 | ||||
-rw-r--r-- | src/xnnpack/operator.h | 15 | ||||
-rw-r--r-- | src/xnnpack/post-operation.h | 38 |
4 files changed, 92 insertions, 50 deletions
diff --git a/src/operators/convolution-nhwc.c b/src/operators/convolution-nhwc.c index 329a92f0d..9015d38c0 100644 --- a/src/operators/convolution-nhwc.c +++ b/src/operators/convolution-nhwc.c @@ -27,6 +27,7 @@ #include <xnnpack/operator.h> #include <xnnpack/pack.h> #include <xnnpack/params.h> +#include <xnnpack/post-operation.h> #include <xnnpack/microparams-init.h> #ifndef XNN_ENABLE_GEMM_M_SPECIALIZATION @@ -1271,41 +1272,7 @@ enum xnn_status xnn_create_fused_convolution2d_nhwc_f32( .post_operations = post_operations, }; - union { - union xnn_f32_hswish_params hswish_params; - } post_op_params; // Anonymous union to hold params of all valid post operations. - - // Calculate how much space all post operation params will take. - size_t total_size = 0; - for (size_t i = 0; i < num_post_operations; i++) { - const struct xnn_post_operation post_op = post_operations[i]; - switch (post_op.op_type) { - case xnn_post_operation_type_hardswish: - if (xnn_params.f32.hswish.init.f32_hswish != NULL) { - total_size += xnn_params.f32.hswish.init.f32_hswish(&post_op_params.hswish_params); - } - break; - default: - XNN_UNREACHABLE; - } - } - // Copy all params compactly into post_operation_params. - char* post_operation_params = xnn_allocate_zero_memory(total_size); - char* cur_params = post_operation_params; - for (size_t i = 0; i < num_post_operations; i++) { - const struct xnn_post_operation post_op = post_operations[i]; - switch (post_op.op_type) { - case xnn_post_operation_type_hardswish: - if (xnn_params.f32.hswish.init.f32_hswish != NULL) { - const size_t initialized_size = xnn_params.f32.hswish.init.f32_hswish(&post_op_params.hswish_params); - memcpy(cur_params, &post_op_params.hswish_params, initialized_size); - cur_params += initialized_size; - } - break; - default: - XNN_UNREACHABLE; - } - } + char* post_operation_params = allocate_and_initialize_post_operation_params(num_post_operations, post_operations); union xnn_f32_minmax_params gemm_params; if XNN_LIKELY(xnn_params.f32.gemm.init.f32 != NULL) { diff --git a/src/operators/post-operation.c b/src/operators/post-operation.c new file mode 100644 index 000000000..d7350ecca --- /dev/null +++ b/src/operators/post-operation.c @@ -0,0 +1,52 @@ +// Copyright 2022 Google LLC +// +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. + +#include <xnnpack/allocator.h> +#include <xnnpack/microparams.h> +#include <xnnpack/params.h> +#include <xnnpack/post-operation.h> + +char* allocate_and_initialize_post_operation_params( + size_t num_post_operations, + struct xnn_post_operation* post_operations) { + + union { + union xnn_f32_hswish_params hswish_params; + } post_op_params; // Anonymous union to hold params of all valid post operations. + + // Calculate how much space all post operation params will take. + size_t total_size = 0; + for (size_t i = 0; i < num_post_operations; i++) { + const struct xnn_post_operation post_op = post_operations[i]; + switch (post_op.op_type) { + case xnn_post_operation_type_hardswish: + if (xnn_params.f32.hswish.init.f32_hswish != NULL) { + total_size += xnn_params.f32.hswish.init.f32_hswish(&post_op_params.hswish_params); + } + break; + default: + XNN_UNREACHABLE; + } + } + // Copy all params compactly into post_operation_params. + char* post_operation_params = xnn_allocate_zero_memory(total_size); + char* cur_params = post_operation_params; + for (size_t i = 0; i < num_post_operations; i++) { + const struct xnn_post_operation post_op = post_operations[i]; + switch (post_op.op_type) { + case xnn_post_operation_type_hardswish: + if (xnn_params.f32.hswish.init.f32_hswish != NULL) { + const size_t initialized_size = xnn_params.f32.hswish.init.f32_hswish(&post_op_params.hswish_params); + memcpy(cur_params, &post_op_params.hswish_params, initialized_size); + cur_params += initialized_size; + } + break; + default: + XNN_UNREACHABLE; + } + } + return post_operation_params; +} + diff --git a/src/xnnpack/operator.h b/src/xnnpack/operator.h index 1f6c8db15..68df18809 100644 --- a/src/xnnpack/operator.h +++ b/src/xnnpack/operator.h @@ -118,21 +118,6 @@ struct subconvolution_params { size_t scaled_kernel_size; }; -/// Operators that can be applied post convolution. -enum xnn_post_operation_type { - xnn_post_operation_type_none, - xnn_post_operation_type_hardswish, -}; - -/// Struct representing a post operation and its associated data. For example, -/// an addition with constant will specify the constant in the arg1 field, a -/// clamp will specify min in arg1, and max in arg2. -struct xnn_post_operation { - enum xnn_post_operation_type op_type; - float arg1; - float arg2; -}; - struct xnn_operator { size_t batch_size; uint32_t padding_top; diff --git a/src/xnnpack/post-operation.h b/src/xnnpack/post-operation.h new file mode 100644 index 000000000..cbf6a5379 --- /dev/null +++ b/src/xnnpack/post-operation.h @@ -0,0 +1,38 @@ +// Copyright 2022 Google LLC +// +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. + +#pragma once + +#include <stddef.h> + +// Operators that can be applied post convolution. +enum xnn_post_operation_type { + xnn_post_operation_type_none, + xnn_post_operation_type_hardswish, +}; + +// Struct representing a post operation and its associated data. For example, +// an addition with constant will specify the constant in the arg1 field, a +// clamp will specify min in arg1, and max in arg2. +struct xnn_post_operation { + enum xnn_post_operation_type op_type; + float arg1; + float arg2; +}; + + +#ifdef __cplusplus +extern "C" { +#endif + +// Allocate space for params required for post_operations and initialize all params. +// This allocation will be freed when the operator holding these params is deleted. +char* allocate_and_initialize_post_operation_params( + size_t num_post_operations, + struct xnn_post_operation* post_operations); + +#ifdef __cplusplus +} // extern "C" +#endif |