# 算子实现

#### 算子原型定义

bool InferShapeAndTypeAdd(Operator& op, const string& input_name1, const string& input_name2, const string& output_name) {
// vOutputDesc.push_back(op.GetInputDesc(0));
TensorDesc vOutputDesc = op.GetOutputDesc(output_name);

DataType input_dtype = op.GetInputDesc(input_name1).GetDataType();
Format input_format = op.GetInputDesc(input_name1).GetFormat();
// 针对shape维度大小进行交换
ge::Shape shapeX = op.GetInputDesc(input_name1).GetShape();
ge::Shape shapeY = op.GetInputDesc(input_name2).GetShape();
std::vector<int64_t> dimsX = shapeX.GetDims();
std::vector<int64_t> dimsY = shapeY.GetDims();
if (dimsX.size() < dimsY.size()) {
std::vector<int64_t> dimsTmp = dimsX;
dimsX = dimsY;
dimsY = dimsTmp;
}

// 对小的shape进行1补齐
if (dimsX.size() != dimsY.size()) {
int dec = dimsX.size() - dimsY.size();
for (int i = 0; i < dec; i++) {
dimsY.insert(dimsY.begin(), (int64_t)1);
}
}

// 设置输出的shape维度，取两个输入shape中每个维度的大值
std::vector<int64_t> dimVec;
for (size_t i = 0; i < dimsX.size(); i++) {
if ((dimsX[i] != dimsY[i]) && (dimsX[i] != 1) && (dimsY[i] != 1)) {
return false;
}

int64_t dims = dimsX[i] > dimsY[i] ? dimsX[i] : dimsY[i];
dimVec.push_back(dims);
}
ge::Shape outputShape = ge::Shape(dimVec);

vOutputDesc.SetShape(outputShape);
vOutputDesc.SetDataType(input_dtype);
vOutputDesc.SetFormat(input_format);
op.UpdateOutputDesc(output_name, vOutputDesc);

return true;
}