ipu_utils.h 3.3 KB
Newer Older
J
jianghaicheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* 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. */

#pragma once

#include <popart/ndarraywrapper.hpp>
#include <popart/tensordata.hpp>
#include <popart/tensorinfo.hpp>
A
Allen Guo 已提交
20
#include <popart/vendored/any.hpp>
J
jianghaicheng 已提交
21

A
Allen Guo 已提交
22
#include "paddle/fluid/framework/ir/graph.h"
J
jianghaicheng 已提交
23
#include "paddle/fluid/framework/lod_tensor.h"
A
Allen Guo 已提交
24 25
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/platform/float16.h"
J
jianghaicheng 已提交
26

27
using float16 = paddle::platform::float16;
28
using Tensor = phi::DenseTensor;
29
using LoDTensor = phi::DenseTensor;
30 31 32 33 34 35 36
using Scope = paddle::framework::Scope;
using OpDesc = paddle::framework::OpDesc;
using Graph = paddle::framework::ir::Graph;
using Node = paddle::framework::ir::Node;
using BlockDesc = paddle::framework::BlockDesc;
using VarType = paddle::framework::proto::VarType;

J
jianghaicheng 已提交
37 38 39 40
namespace paddle {
namespace platform {
namespace ipu {

A
Allen Guo 已提交
41 42 43
template <typename T>
T GetSingleVarFromScope(const Scope* scope, const std::string& var_name) {
  auto var = scope->GetVar(var_name);
44
  auto tensor = var->Get<phi::DenseTensor>();
A
Allen Guo 已提交
45
  return tensor.data<T>()[0];
J
jianghaicheng 已提交
46 47
}

A
Allen Guo 已提交
48 49 50
struct IpuCustomOpIdentifier {
  IpuCustomOpIdentifier(const std::string& _paddle_op,
                        const std::string& _popart_op,
51 52
                        const std::string& _domain,
                        unsigned int _version)
A
Allen Guo 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65
      : paddle_op(_paddle_op), popart_op(_domain, _popart_op, _version) {}

  std::string repr() {
    std::ostringstream os;
    os << "paddle_op: " << paddle_op << ", domain: " << popart_op.domain
       << ", type: " << popart_op.type << ", version: " << popart_op.version;
    return os.str();
  }

  std::string paddle_op;
  popart::OperatorIdentifier popart_op;
};

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
// Onnx dtype
// https://github.com/onnx/onnx/blob/master/onnx/onnx-ml.proto3
enum ONNXDataType : int {
  UNDEFINED = 0,
  FLOAT = 1,
  UINT8 = 2,
  INT8 = 3,
  UINT16 = 4,
  INT16 = 5,
  INT32 = 6,
  INT64 = 7,
  STRING = 8,
  BOOL = 9,
  FLOAT16 = 10,
  DOUBLE = 11,
  UINT32 = 12,
  UINT64 = 13,
  COMPLEX64 = 14,
  COMPLEX128 = 15,
  BFLOAT16 = 16
A
Allen Guo 已提交
86 87
};

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
// VarType::Type to popart::DataType
const popart::DataType VarType2PopartDType(const VarType::Type type);
// phi::DataType to popart::DataType
const popart::DataType PhiDType2PopartDType(const phi::DataType type);
// popart::DataType to VarType::Type
const VarType::Type PopartDType2VarType(const popart::DataType type);
// ONNXDataType to popart::DataType
const popart::DataType OnnxDType2PopartType(const ONNXDataType type);
// VarType::Type to ONNXDataType
const ONNXDataType VarType2OnnxDType(const VarType::Type type);
// VarType::Type to String in Popart
const std::string VarType2PopartStr(const VarType::Type type);
// Get bool from envirnment varaible
const bool GetBoolEnv(const std::string& str);
// Request number of ipus must be pow(2, n)
const int RequestIpus(const int num_ipus);
A
Allen Guo 已提交
104

J
jianghaicheng 已提交
105 106 107
}  // namespace ipu
}  // namespace platform
}  // namespace paddle