xref: /aosp_15_r20/external/pytorch/aten/src/ATen/native/quantized/cpu/qnnpack/src/hardswish.c (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #include <assert.h>
10 #include <math.h>
11 #include <stddef.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 
15 #include <pytorch_qnnpack.h>
16 #include <qnnpack/log.h>
17 #include <qnnpack/operator.h>
18 
pytorch_qnnp_create_hardswish_nc_q8(size_t channels,uint8_t input_zero_point,float input_scale,uint8_t output_zero_point,float output_scale,uint8_t output_min,uint8_t output_max,uint32_t flags,pytorch_qnnp_operator_t * hardswish_out)19 enum pytorch_qnnp_status pytorch_qnnp_create_hardswish_nc_q8(
20     size_t channels,
21     uint8_t input_zero_point,
22     float input_scale,
23     uint8_t output_zero_point,
24     float output_scale,
25     uint8_t output_min,
26     uint8_t output_max,
27     uint32_t flags,
28     pytorch_qnnp_operator_t* hardswish_out) {
29   pytorch_qnnp_operator_t hardswish_op = NULL;
30   enum pytorch_qnnp_status status = pytorch_qnnp_status_uninitialized;
31 
32   if (!pytorch_qnnp_params.initialized) {
33     pytorch_qnnp_log_error(
34         "pytorch_qnnp_create_hardswish_nc_q8 failed because QNNPACK is not properly initialized");
35     goto error;
36   }
37 
38   status = pytorch_qnnp_status_invalid_parameter;
39 
40   if (channels == 0) {
41     pytorch_qnnp_log_error(
42         "failed to create Hardswish operator with %zu channels: number of channels must be non-zero",
43         channels);
44     goto error;
45   }
46 
47   if (input_scale <= 0.0f || !isnormal(input_scale)) {
48     pytorch_qnnp_log_error(
49         "failed to create Hardswish operator with %.7g input scale: scale must be finite and positive",
50         input_scale);
51     goto error;
52   }
53 
54   if (output_scale <= 0.0f || !isnormal(output_scale)) {
55     pytorch_qnnp_log_error(
56         "failed to create Hardswish operator with %.7g output scale: scale must be finite and positive",
57         output_scale);
58     goto error;
59   }
60 
61   if (output_min >= output_max) {
62     pytorch_qnnp_log_error(
63         "failed to create Hardswish operator with [%" PRIu8 ", %" PRIu8
64         "] output range: range min must be below range max",
65         output_min,
66         output_max);
67     goto error;
68   }
69 
70   status = pytorch_qnnp_status_out_of_memory;
71 
72   hardswish_op = calloc(1, sizeof(struct pytorch_qnnp_operator));
73   if (hardswish_op == NULL) {
74     pytorch_qnnp_log_error(
75         "failed to allocate %zu bytes for pytorch_qnnp_operator structure",
76         sizeof(struct pytorch_qnnp_operator));
77     goto error;
78   }
79 
80   hardswish_op->lookup_table = malloc(256 * sizeof(uint8_t));
81   if (hardswish_op->lookup_table == NULL) {
82     pytorch_qnnp_log_error(
83         "failed to allocate 256 bytes for Hardswish lookup table");
84     goto error;
85   }
86 
87   uint8_t* lookup_table = hardswish_op->lookup_table;
88   const float scaled_min = (float)(int32_t)output_min;
89   const float scaled_max = (float)(int32_t)output_max;
90   const float inv_output_scale = 1.0f / output_scale;
91   for (int32_t i = 0; i < 256; i++) {
92     float x =
93         input_scale * (float)(i - (int32_t)(uint32_t)input_zero_point);
94     // hardswish, no min/max functions in C
95     float x2 = x + 3.0f;
96     x2 = x2 > 0.0f ? x2 : 0.0f;
97     x2 = x2 < 6.0f ? x2 : 6.0f;
98     x2 = x * x2 / 6.0f;
99     float scaled_hardswish_x = inv_output_scale * x2 + output_zero_point;
100     if (scaled_hardswish_x < scaled_min) {
101       scaled_hardswish_x = scaled_min;
102     }
103     if (scaled_hardswish_x > scaled_max) {
104       scaled_hardswish_x = scaled_max;
105     }
106     lookup_table[(uint32_t)i] = (uint8_t)lrintf(scaled_hardswish_x);
107   }
108 
109   hardswish_op->channels = channels;
110 
111   hardswish_op->ukernel_type = pytorch_qnnp_ukernel_type_lut;
112   hardswish_op->format = pytorch_qnnp_format_quint8;
113 
114   *hardswish_out = hardswish_op;
115   return pytorch_qnnp_status_success;
116 
117 error:
118   pytorch_qnnp_delete_operator(hardswish_op);
119   return status;
120 }
121 
pytorch_qnnp_setup_hardswish_nc_q8(pytorch_qnnp_operator_t hardswish,size_t batch_size,const uint8_t * input,size_t input_stride,uint8_t * output,size_t output_stride)122 enum pytorch_qnnp_status pytorch_qnnp_setup_hardswish_nc_q8(
123     pytorch_qnnp_operator_t hardswish,
124     size_t batch_size,
125     const uint8_t* input,
126     size_t input_stride,
127     uint8_t* output,
128     size_t output_stride) {
129   if (!pytorch_qnnp_params.initialized) {
130     pytorch_qnnp_log_error(
131         "pytorch_qnnp_setup_hardswish_nc_q8 failed because QNNPACK is not properly initialized");
132     return pytorch_qnnp_status_uninitialized;
133   }
134 
135   if (batch_size == 0) {
136     hardswish->batch_size = 0;
137     return pytorch_qnnp_status_success;
138   }
139 
140   hardswish->batch_size = batch_size;
141   hardswish->input = input;
142   hardswish->input_pixel_stride = input_stride;
143   hardswish->output = output;
144   hardswish->output_pixel_stride = output_stride;
145 
146   return pytorch_qnnp_status_success;
147 }
148