Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BERT implementation #81

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions operator/include/operator/identity.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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) 2021, OPEN AI LAB
* Author: [email protected]
*/
#ifndef __IDENTITY_HPP__
#define __IDENTITY_HPP__

#include "operator.hpp"

namespace TEngine {

class Identity : public OperatorNoParam<Identity>
{
public:
Identity()
{
name_ = "Identity";
}
Identity(const Identity& src) = default;
virtual ~Identity(){};

void SetSchema(void) override;
};

} // namespace TEngine

#endif
34 changes: 30 additions & 4 deletions operator/operator/eltwise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,44 @@ bool Eltwise::InferShape(const std::vector<TShape>& ishape, std::vector<TShape>&
return false;
}

int i0_size = ishape[0].GetSize();
int i1_size = ishape[1].GetSize();
TShape input_shape0 = ishape[0];
TShape input_shape1 = ishape[1];
auto& dim0 = input_shape0.GetDim();
auto& dim1 = input_shape1.GetDim();

if (i0_size >= i1_size)
int dim_num = dim0.size() >= dim1.size() ? dim0.size():dim1.size();
std::vector<int> out_dims;
if (dim0.size() >= dim1.size()){
for (int i=0; i<dim0.size()-dim1.size();i++){
out_dims.push_back(dim0[i]);
}
for (int i=0; i<dim1.size();i++){
out_dims.push_back(dim0[dim0.size()-dim1.size()+i] >= dim1[i] ? dim0[dim0.size()-dim1.size()+i] : dim1[i]);
}

}
else{
for (int i=0; i<dim1.size()-dim0.size();i++){
out_dims.push_back(dim1[i]);
}
for (int i=0; i<dim0.size();i++){
out_dims.push_back(dim1[dim1.size()-dim0.size()+i] >= dim0[i] ? dim1[dim1.size()-dim0.size()+i] : dim0[i]);
}
}

/* if (i0_size >= i1_size)
{
oshape[0] = ishape[0];
}
else if (i0_size < i1_size)
{
oshape[0] = ishape[1];
}
} */
TShape shape;
shape.SetDim(out_dims);
shape.SetDataLayout(layout);

oshape[0] = shape;
return true;
}

