floordiv_kernel.cc 10.5 KB
Newer Older
L
lujiale 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
/**
 * Copyright 2019-2020 Huawei Technologies Co., Ltd
 *
 * 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 "graph/passes/folding_kernel/floordiv_kernel.h"

#include <cfloat>

#include <memory>
#include <set>

#include "common/op/ge_op_utils.h"
#include "common/types.h"
#include "framework/common/debug/ge_log.h"
#include "graph/passes/folding_kernel/kernel_utils.h"
#include "graph/utils/type_utils.h"
#include "inc/kernel_factory.h"

namespace ge {
namespace {
const size_t kFloorDivInputX = 0;
const size_t kFloorDivInputY = 1;
const size_t kFloorDivTensorShapeIsEmpty = 0;
const size_t kFloorDivInputSize = 2;
const std::set<DataType> kFloorDivSupportedType = {DT_FLOAT,  DT_DOUBLE, DT_UINT8, DT_INT8,
                                                   DT_UINT16, DT_INT16,  DT_INT32, DT_INT64};
}  // namespace
Status FloorDivKernel::FloorDivCheck(const OpDescPtr &op_desc_ptr,
                                     const std::vector<ge::ConstGeTensorPtr> &input) const {
  // check input size
  if (op_desc_ptr == nullptr) {
    GELOGE(PARAM_INVALID, "input opdesc is nullptr.");
    return PARAM_INVALID;
  }
  if (input.size() != kFloorDivInputSize) {
    GELOGW("Unexpected FloorDiv node, node input size: %zu, node name: %s", input.size(),
           op_desc_ptr->GetName().c_str());
    return PARAM_INVALID;
  }

  // check dims of x and y
  ConstGeTensorPtr x_tensor = input.at(kFloorDivInputX);
  ConstGeTensorPtr y_tensor = input.at(kFloorDivInputY);
  GE_CHECK_NOTNULL(x_tensor);
  GE_CHECK_NOTNULL(y_tensor);
  if (x_tensor->GetTensorDesc().GetShape().GetDimNum() != kFloorDivTensorShapeIsEmpty &&
      y_tensor->GetTensorDesc().GetShape().GetDimNum() != kFloorDivTensorShapeIsEmpty) {
    // x and y are not scalars
    vector<int64_t> x_dims = x_tensor->GetTensorDesc().GetShape().GetDims();
    vector<int64_t> y_dims = y_tensor->GetTensorDesc().GetShape().GetDims();
    if (x_dims.size() != y_dims.size()) {
      GELOGW("FloorDivKernel dims of x and y do not match, node name: %s", op_desc_ptr->GetName().c_str());
      return PARAM_INVALID;
    } else {
      for (size_t i = 0; i < x_dims.size(); ++i) {
        if (x_dims[i] != y_dims[i]) {
          GELOGW("FloorDivKernel dims of x and y do not match, node name: %s", op_desc_ptr->GetName().c_str());
          return PARAM_INVALID;
        }
      }
    }
  }

  // check data type
  DataType x_data_dtype = x_tensor->GetTensorDesc().GetDataType();
  DataType y_data_dtype = y_tensor->GetTensorDesc().GetDataType();
  if (x_data_dtype != y_data_dtype) {
    GELOGW("FloorDivKernel data type of x and y do not match, x data type is %s, but y data type is %s, node name: %s.",
           TypeUtils::DataTypeToSerialString(x_data_dtype).c_str(),
           TypeUtils::DataTypeToSerialString(y_data_dtype).c_str(), op_desc_ptr->GetName().c_str());
    return PARAM_INVALID;
  }
  if (kFloorDivSupportedType.find(x_data_dtype) == kFloorDivSupportedType.end()) {
    GELOGW("FloorDivKernel data type %s not support, node name: %s",
           TypeUtils::DataTypeToSerialString(x_data_dtype).c_str(), op_desc_ptr->GetName().c_str());
    return PARAM_INVALID;
  }

  // check data
  if (x_tensor->GetData().size() == 0 || y_tensor->GetData().size() == 0) {
    GELOGW("FloorDivKernel data size of inputs is 0, node name: %s", op_desc_ptr->GetName().c_str());
    return PARAM_INVALID;
  }

  return SUCCESS;
}

void FloorDivKernel::ShapeCal(const std::vector<ge::ConstGeTensorPtr> &input, GeTensorPtr output_ptr) {
  vector<int64_t> output_dims;
  size_t x_dim = input.at(kFloorDivInputX)->GetTensorDesc().GetShape().GetDimNum();
  size_t y_dim = input.at(kFloorDivInputY)->GetTensorDesc().GetShape().GetDimNum();
  if (x_dim >= y_dim) {
    output_dims = input.at(kFloorDivInputX)->GetTensorDesc().GetShape().GetDims();
  } else {
    output_dims = input.at(kFloorDivInputY)->GetTensorDesc().GetShape().GetDims();
  }
  output_ptr->MutableTensorDesc().SetShape(GeShape(output_dims));
}

template <typename T>
T FloorDivKernel::DivCal(const T &x_i, const T &y_i) {
  if ((x_i < static_cast<T>(0)) != (y_i < static_cast<T>(0))) {
    T abs_x_i = std::abs(x_i);
    T abs_y_i = std::abs(y_i);
    return static_cast<T>(static_cast<int32_t>(-(abs_x_i + abs_y_i - 1) / abs_y_i));
  } else {
    return static_cast<T>(static_cast<int32_t>(x_i / y_i));
  }
}

template <typename T>
bool FloorDivKernel::ZeroCheck(const T &element, DataType data_type) {
  bool result = false;
  if (data_type == DT_UINT8 || data_type == DT_INT8 || data_type == DT_UINT16 || data_type == DT_INT16 ||
      data_type == DT_INT32 || data_type == DT_INT64) {
    result = (element == 0);
  } else if (data_type == DT_FLOAT) {
    result = (fabs(element) < FLT_EPSILON);
  } else if (data_type == DT_DOUBLE) {
    result = (fabs(element) < DBL_EPSILON);
  }
  return result;
}

template <typename T>
Status FloorDivKernel::DataCalBroadcast(const T &x, const T &y, size_t num_x, size_t num_y, DataType data_type,
                                        GeTensorPtr output_ptr) {
  size_t data_num = (num_x > num_y) ? num_x : num_y;
  unique_ptr<T[]> buf(new (std::nothrow) T[data_num]());
  if (buf == nullptr) {
    GELOGE(MEMALLOC_FAILED, "new buf failed");
    return INTERNAL_ERROR;
  }

  if (num_x > num_y) {
    if (ZeroCheck<T>(y, data_type)) {
      GELOGE(PARAM_INVALID, "The divisor of FloorDiv con not be zero");
      return PARAM_INVALID;
    }
    for (size_t i = 0; i < num_x; ++i) {
      buf[i] = DivCal<T>((&x)[i], y);
    }
  } else {
    for (size_t i = 0; i < num_y; ++i) {
      if (ZeroCheck<T>((&y)[i], data_type)) {
        GELOGE(PARAM_INVALID, "The divisor of FloorDiv con not be zero");
        return PARAM_INVALID;
      }
      buf[i] = DivCal<T>(x, (&y)[i]);
    }
  }
  if (output_ptr->SetData(reinterpret_cast<uint8_t *>(buf.get()), data_num * sizeof(T)) != GRAPH_SUCCESS) {
    GELOGE(PARAM_INVALID, "set data failed");
    return PARAM_INVALID;
  }

  return SUCCESS;
}

template <typename T>
Status FloorDivKernel::DataCal(const std::vector<ConstGeTensorPtr> &input, GeTensorPtr output_ptr) {
  ConstGeTensorPtr x_tensor = input.at(kFloorDivInputX);
  ConstGeTensorPtr y_tensor = input.at(kFloorDivInputY);
  GE_CHECK_NOTNULL(x_tensor);
  GE_CHECK_NOTNULL(y_tensor);
  T *x = const_cast<T *>(reinterpret_cast<const T *>(x_tensor->GetData().GetData()));
  T *y = const_cast<T *>(reinterpret_cast<const T *>(y_tensor->GetData().GetData()));
  if (x == nullptr || y == nullptr) {
    GELOGE(PARAM_INVALID, "Input tensor is nullptr.");
    return PARAM_INVALID;
  }

  size_t data_num_x = x_tensor->GetData().size() / sizeof(T);
  size_t data_num_y = y_tensor->GetData().size() / sizeof(T);
  DataType data_type = x_tensor->GetTensorDesc().GetDataType();
  if (x_tensor->GetTensorDesc().GetShape().GetDimNum() == y_tensor->GetTensorDesc().GetShape().GetDimNum()) {
    // x and y are both scalars or vector, no need broadcast
    unique_ptr<T[]> buf(new (std::nothrow) T[data_num_x]());
    if (buf == nullptr) {
      GELOGE(MEMALLOC_FAILED, "new buf failed");
      return INTERNAL_ERROR;
    }

    for (size_t i = 0; i < data_num_x; ++i) {
      if (ZeroCheck<T>(y[i], data_type)) {
        GELOGE(PARAM_INVALID, "The divisor of FloorDiv con not be zero");
        return PARAM_INVALID;
      }
      buf[i] = DivCal<T>(x[i], y[i]);
    }
    if (output_ptr->SetData(reinterpret_cast<uint8_t *>(buf.get()), data_num_x * sizeof(T)) != GRAPH_SUCCESS) {
      GELOGE(PARAM_INVALID, "set data failed");
      return PARAM_INVALID;
    }
  } else {
    // x-y is vector-scalar, need broadcast
    if (DataCalBroadcast<T>(*x, *y, data_num_x, data_num_y, data_type, output_ptr) != SUCCESS) {
      return PARAM_INVALID;
    }
  }
  return SUCCESS;
}

Status FloorDivKernel::ComputeByDataType(DataType data_type, const std::vector<ConstGeTensorPtr> &input,
                                         GeTensorPtr output_ptr) {
  Status ret;
  switch (data_type) {
    case DT_FLOAT:
      ret = DataCal<float>(input, output_ptr);
      break;
    case DT_DOUBLE:
      ret = DataCal<double>(input, output_ptr);
      break;
    case DT_UINT8:
      ret = DataCal<uint8_t>(input, output_ptr);
      break;
    case DT_INT8:
      ret = DataCal<int8_t>(input, output_ptr);
      break;
    case DT_UINT16:
      ret = DataCal<uint16_t>(input, output_ptr);
      break;
    case DT_INT16:
      ret = DataCal<int16_t>(input, output_ptr);
      break;
    case DT_INT32:
      ret = DataCal<int32_t>(input, output_ptr);
      break;
    case DT_INT64:
      ret = DataCal<int64_t>(input, output_ptr);
      break;
    default:
      GELOGI("FloorDivKernel does not support Data type:%s", TypeUtils::DataTypeToSerialString(data_type).c_str());
      return PARAM_INVALID;
  }
  return ret;
}

Status FloorDivKernel::Compute(const OpDescPtr op_desc_ptr, const std::vector<ConstGeTensorPtr> &input,
                               std::vector<GeTensorPtr> &v_output) {
  GELOGI("FloorDivKernel in");
  if (FloorDivCheck(op_desc_ptr, input) != SUCCESS) {
    GELOGW("FloorDivKernel input is invalid, failed to fold node.");
    return NOT_CHANGED;
  }

  GeTensorPtr output_ptr = MakeShared<GeTensor>();
  if (output_ptr == nullptr) {
    GELOGE(MEMALLOC_FAILED, "make_shared ge::GeTensor failed, node name %s.", op_desc_ptr->GetName().c_str());
    return NOT_CHANGED;
  }

  // calculate shape
  ShapeCal(input, output_ptr);

  // calculate data and data type
  DataType x_data_dtype = input.at(kFloorDivInputX)->GetTensorDesc().GetDataType();
  output_ptr->MutableTensorDesc().SetDataType(x_data_dtype);
  if (ComputeByDataType(x_data_dtype, input, output_ptr) != SUCCESS) {
    return NOT_CHANGED;
  }

  // print output tensor information, and will be deleted
  GELOGD("FloorDiv op %s output tensor data size is %zu", op_desc_ptr->GetName().c_str(), output_ptr->GetData().size());
  vector<int64_t> data_dims = output_ptr->GetTensorDesc().GetShape().GetDims();
  GELOGD("FloorDiv op %s output tensor dim size is %zu", op_desc_ptr->GetName().c_str(), data_dims.size());

  v_output.emplace_back(output_ptr);
  GELOGI("FloorDivKernel success.");
  return SUCCESS;
}
REGISTER_KERNEL(FLOORDIV, FloorDivKernel);
}  // namespace ge