diff --git a/include/tengine_op.h b/include/tengine_op.h index 009141bc366c63a6c9c3997b292b37b739ff170d..401d710478c68428bda913946a805804d72a7a67 100644 --- a/include/tengine_op.h +++ b/include/tengine_op.h @@ -121,6 +121,7 @@ enum OP_UNSQUEEZE, OP_UPSAMPLE, OP_ZEROSLIKE, + OP_MISH, OP_BUILTIN_LAST }; diff --git a/include/tengine_op_name.h b/include/tengine_op_name.h index d105fda7bdac962e72954d99fcb7ce70716f0186..21dbadec1b0a16fe1daa1e3847c5bdd9a1cd31ed 100644 --- a/include/tengine_op_name.h +++ b/include/tengine_op_name.h @@ -114,5 +114,6 @@ #define OP_UNSQUEEZE_NAME "Unsqueeze" #define OP_UPSAMPLE_NAME "Upsample" #define OP_ZEROSLIKE_NAME "ZerosLike" +#define OP_MISH_NAME "Mish" #endif diff --git a/src/dev/cpu/op/mish/mish_ref.c b/src/dev/cpu/op/mish/mish_ref.c new file mode 100644 index 0000000000000000000000000000000000000000..c5657b0e22af1e6dc1d707278711ea8d99f5c3f0 --- /dev/null +++ b/src/dev/cpu/op/mish/mish_ref.c @@ -0,0 +1,119 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * License); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * Copyright (c) 2020, OPEN AI LAB + * Author: 942002795@qq.com + */ +#include +#include "sys_port.h" +#include "module.h" +#include "tengine_errno.h" +#include "tengine_log.h" +#include "tengine_ir.h" +#include "../../cpu_node_ops.h" +#include "tengine_op.h" + +int ref_mish_fp32(struct ir_tensor* input_tensor, struct ir_tensor* output_tensor, int num_thread) +{ + int w = input_tensor->dims[3]; + int h = output_tensor->dims[2]; + int channels = input_tensor->dims[1]; + int size = h * w; + int c_step = h * w; + + float* input_data = input_tensor->data; + float* out_data = output_tensor->data; + +#pragma omp parallel for num_threads(num_thread) + for (int q = 0; q < channels; q++) + { + float* src = input_data + c_step * q; + float* dst = out_data + c_step * q; + + for (int i = 0; i < size; i++) + { + dst[i] = src[i] * tanhf(log(1 + exp(src[i]))); + } + } + + return 0; +} + +static int init_node(struct node_ops* node_ops, struct exec_node* exec_node, struct exec_graph* exec_graph) +{ + return 0; +} + +static int release_node(struct node_ops* node_ops, struct exec_node* exec_node, struct exec_graph* exec_graph) +{ + return 0; +} + +static int run(struct node_ops* node_ops, struct exec_node* exec_node, struct exec_graph* exec_graph) +{ + struct ir_node* ir_node = exec_node->ir_node; + struct ir_graph* ir_graph = ir_node->graph; + struct ir_tensor* input_tensor; + struct ir_tensor* output_tensor; + + input_tensor = get_ir_graph_tensor(ir_graph, ir_node->input_tensors[0]); + output_tensor = get_ir_graph_tensor(ir_graph, ir_node->output_tensors[0]); + + ref_mish_fp32(input_tensor, output_tensor, exec_graph->num_thread); + + return 0; +} + +static int reshape(struct node_ops* node_ops, struct exec_node* exec_node, struct exec_graph* exec_graph) +{ + struct ir_node* node = exec_node->ir_node; + struct ir_graph* ir_graph = node->graph; + struct ir_tensor* input = get_ir_graph_tensor(ir_graph, node->input_tensors[0]); + struct ir_tensor* output = get_ir_graph_tensor(ir_graph, node->output_tensors[0]); + + int ret = set_ir_tensor_shape(output, input->dims, input->dim_num); + return ret; +} + +static int score(struct node_ops* node_ops, struct exec_graph* exec_graph, struct ir_node* exec_node) +{ + return OPS_SCORE_CANDO; +} + +static struct node_ops hcl_node_ops = {.prerun = NULL, + .run = run, + .reshape = reshape, + .postrun = NULL, + .init_node = init_node, + .release_node = release_node, + .score = score}; + +static int reg_mish_hcl_ops(void* arg) +{ + return register_builtin_node_ops(OP_MISH, &hcl_node_ops); +} + +static int unreg_mish_hcl_ops(void* arg) +{ + return unregister_builtin_node_ops(OP_MISH, &hcl_node_ops); +} + +AUTO_REGISTER_OPS(reg_mish_hcl_ops); +AUTO_UNREGISTER_OPS(unreg_mish_hcl_ops); diff --git a/src/op/mish.c b/src/op/mish.c new file mode 100644 index 0000000000000000000000000000000000000000..4b761ad3120c6dbce6c1eb928c69bd9652e354e0 --- /dev/null +++ b/src/op/mish.c @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * License); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * Copyright (c) 2020, OPEN AI LAB + * Author: 942002795@qq.com + */ + +#include +#include +#include "sys_port.h" +#include "tengine_ir.h" +#include "tengine_errno.h" +#include "tengine_log.h" +#include "tengine_op.h" +#include "parameter.h" + +static int infer_shape(struct ir_node* node) +{ + struct ir_graph* ir_graph = node->graph; + struct ir_tensor* input = get_ir_graph_tensor(ir_graph, node->input_tensors[0]); + struct ir_tensor* output = get_ir_graph_tensor(ir_graph, node->output_tensors[0]); + + set_ir_tensor_shape(output, input->dims, input->dim_num); + + return 0; +} + +static int init_op(struct ir_op* op) +{ + op->same_shape = 0; + op->infer_shape = infer_shape; + + return 0; +} + +static void release_op(struct ir_op* op) {} + +static int register_mish_op(void* arg) +{ + struct op_method m; + + m.op_version = 1; + m.init_op = init_op; + m.release_op = release_op; + + return register_op(OP_MISH, OP_MISH_NAME, &m); +} + +static int unregister_mish_op(void* arg) +{ + return unregister_op(OP_MISH, 1); +} + +AUTO_REGISTER_OP(register_mish_op); +AUTO_UNREGISTER_OP(unregister_mish_op); diff --git a/src/serializer/tm/op/tm2_mish.c b/src/serializer/tm/op/tm2_mish.c new file mode 100644 index 0000000000000000000000000000000000000000..0b5295fdaeea22ad987b61d72b7ccb025276055f --- /dev/null +++ b/src/serializer/tm/op/tm2_mish.c @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * License); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * Copyright (c) 2020, OPEN AI LAB + * Author: 942002795@qq.com + */ + +#include +#include + +#include "sys_port.h" +#include "module.h" +#include "tengine_ir.h" +#include "tengine_errno.h" +#include "tengine_log.h" +#include "tengine_serializer.h" +#include "tm2_serializer.h" +#include "tengine_op.h" + +static int mish_op_map(int op) +{ + return OP_MISH; +} + +static int tm2_load_mish(struct ir_graph* ir_graph, struct ir_node* ir_node, const TM2_Node* tm_node, + const TM2_Operator* tm_op) +{ + return 0; +} + +static int reg_tm2_ops(void* arg) +{ + struct serializer* tm2_s = find_serializer("tengine"); + + if (tm2_s == NULL) + { + TLOG_ERR("tengine serializer has not been registered yet\n"); + return -1; + } + + tm2_s->register_op_loader(tm2_s, TM2_OPTYPE_MISH, 1, tm2_load_mish, mish_op_map, NULL); + + return 0; +} + +static int unreg_tm2_ops(void* arg) +{ + struct serializer* tm2_s = find_serializer("tengine"); + + tm2_s->unregister_op_loader(tm2_s, TM2_OPTYPE_MISH, 1, tm2_load_mish); + + return 0; +} + +REGISTER_MODULE_INIT(MOD_OP_LEVEL, "reg_mish_ops", reg_tm2_ops); +REGISTER_MODULE_EXIT(MOD_OP_LEVEL, "unreg_mish_ops", unreg_tm2_ops); diff --git a/src/serializer/tm/tm2_format.h b/src/serializer/tm/tm2_format.h index 4dace24331eb8e2fb9e01df64591e74d060f9cc7..9cf7d2f9d93c3f98ac70fcd54ab7c6abdc5dbfdf 100644 --- a/src/serializer/tm/tm2_format.h +++ b/src/serializer/tm/tm2_format.h @@ -137,6 +137,7 @@ typedef uint8_t tm_bool_t; /* bool is 1-byte unsigned integer */ #define TM2_OPSTR_REDUCEL2 "ReduceL2" #define TM2_OPSTR_MEAN "Mean" #define TM2_OPSTR_MATMUL "MatMul" +#define TM2_OPSTR_MISH "Mish" /* Operator types */ #define TM2_OPTYPE_ACCURACY 0 /* No Param */ #define TM2_OPTYPE_BATCHNORMALIZATION 1 /* TM2_BatchNormParam */ @@ -230,7 +231,10 @@ typedef uint8_t tm_bool_t; /* bool is 1-byte unsigned integer */ #define TM2_OPTYPE_REDUCEL2 89 #define TM2_OPTYPE_MEAN 90 #define TM2_OPTYPE_MATMUL 91 -#define TM2_OPTYPE_NUM 92 + + +#define TM2_OPTYPE_MISH 97 +#define TM2_OPTYPE_NUM 98 /* --------------------- -------- TM objects -------------------------------- */ diff --git a/test.jpg b/test.jpg new file mode 100644 index 0000000000000000000000000000000000000000..6ba823b99f732b06bc4468e0c377a8c37540cf04 Binary files /dev/null and b/test.jpg differ