Expand Down
16 changes: 14 additions & 2 deletions operator/operator/gather.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,26 @@ namespace TEngine {
bool Gather::InferShape(const std::vector<TShape>& ishape, std::vector<TShape>& oshape, int layout)
{
const TShape& input = ishape[0];
const TShape& input2 = ishape[1];

std::vector<int> input_dim = input.GetDim();
std::vector<int> input_dim2 = input2.GetDim();
std::vector<int> output_dim;

printf ("input2_num: %d\n", input_dim2[0]);
if (param_.axis > ( int )input_dim.size())
{
return false;
}
int indices_size = param_.indices_num;
int indices_size;
if (param_.indices_num != 0){

indices_size = param_.indices_num;

}
else {
indices_size = input_dim2[0];
}

/*
printf("gather input dims: ");
for(int i =0; i<(int)input_dim.size(); i++){
Expand Down
33 changes: 33 additions & 0 deletions operator/operator/identity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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) 2021, OPEN AI LAB
* Author: [email protected]
*/
#include "operator/identity.hpp"

namespace TEngine {

void Identity::SetSchema(void)
{
Input({"input:float32"}).Output({"output:float32"}).SetDoc(R"DOC(Identity Operator)DOC");
}

} // namespace TEngine
2 changes: 2 additions & 0 deletions operator/plugin/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
#include "operator/mish.hpp"
#include "operator/softplus.hpp"
#include "operator/reciprocal.hpp"
#include "operator/identity.hpp"
#include "operator/spatialtransformer.hpp"
#include "operator/nms.hpp"

Expand Down Expand Up @@ -251,6 +252,7 @@ int operator_plugin_init(void)
RegisterOp<Mish>("Mish");
RegisterOp<Softplus>("Softplus");
RegisterOp<Reciprocal>("Reciprocal");
RegisterOp<Identity>("Identity");
RegisterOp<SpatialTransformer>("SpatialTransformer");
RegisterOp<NMS>("NMS");
// std::cout<<"OPERATOR PLUGIN INITED\n";
Expand Down
5 changes: 4 additions & 1 deletion serializer/include/tengine/v2/tm2_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ typedef uint8_t tm_bool_t; /* bool is 1-byte unsigned integer */
#define TM2_OPSTR_L2NORMALIZATION "L2Normalization"
#define TM2_OPSTR_SOFTPLUS "Softplus"
#define TM2_OPSTR_RECIPROCAL "Reciprocal"
#define TM2_OPSTR_IDENTITY "Identity"
#define TM2_OPSTR_NMS "NMS"
#define TM2_OPSTR_SPATIALTRANSFORMER "SpatialTransformer"
/* Operator types */
Expand Down Expand Up @@ -259,7 +260,9 @@ typedef uint8_t tm_bool_t; /* bool is 1-byte unsigned integer */
#define TM2_OPTYPE_RECIPROCAL 103
#define TM2_OPTYPE_NMS 104
#define TM2_OPTYPE_SPATIALTRANSFORMER 105
#define TM2_OPTYPE_NUM 106
#define TM2_OPTYPE_IDENTITY 106
#define TM2_OPTYPE_NUM 107


/* --------------------- -------- TM objects -------------------------------- */

Expand Down
3 changes: 3 additions & 0 deletions serializer/include/tengine/v2/tm2_op_serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
#include "operator/relu1.hpp"
#include "operator/softplus.hpp"
#include "operator/reciprocal.hpp"
#include "operator/identity.hpp"
#include "operator/nms.hpp"
#include "operator/spatialtransformer.hpp"

Expand Down Expand Up @@ -317,6 +318,7 @@ bool LoadTmLogSoftmaxOp(StaticGraph* graph, StaticNode* node, void* const start_
bool LoadTmReLU1Op(StaticGraph* graph, StaticNode* node, void* const start_ptr, const TM2_Operator* tm_op);
bool LoadTmSoftplusOp(StaticGraph* graph, StaticNode* node, void* const start_ptr, const TM2_Operator* tm_op);
bool LoadTmReciprocalOp(StaticGraph* graph, StaticNode* node, void* const start_ptr, const TM2_Operator* tm_op);
bool LoadTmIdentityOp(StaticGraph* graph, StaticNode* node, void* const start_ptr, const TM2_Operator* tm_op);
bool LoadTmNMSOp(StaticGraph* graph, StaticNode* node, void* const start_ptr, const TM2_Operator* tm_op);
bool LoadTmSpatialTransformerOp(StaticGraph* graph, StaticNode* node, void* const start_ptr, const TM2_Operator* tm_op);

Expand Down Expand Up @@ -424,6 +426,7 @@ tm_uoffset_t SaveTmLogSoftmaxOp(void* const start_ptr, tm_uoffset_t* cur_pos, Op
tm_uoffset_t SaveTmReLU1Op(void* const start_ptr, tm_uoffset_t* cur_pos, Operator* op);
tm_uoffset_t SaveTmSoftplusOp(void* const start_ptr, tm_uoffset_t* cur_pos, Operator* op);
tm_uoffset_t SaveTmReciprocalOp(void* const start_ptr, tm_uoffset_t* cur_pos, Operator* op);
tm_uoffset_t SaveTmIdentityOp(void* const start_ptr, tm_uoffset_t* cur_pos, Operator* op);
tm_uoffset_t SaveTmNMSop(void* const start_ptr, tm_uoffset_t* cur_pos, Operator* op);
tm_uoffset_t SaveTmSpatialTransformerOp(void* const start_ptr, tm_uoffset_t* cur_pos, Operator* op);

Expand Down
11 changes: 11 additions & 0 deletions serializer/tengine/v2/tm2_op_load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,13 @@ bool LoadTmReciprocalOp(StaticGraph* graph, StaticNode* node, void* const start_
SetNodeOp(node, op);
return true;
}
bool LoadTmIdentityOp(StaticGraph* graph, StaticNode* node, void* const start_ptr, const TM2_Operator* tm_op)
{
StaticOp* op = CreateStaticOp(graph, TM2_OPSTR_IDENTITY);
SetNodeOp(node, op);
return true;
}

bool LoadTmNMSOp(StaticGraph* graph, StaticNode* node, void* const start_ptr, const TM2_Operator* tm_op)
{
const std::string& op_str = TM2_OPSTR_NMS;
Expand Down Expand Up @@ -1910,6 +1917,8 @@ op_load_t LoadTmOpFunc(uint32_t op_type)
return LoadTmSoftplusOp;
case TM2_OPTYPE_RECIPROCAL:
return LoadTmReciprocalOp;
case TM2_OPTYPE_IDENTITY:
return LoadTmIdentityOp;
case TM2_OPTYPE_NMS:
return LoadTmNMSOp;
case TM2_OPTYPE_SPATIALTRANSFORMER:
Expand Down Expand Up @@ -2146,6 +2155,8 @@ std::string GetOpStr(uint32_t op_type)
return std::string(TM2_OPSTR_SOFTPLUS);
case TM2_OPTYPE_RECIPROCAL:
return std::string(TM2_OPSTR_RECIPROCAL);
case TM2_OPTYPE_IDENTITY:
return std::string(TM2_OPSTR_IDENTITY);
case TM2_OPTYPE_NMS:
return std::string(TM2_OPSTR_NMS);
case TM2_OPTYPE_SPATIALTRANSFORMER:
Expand Down
10 changes: 10 additions & 0 deletions serializer/tengine/v2/tm2_op_save.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,14 @@ tm_uoffset_t SaveTmReciprocalOp(void* const start_ptr, tm_uoffset_t* cur_pos, Op
SetTmOperator(&tm_op, TM2_OPTYPE_RECIPROCAL, TM2_NOT_SET);
return WriteTmObject(start_ptr, cur_pos, &tm_op, sizeof(TM2_Operator));
}

tm_uoffset_t SaveTmIdentityOp(void* const start_ptr, tm_uoffset_t* cur_pos, Operator* op)
{
TM2_Operator tm_op;
memset(&tm_op, 0, sizeof(TM2_Operator));
SetTmOperator(&tm_op, TM2_OPTYPE_IDENTITY, TM2_NOT_SET);
return WriteTmObject(start_ptr, cur_pos, &tm_op, sizeof(TM2_Operator));
}
tm_uoffset_t SaveTmNMSOp(void* const start_ptr, tm_uoffset_t* cur_pos, Operator* op)
{
NMSParam* p = (dynamic_cast<NMS*>(op))->GetParam();
Expand Down Expand Up @@ -1982,6 +1990,8 @@ op_save_t SaveTmOpFunc(uint32_t op_type)
return SaveTmSoftplusOp;
case TM2_OPTYPE_RECIPROCAL:
return SaveTmReciprocalOp;
case TM2_OPTYPE_IDENTITY:
return SaveTmIdentityOp;
case TM2_OPTYPE_NMS:
return SaveTmNMSOp;
case TM2_OPTYPE_SPATIALTRANSFORMER:
Expand Down
1 change: 1 addition & 0 deletions tools/convert_model_to_tm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ int main(int argc, char* argv[])
std::cout << "prerun failed\n";
return -1;
}
//dump_graph(graph);
}

// Save the tengine model file
Expand Down
24 changes: 19 additions & 5 deletions tools/onnx/onnx_serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,7 @@ static bool LoadOnnxMul(StaticGraph* graph, StaticNode* node, const onnx::NodePr

param.type = ELT_PROD;

for(int i = 0; i < onnx_node.input().size(); ++i)
/* for(int i = 0; i < onnx_node.input().size(); ++i)
{
StaticTensor* tensor = FindTensor(graph, onnx_node.input(i));
std::vector<int> dims = tensor->dims;
Expand All @@ -1263,7 +1263,7 @@ static bool LoadOnnxMul(StaticGraph* graph, StaticNode* node, const onnx::NodePr
new_dims.push_back(1);
SetTensorDim(tensor, new_dims);
}
}
} */

StaticOp* op = CreateStaticOp(graph, "Eltwise");

Expand Down Expand Up @@ -1521,7 +1521,8 @@ static bool LoadOnnxGather(StaticGraph* graph, StaticNode* node, const onnx::Nod
{
GatherParam param = any_cast<GatherParam>(OpManager::GetOpDefParam("Gather"));
StaticTensor* indices_tensor = FindTensor(graph, onnx_node.input(1));

printf ("input_size: %d\n",onnx_node.input_size());
printf("gather_tensor_size: %d\n", indices_tensor->dims.size());
for (int k = 0; k < onnx_node.attribute_size(); k++)
{
const onnx::AttributeProto& attr = onnx_node.attribute(k);
Expand All @@ -1530,8 +1531,14 @@ static bool LoadOnnxGather(StaticGraph* graph, StaticNode* node, const onnx::Nod
param.axis = attr.i();
}
}
int64_t* data = ( int64_t* )GetConstTensorBuffer(indices_tensor);
param.indices_num = *data;
if (indices_tensor->dims.size() != 0){
int64_t* data = ( int64_t* )GetConstTensorBuffer(indices_tensor);
param.indices_num = *data;
}
else {
param.indices_num = 0;
}

param.is_onnx = true;

StaticOp* op = CreateStaticOp(graph, "Gather");
Expand Down Expand Up @@ -2891,7 +2898,13 @@ static bool LoadOnnxReciprocal(StaticGraph* graph, StaticNode* node, const onnx:

return true;
}
static bool LoadOnnxIdentity(StaticGraph* graph, StaticNode* node, const onnx::NodeProto& onnx_node)
{
StaticOp* op = CreateStaticOp(graph, "Identity");
SetNodeOp(node, op);

return true;
}
static bool LoadOnnxResize(StaticGraph* graph, StaticNode* node, const onnx::NodeProto& onnx_node)
{
StaticOp* op = CreateStaticOp(graph, "Interp");
Expand Down Expand Up @@ -3077,6 +3090,7 @@ bool OnnxSerializerRegisterOpLoader(void)
p_onnx->RegisterOpLoadMethod("Sqrt", op_load_t(LoadOnnxSqrt));
p_onnx->RegisterOpLoadMethod("Resize", op_load_t(LoadOnnxResize));
p_onnx->RegisterOpLoadMethod("Reciprocal", op_load_t(LoadOnnxReciprocal));
p_onnx->RegisterOpLoadMethod("Identity", op_load_t(LoadOnnxIdentity));
p_onnx->RegisterOpLoadMethod("InstanceNormalization", op_load_t(LoadOnnxInstanceNormalization));

return true;
Expand Down