ipu_utils.cc 6.5 KB
Newer Older
J
jianghaicheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.

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. */

A
Allen Guo 已提交
15 16
#include "paddle/fluid/platform/device/ipu/ipu_utils.h"
#include <cmath>
J
jianghaicheng 已提交
17 18 19 20 21

namespace paddle {
namespace platform {
namespace ipu {

A
Allen Guo 已提交
22
void* PaddleIArray::data() { return tensor_.data(); }
J
jianghaicheng 已提交
23 24

popart::DataType PaddleIArray::dataType() const {
A
Allen Guo 已提交
25
  return PdDataType2PopartType(tensor_.dtype());
J
jianghaicheng 已提交
26 27
}

A
Allen Guo 已提交
28
std::size_t PaddleIArray::rank() const { return tensor_.dims().size(); }
J
jianghaicheng 已提交
29 30

int64_t PaddleIArray::dim(size_t index) const {
A
Allen Guo 已提交
31
  return tensor_.dims().at(index);
J
jianghaicheng 已提交
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
}

std::size_t PaddleIArray::nelms() const {
  return std::accumulate(shape_.begin(), shape_.end(), static_cast<int64_t>(1),
                         std::multiplies<int64_t>());
}

const popart::Shape PaddleIArray::shape() const { return shape_; }

popart::DataType VarType2PopartType(
    const framework::proto::VarType::Type type) {
  switch (type) {
    case framework::proto::VarType::UINT8:
      return popart::DataType::UINT8;
    case framework::proto::VarType::INT8:
      return popart::DataType::INT8;
    case framework::proto::VarType::INT16:
      return popart::DataType::INT16;
    case framework::proto::VarType::INT32:
      return popart::DataType::INT32;
    case framework::proto::VarType::INT64:
      return popart::DataType::INT64;
    case framework::proto::VarType::BOOL:
      return popart::DataType::BOOL;
    case framework::proto::VarType::FP64:
      return popart::DataType::DOUBLE;
    case framework::proto::VarType::FP32:
      return popart::DataType::FLOAT;
    case framework::proto::VarType::FP16:
      return popart::DataType::FLOAT16;
    case framework::proto::VarType::BF16:
      return popart::DataType::BFLOAT16;
    case framework::proto::VarType::COMPLEX64:
      return popart::DataType::COMPLEX64;
    case framework::proto::VarType::COMPLEX128:
      return popart::DataType::COMPLEX128;
    default:
A
Allen Guo 已提交
69
      PADDLE_THROW(paddle::platform::errors::Unimplemented(
J
jianghaicheng 已提交
70 71 72 73
          "Unsupported Paddle var type."));
  }
}

A
Allen Guo 已提交
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
popart::DataType PdDataType2PopartType(
    const paddle::experimental::DataType type) {
  switch (type) {
    case paddle::experimental::DataType::UINT8:
      return popart::DataType::UINT8;
    case paddle::experimental::DataType::INT8:
      return popart::DataType::INT8;
    case paddle::experimental::DataType::INT16:
      return popart::DataType::INT16;
    case paddle::experimental::DataType::INT32:
      return popart::DataType::INT32;
    case paddle::experimental::DataType::INT64:
      return popart::DataType::INT64;
    case paddle::experimental::DataType::BOOL:
      return popart::DataType::BOOL;
    case paddle::experimental::DataType::FLOAT64:
      return popart::DataType::DOUBLE;
    case paddle::experimental::DataType::FLOAT32:
      return popart::DataType::FLOAT;
    case paddle::experimental::DataType::FLOAT16:
      return popart::DataType::FLOAT16;
    case paddle::experimental::DataType::BFLOAT16:
      return popart::DataType::BFLOAT16;
    case paddle::experimental::DataType::COMPLEX64:
      return popart::DataType::COMPLEX64;
    case paddle::experimental::DataType::COMPLEX128:
      return popart::DataType::COMPLEX128;
    default:
      PADDLE_THROW(paddle::platform::errors::Unimplemented(
          "Unsupported Paddle data type."));
  }
}

J
jianghaicheng 已提交
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
framework::proto::VarType::Type PopartType2VarType(
    const popart::DataType type) {
  switch (type) {
    case popart::DataType::UINT8:
      return framework::proto::VarType::UINT8;
    case popart::DataType::INT8:
      return framework::proto::VarType::INT8;
    case popart::DataType::INT16:
      return framework::proto::VarType::INT16;
    case popart::DataType::INT32:
      return framework::proto::VarType::INT32;
    case popart::DataType::INT64:
      return framework::proto::VarType::INT64;
    case popart::DataType::BOOL:
      return framework::proto::VarType::BOOL;
    case popart::DataType::DOUBLE:
      return framework::proto::VarType::FP64;
    case popart::DataType::FLOAT:
      return framework::proto::VarType::FP32;
    case popart::DataType::FLOAT16:
      return framework::proto::VarType::FP16;
    case popart::DataType::BFLOAT16:
      return framework::proto::VarType::BF16;
    case popart::DataType::COMPLEX64:
      return framework::proto::VarType::COMPLEX64;
    case popart::DataType::COMPLEX128:
      return framework::proto::VarType::COMPLEX128;
    default:
      PADDLE_THROW(paddle::platform::errors::Unavailable(
          "Unsupported Paddle var type."));
  }
}

popart::DataType OnnxDtype2PopartType(const int type) {
  auto dtype = static_cast<ONNXDataType>(type);
  switch (dtype) {
    case ONNXDataType::BOOL:
      return popart::DataType::BOOL;
    case ONNXDataType::INT16:
      return popart::DataType::INT16;
    case ONNXDataType::INT32:
      return popart::DataType::INT32;
    case ONNXDataType::INT64:
      return popart::DataType::INT64;
    case ONNXDataType::FLOAT16:
      return popart::DataType::FLOAT16;
    case ONNXDataType::FLOAT:
      return popart::DataType::FLOAT;
    case ONNXDataType::DOUBLE:
      return popart::DataType::DOUBLE;
    case ONNXDataType::UINT8:
      return popart::DataType::UINT8;
    case ONNXDataType::INT8:
      return popart::DataType::INT8;
    case ONNXDataType::BFLOAT16:
      return popart::DataType::BFLOAT16;
    case ONNXDataType::COMPLEX64:
      return popart::DataType::COMPLEX64;
    case ONNXDataType::COMPLEX128:
      return popart::DataType::COMPLEX128;
    default:
      PADDLE_THROW(platform::errors::Unimplemented(
          "Unsupported ONNX data type: %d.", dtype));
  }
}

// count num should > 0
bool GetBoolEnv(std::string str) {
  char* str_val = getenv(str.c_str());
  if (str_val == NULL) {
    return false;
  } else {
    bool val = false;
    if (strcmp(str_val, "1") == 0 || strcmp(str_val, "true") == 0 ||
        strcmp(str_val, "True") == 0 || strcmp(str_val, "TRUE") == 0)
      val = true;
    return val;
  }
}

A
Allen Guo 已提交
187 188 189 190 191
int RequestIpus(const int num_ipus) {
  // num_ipus must be pow(2, n);
  return std::pow(2, ceil(log2(num_ipus)));
}

J
jianghaicheng 已提交
192 193 194
}  // namespace ipu
}  // namespace platform
}  // namespace paddle