You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
graphengine/ge/host_kernels/concat_v2_kernel.cc

183 lines
6.5 KiB

5 years ago
/**
* Copyright 2020 Huawei Technologies Co., Ltd
5 years ago
*
* Licensed 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.
*/
#include "host_kernels/concat_v2_kernel.h"
5 years ago
#include <memory>
#include <set>
#include "common/debug/log.h"
#include "common/fp16_t.h"
#include "common/op/ge_op_utils.h"
#include "framework/common/debug/ge_log.h"
#include "host_kernels/kernel_utils.h"
5 years ago
#include "graph/utils/type_utils.h"
#include "inc/kernel_factory.h"
4 years ago
#include "framework/common/types.h"
5 years ago
namespace ge {
namespace {
const size_t kConcatV2InputNum = 3;
const int kSupportEmptyTensorRank = 1;
5 years ago
const std::set<DataType> concatv2_supported_type = {DT_INT32, DT_FLOAT};
template <typename T>
void GetOutputData(std::vector<T> &y_data, int64_t loop, size_t &input_size,
const std::vector<ConstGeTensorPtr> &input) {
for (int64_t i = 0; i < loop; i++) {
5 years ago
for (size_t k = 0; k < input_size; k++) {
GeShape datak_shape = input.at(k)->GetTensorDesc().GetShape();
auto buffer = input.at(k)->GetData();
const T *datak = reinterpret_cast<const T *>(buffer.data());
if (datak == nullptr || buffer.size() == 0) {
GELOGW("input[%zu] is with no data", k);
continue;
}
int64_t gapk = datak_shape.GetShapeSize() / loop; // [2,3] is 6/loop
for (int64_t j = 0; j < gapk; j++) {
5 years ago
y_data.push_back(datak[j + gapk * i]);
}
}
}
}
#define SET_OUTPUT(DTYPE, TYPE) \
case DTYPE: \
GetOutputData(y_data_##TYPE, loop, input_size, input); \
(void)output_ptr->SetData(reinterpret_cast<uint8_t *>(y_data_##TYPE.data()), y_data_##TYPE.size() * length); \
break;
} // namespace
Status ConcatV2Kernel::Compute(const ge::OpDescPtr op_desc_ptr, const vector<ge::ConstGeTensorPtr> &input,
vector<ge::GeTensorPtr> &v_output) {
GELOGI("ConcatV2Kernel in.");
if (op_desc_ptr == nullptr) {
GELOGE(PARAM_INVALID, "input opdesc is nullptr.");
return PARAM_INVALID;
}
int tidx = -1;
ConstGeTensorPtr tensor = nullptr;
Status ret = ConcatV2PreCompute(input, tidx, tensor);
5 years ago
if (ret != SUCCESS) {
return ret;
}
size_t input_size = input.size(); // N + 1
input_size--; // N
GE_CHECK_NOTNULL(tensor);
DataType data_type = tensor->GetTensorDesc().GetDataType();
5 years ago
uint32_t length = 0;
if (!TypeUtils::GetDataTypeLength(data_type, length)) {
GELOGW("Can't GetDataTypeLength of data_type: %s", TypeUtils::DataTypeToSerialString(data_type).c_str());
return NOT_CHANGED;
}
std::vector<int32_t> y_data_int32_t;
std::vector<float> y_data_float;
// Index 0 can always gets a GeTensorDesc object from any OpDescPtr.
auto output_tensor_desc = op_desc_ptr->GetOutputDesc(0);
GeTensorPtr output_ptr = MakeShared<GeTensor>(output_tensor_desc);
5 years ago
if (output_ptr == nullptr) {
GELOGE(MEMALLOC_FAILED, "MakeShared failed.");
return MEMALLOC_FAILED;
}
GeShape data0_shape = tensor->GetTensorDesc().GetShape();
int64_t loop = 1;
5 years ago
for (int i = 0; i < tidx; i++) {
loop *= data0_shape.GetDim(i);
}
switch (data_type) {
SET_OUTPUT(DT_INT32, int32_t)
SET_OUTPUT(DT_FLOAT, float)
default:
break;
}
output_ptr->MutableTensorDesc().SetDataType(data_type);
output_ptr->MutableTensorDesc().SetShape(GeShape({op_desc_ptr->GetOutputDesc(0).GetShape()}));
v_output.push_back(output_ptr);
GELOGI("ConcatV2Kernel success.");
return SUCCESS;
}
4 years ago
Status ConcatV2Kernel::ConcatV2PreCompute(const std::vector<ConstGeTensorPtr> &input,
int &tidx,
ConstGeTensorPtr &tensor) {
5 years ago
size_t input_size = input.size();
// N + 1 is greater than or equal to 3
5 years ago
if (input_size < kConcatV2InputNum) {
GELOGI("The number of input for ConcatV2 must not be less than %zu.", kConcatV2InputNum);
return NOT_CHANGED;
}
bool has_empty_tensor = false;
input_size--;
5 years ago
for (size_t i = 0; i < input_size; i++) {
if (input[i] == nullptr) {
GELOGI("Input%zu must not be null.", i);
return NOT_CHANGED;
}
if (input.at(i)->GetData().size() == 0) {
GELOGW("input[%zu] is with no data.", i);
has_empty_tensor = true;
continue;
}
if (tensor == nullptr) {
4 years ago
tensor = input.at(i); // get first valid tensor with data
5 years ago
}
}
GE_CHECK_NOTNULL(tensor);
DataType data_type = tensor->GetTensorDesc().GetDataType();
5 years ago
for (size_t i = 1; i < input_size; i++) {
if (data_type != input.at(i)->GetTensorDesc().GetDataType()) {
GELOGI("Data type of N inputs for ConcatV2 not the same, check input %zu failed.", i);
return NOT_CHANGED;
}
}
// check if input data type is supported
if (concatv2_supported_type.find(data_type) == concatv2_supported_type.end()) {
GELOGI("ConcatV2 does not support this Data type: %s.", TypeUtils::DataTypeToSerialString(data_type).c_str());
return NOT_CHANGED;
}
ConstGeTensorPtr tensor_axis = input.at(input_size);
GE_CHECK_NOTNULL(tensor_axis);
const int *axis = reinterpret_cast<const int *>(tensor_axis->GetData().data());
GE_CHECK_NOTNULL(axis);
4 years ago
tidx = axis[0]; // [-rank(values), rank(values))
int rank = static_cast<int>(tensor->GetTensorDesc().GetShape().GetDimNum()); // rank
5 years ago
if (tidx < 0) {
tidx += rank;
5 years ago
}
// 1. tidx should in range [0,rank)
// 2. empty tensor only support case: [n],[m],[]
// case: [[],[]] ,[[],[]] ,[] or other case when rank >=2 is not supported
if (tidx < 0 || tidx >= rank || (has_empty_tensor && rank > kSupportEmptyTensorRank)) {
4 years ago
GELOGW("ConcatV2 info: tidx[%d]_rank[%d]_has_empty_tensor[bool:%d] cannot be supported, skip fold.",
tidx, rank, has_empty_tensor);
5 years ago
return NOT_CHANGED;
}
return SUCCESS;
}
REGISTER_KERNEL(CONCATV2, ConcatV2Kernel);
} // namespace ge