1*4bdc9457SAndroid Build Coastguard Worker // Copyright 2022 Google LLC 2*4bdc9457SAndroid Build Coastguard Worker // 3*4bdc9457SAndroid Build Coastguard Worker // This source code is licensed under the BSD-style license found in the 4*4bdc9457SAndroid Build Coastguard Worker // LICENSE file in the root directory of this source tree. 5*4bdc9457SAndroid Build Coastguard Worker 6*4bdc9457SAndroid Build Coastguard Worker #include <xnnpack/allocator.h> 7*4bdc9457SAndroid Build Coastguard Worker #include <xnnpack/microparams.h> 8*4bdc9457SAndroid Build Coastguard Worker #include <xnnpack/params.h> 9*4bdc9457SAndroid Build Coastguard Worker #include <xnnpack/post-operation.h> 10*4bdc9457SAndroid Build Coastguard Worker allocate_and_initialize_post_operation_params(size_t num_post_operations,struct xnn_post_operation * post_operations)11*4bdc9457SAndroid Build Coastguard Workerchar* allocate_and_initialize_post_operation_params( 12*4bdc9457SAndroid Build Coastguard Worker size_t num_post_operations, 13*4bdc9457SAndroid Build Coastguard Worker struct xnn_post_operation* post_operations) { 14*4bdc9457SAndroid Build Coastguard Worker 15*4bdc9457SAndroid Build Coastguard Worker union { 16*4bdc9457SAndroid Build Coastguard Worker union xnn_f32_hswish_params hswish_params; 17*4bdc9457SAndroid Build Coastguard Worker } post_op_params; // Anonymous union to hold params of all valid post operations. 18*4bdc9457SAndroid Build Coastguard Worker 19*4bdc9457SAndroid Build Coastguard Worker // Calculate how much space all post operation params will take. 20*4bdc9457SAndroid Build Coastguard Worker size_t total_size = 0; 21*4bdc9457SAndroid Build Coastguard Worker for (size_t i = 0; i < num_post_operations; i++) { 22*4bdc9457SAndroid Build Coastguard Worker const struct xnn_post_operation post_op = post_operations[i]; 23*4bdc9457SAndroid Build Coastguard Worker switch (post_op.op_type) { 24*4bdc9457SAndroid Build Coastguard Worker case xnn_post_operation_type_hardswish: 25*4bdc9457SAndroid Build Coastguard Worker if (xnn_params.f32.hswish.init.f32_hswish != NULL) { 26*4bdc9457SAndroid Build Coastguard Worker total_size += xnn_params.f32.hswish.init.f32_hswish(&post_op_params.hswish_params); 27*4bdc9457SAndroid Build Coastguard Worker } 28*4bdc9457SAndroid Build Coastguard Worker break; 29*4bdc9457SAndroid Build Coastguard Worker default: 30*4bdc9457SAndroid Build Coastguard Worker XNN_UNREACHABLE; 31*4bdc9457SAndroid Build Coastguard Worker } 32*4bdc9457SAndroid Build Coastguard Worker } 33*4bdc9457SAndroid Build Coastguard Worker // Copy all params compactly into post_operation_params. 34*4bdc9457SAndroid Build Coastguard Worker char* post_operation_params = xnn_allocate_zero_memory(total_size); 35*4bdc9457SAndroid Build Coastguard Worker char* cur_params = post_operation_params; 36*4bdc9457SAndroid Build Coastguard Worker for (size_t i = 0; i < num_post_operations; i++) { 37*4bdc9457SAndroid Build Coastguard Worker const struct xnn_post_operation post_op = post_operations[i]; 38*4bdc9457SAndroid Build Coastguard Worker switch (post_op.op_type) { 39*4bdc9457SAndroid Build Coastguard Worker case xnn_post_operation_type_hardswish: 40*4bdc9457SAndroid Build Coastguard Worker if (xnn_params.f32.hswish.init.f32_hswish != NULL) { 41*4bdc9457SAndroid Build Coastguard Worker const size_t initialized_size = xnn_params.f32.hswish.init.f32_hswish(&post_op_params.hswish_params); 42*4bdc9457SAndroid Build Coastguard Worker memcpy(cur_params, &post_op_params.hswish_params, initialized_size); 43*4bdc9457SAndroid Build Coastguard Worker cur_params += initialized_size; 44*4bdc9457SAndroid Build Coastguard Worker } 45*4bdc9457SAndroid Build Coastguard Worker break; 46*4bdc9457SAndroid Build Coastguard Worker default: 47*4bdc9457SAndroid Build Coastguard Worker XNN_UNREACHABLE; 48*4bdc9457SAndroid Build Coastguard Worker } 49*4bdc9457SAndroid Build Coastguard Worker } 50*4bdc9457SAndroid Build Coastguard Worker return post_operation_params; 51*4bdc9457SAndroid Build Coastguard Worker } 52*4bdc9457SAndroid Build Coastguard Worker 53