ipu_utils.h 3.8 KB
Newer Older
J
jianghaicheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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>
18
#include <popart/session.hpp>
J
jianghaicheng 已提交
19 20
#include <popart/tensordata.hpp>
#include <popart/tensorinfo.hpp>
A
Allen Guo 已提交
21
#include <popart/vendored/any.hpp>
22 23 24 25
#include <popart/vendored/optional.hpp>
#include <popef/Model.hpp>
#include <popef/Reader.hpp>
#include <popef/Types.hpp>
J
jianghaicheng 已提交
26

A
Allen Guo 已提交
27
#include "paddle/fluid/framework/ir/graph.h"
J
jianghaicheng 已提交
28
#include "paddle/fluid/framework/lod_tensor.h"
A
Allen Guo 已提交
29 30
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/platform/float16.h"
J
jianghaicheng 已提交
31

32
using float16 = paddle::platform::float16;
33
using Tensor = phi::DenseTensor;
34
using LoDTensor = phi::DenseTensor;
35 36 37 38 39 40 41
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 已提交
42 43 44 45
namespace paddle {
namespace platform {
namespace ipu {

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

A
Allen Guo 已提交
53 54 55
struct IpuCustomOpIdentifier {
  IpuCustomOpIdentifier(const std::string& _paddle_op,
                        const std::string& _popart_op,
56 57
                        const std::string& _domain,
                        unsigned int _version)
A
Allen Guo 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70
      : 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;
};

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
// 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 已提交
91 92
};

93 94 95 96
// TODO(czr): remove const qualifier on return value.
const VarType::Type PopefDType2VarType(const popef::DataType type);
const phi::DataType PopefDtype2PhiDtype(const popef::DataType type);

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
// 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 已提交
113

114 115 116 117
// Convert popart session to popef
std::shared_ptr<popef::Model> PopartSessionToPopefModel(
    popart::Session* session);

J
jianghaicheng 已提交
118 119 120
}  // namespace ipu
}  // namespace platform
}  // namespace paddle