|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * License); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +/* |
| 21 | + * Copyright (c) 2021, OPEN AI LAB |
| 22 | + * Author: Shijie Chen |
| 23 | + */ |
| 24 | + |
| 25 | +#include "layernorm_param.h" |
| 26 | + |
| 27 | +#include "graph/tensor.h" |
| 28 | +#include "graph/node.h" |
| 29 | +#include "graph/graph.h" |
| 30 | +#include "utility/sys_port.h" |
| 31 | +#include "utility/float.h" |
| 32 | +#include "utility/log.h" |
| 33 | +#include "device/cpu/cpu_node.h" |
| 34 | +#include "device/cpu/cpu_graph.h" |
| 35 | +#include "device/cpu/cpu_module.h" |
| 36 | + |
| 37 | +#include <math.h> |
| 38 | + |
| 39 | +static int init_node(struct node_ops* node_ops, struct exec_node* exec_node, struct exec_graph* exec_graph) |
| 40 | +{ |
| 41 | + return 0; |
| 42 | +} |
| 43 | + |
| 44 | +static int release_node(struct node_ops* node_ops, struct exec_node* exec_node, struct exec_graph* exec_graph) |
| 45 | +{ |
| 46 | + return 0; |
| 47 | +} |
| 48 | + |
| 49 | +static int prerun(struct node_ops* node_ops, struct exec_node* exec_node, struct exec_graph* exec_graph) |
| 50 | +{ |
| 51 | + return 0; |
| 52 | +} |
| 53 | + |
| 54 | +static int ref_layernorm_fp32(struct tensor* input_tensor, struct tensor* output_tensor, |
| 55 | + struct tensor* gamma_tensor, struct tensor* beta_tensor, float eps) |
| 56 | +{ |
| 57 | +#if 1 |
| 58 | + // TIM-VX |
| 59 | + int norm_size = input_tensor->dims[input_tensor->dim_num - 1]; |
| 60 | + int count = 1; |
| 61 | + for (int i = 0; i < input_tensor->dim_num - 1; i++) |
| 62 | + { |
| 63 | + count *= input_tensor->dims[i]; |
| 64 | + } |
| 65 | +#else |
| 66 | + // PyTorch |
| 67 | + int norm_size = gamma_tensor->elem_num; |
| 68 | + int count = input_tensor->elem_num / gamma_tensor->elem_num; |
| 69 | +#endif |
| 70 | + |
| 71 | + const float* input_data = (const float*)input_tensor->data; |
| 72 | + float* output_data = (float*)output_tensor->data; |
| 73 | + |
| 74 | + const float* gamma_data = (const float*)gamma_tensor->data; |
| 75 | + const float* beta_data = (const float*)beta_tensor->data; |
| 76 | + |
| 77 | + for (int i = 0; i < count; i++) |
| 78 | + { |
| 79 | + float sum = 0.f; |
| 80 | + float sqsum = 0.f; |
| 81 | + for (int j = 0; j < norm_size; j++) |
| 82 | + { |
| 83 | + float x = input_data[i * norm_size + j]; |
| 84 | + sum += x; |
| 85 | + sqsum += x * x; |
| 86 | + } |
| 87 | + float mean = sum / norm_size; |
| 88 | + float var = sqsum / norm_size - mean * mean; |
| 89 | + float a = 1.0f / sqrtf(var + eps); |
| 90 | + float b = -mean * a; |
| 91 | + for (int j = 0; j < norm_size; j++) |
| 92 | + { |
| 93 | + int offset = i * norm_size + j; |
| 94 | + output_data[offset] = (input_data[offset] * a + b) * gamma_data[j] + beta_data[j]; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return 0; |
| 99 | +} |
| 100 | + |
| 101 | +static int ref_layernorm_uint8(struct tensor* input_tensor, struct tensor* output_tensor, |
| 102 | + struct tensor* gamma_tensor, struct tensor* beta_tensor, float eps) |
| 103 | +{ |
| 104 | +#if 1 |
| 105 | + // TIM-VX |
| 106 | + int norm_size = input_tensor->dims[input_tensor->dim_num - 1]; |
| 107 | + int count = 1; |
| 108 | + for (int i = 0; i < input_tensor->dim_num - 1; i++) |
| 109 | + { |
| 110 | + count *= input_tensor->dims[i]; |
| 111 | + } |
| 112 | +#else |
| 113 | + // PyTorch |
| 114 | + int norm_size = gamma_tensor->elem_num; |
| 115 | + int count = input_tensor->elem_num / gamma_tensor->elem_num; |
| 116 | +#endif |
| 117 | + |
| 118 | + int total_size = input_tensor->elem_num; |
| 119 | + float* input_data = (float*)sys_malloc(total_size * sizeof(float)); |
| 120 | + float* output_data = (float*)sys_malloc(total_size * sizeof(float)); |
| 121 | + |
| 122 | + // dequant |
| 123 | + { |
| 124 | + const uint8_t* input_uint8 = (const uint8_t*)input_tensor->data; |
| 125 | + float input_scale = input_tensor->scale; |
| 126 | + int input_zero = input_tensor->zero_point; |
| 127 | + |
| 128 | + for (int i = 0; i < total_size; i++) |
| 129 | + input_data[i] = ((float)input_uint8[i] - (float)input_zero) * input_scale; |
| 130 | + } |
| 131 | + |
| 132 | + const float* gamma_data = (const float*)gamma_tensor->data; |
| 133 | + const float* beta_data = (const float*)beta_tensor->data; |
| 134 | + |
| 135 | + for (int i = 0; i < count; i++) |
| 136 | + { |
| 137 | + float sum = 0.f; |
| 138 | + float sqsum = 0.f; |
| 139 | + for (int j = 0; j < norm_size; j++) |
| 140 | + { |
| 141 | + float x = input_data[i * norm_size + j]; |
| 142 | + sum += x; |
| 143 | + sqsum += x * x; |
| 144 | + } |
| 145 | + float mean = sum / norm_size; |
| 146 | + float var = sqsum / norm_size - mean * mean; |
| 147 | + float a = 1.0f / sqrtf(var + eps); |
| 148 | + float b = -mean * a; |
| 149 | + for (int j = 0; j < norm_size; j++) |
| 150 | + { |
| 151 | + int offset = i * norm_size + j; |
| 152 | + output_data[offset] = (input_data[offset] * a + b) * gamma_data[j] + beta_data[j]; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + // quant |
| 157 | + { |
| 158 | + uint8_t* output_uint8 = (uint8_t*)output_tensor->data; |
| 159 | + float output_scale = output_tensor->scale; |
| 160 | + int output_zero = output_tensor->zero_point; |
| 161 | + for (int i = 0; i < total_size; i++) |
| 162 | + { |
| 163 | + int udata = (int)roundf(output_data[i] / output_scale + output_zero); |
| 164 | + if (udata > 255) |
| 165 | + udata = 255; |
| 166 | + else if (udata < 0) |
| 167 | + udata = 0; |
| 168 | + output_uint8[i] = udata; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + sys_free(input_data); |
| 173 | + sys_free(output_data); |
| 174 | + return 0; |
| 175 | +} |
| 176 | + |
| 177 | +static int run(struct node_ops* node_ops, struct exec_node* exec_node, struct exec_graph* exec_graph) |
| 178 | +{ |
| 179 | + struct node* node = exec_node->ir_node; |
| 180 | + struct graph* graph = node->graph; |
| 181 | + |
| 182 | + struct tensor* input_tensor = get_ir_graph_tensor(graph, node->input_tensors[0]); |
| 183 | + struct tensor* gamma_tensor = get_ir_graph_tensor(graph, node->input_tensors[1]); |
| 184 | + struct tensor* beta_tensor = get_ir_graph_tensor(graph, node->input_tensors[2]); |
| 185 | + |
| 186 | + struct tensor* output_tensor = get_ir_graph_tensor(graph, node->output_tensors[0]); |
| 187 | + |
| 188 | + struct layernorm_Param* param = (struct layernorm_Param*)node->op.param_mem; |
| 189 | + float eps = param->eps; |
| 190 | + |
| 191 | + int ret = -1; |
| 192 | + if (input_tensor->data_type == TENGINE_DT_FP32) |
| 193 | + ret = ref_layernorm_fp32(input_tensor, output_tensor, gamma_tensor, beta_tensor, eps); |
| 194 | + else if (input_tensor->data_type == TENGINE_DT_UINT8) |
| 195 | + ret = ref_layernorm_uint8(input_tensor, output_tensor, gamma_tensor, beta_tensor, eps); |
| 196 | + |
| 197 | + return ret; |
| 198 | +} |
| 199 | + |
| 200 | +static int score(struct node_ops* node_ops, struct exec_graph* exec_graph, struct node* exec_node) |
| 201 | +{ |
| 202 | + return OPS_SCORE_BEST; |
| 203 | +} |
| 204 | + |
| 205 | +static struct node_ops hcl_node_ops = {.prerun = NULL, |
| 206 | + .run = run, |
| 207 | + .reshape = NULL, |
| 208 | + .postrun = NULL, |
| 209 | + .init_node = init_node, |
| 210 | + .release_node = release_node, |
| 211 | + .score = score}; |
| 212 | + |
| 213 | +int register_layernorm_ref_op() |
| 214 | +{ |
| 215 | + return register_builtin_node_ops(OP_LAYERNORM, &hcl_node_ops); |
| 216 | +} |
| 217 | + |
| 218 | +int unregister_layernorm_ref_op() |
| 219 | +{ |
| 220 | + return unregister_builtin_node_ops(OP_LAYERNORM, &hcl_node_ops); |
| 221 | +} |
0 commit comments