subgraph_compute.cc 12.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright (c) 2019 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.

#include "lite/kernels/npu/subgraph_compute.h"
#include <sys/time.h>
#include <time.h>
18
#include <algorithm>
19
#include <utility>
20
#include "hiai_ir_build.h"  // NOLINT
21 22 23 24
#include "lite/backends/npu/device.h"
#include "lite/core/op_registry.h"
#include "lite/kernels/npu/bridges/graph.h"
#include "lite/kernels/npu/bridges/paddle_use_bridges.h"
25
#include "lite/kernels/npu/bridges/utility.h"
26
#include "lite/utils/io.h"
27 28 29 30 31 32

namespace paddle {
namespace lite {
namespace kernels {
namespace npu {

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
std::string SubgraphEngine::GenerateModelCacheName() const {
  auto inames = device_inames_;
  auto onames = device_onames_;
  std::sort(inames.begin(), inames.end());
  std::sort(onames.begin(), onames.end());

  std::string model_cache_name = "";
  for (auto iname : inames) {
    auto itensor = scope_->FindTensor(iname);
    std::replace(iname.begin(), iname.end(), '/', '_');
    model_cache_name += "_" + iname;
    for (auto i : itensor->dims().Vectorize()) {
      model_cache_name += "_" + std::to_string(i);
    }
  }
  for (auto oname : onames) {
    auto otensor = scope_->FindTensor(oname);
    std::replace(oname.begin(), oname.end(), '/', '_');
    model_cache_name += "_" + oname;
    for (auto i : otensor->dims().Vectorize()) {
      model_cache_name += "_" + std::to_string(i);
    }
  }
  model_cache_name += "_.om";

  return model_cache_name;
}

61 62
int SubgraphEngine::BuildDeviceProgram() {
  int status = 0;
63 64
  // Convert all of ops and their input vars and weights and added into the NPU
  // HiAI IR graph
65 66 67
  subgraph::npu::Graph graph;
  const auto& bridges = subgraph::Registry::Instance();
  for (auto& inst : origin_program_) {
68
    auto op = const_cast<OpLite*>(inst.op());
69 70 71 72
    CHECK(op);
    op->CheckShape();
    op->InferShape();
    std::string op_type = op->op_info()->Type();
73
    if (!bridges.Exists(op_type, TARGET(kNPU))) {
74 75
      return subgraph::FAILED;
    }
76
    auto kernel = inst.kernel();
77 78
    status |= bridges.Select(op_type, TARGET(kNPU))(
        reinterpret_cast<void*>(&graph), op, const_cast<KernelBase*>(kernel));
79 80 81 82
    if (subgraph::CHECK_FAILED(status)) {
      return subgraph::FAILED;
    }
  }
83 84 85 86 87 88
  // Collect the valid input and output nodes in the HiAI IR graph and update
  // the input and output names
  device_inames_.clear();
  device_onames_.clear();
  std::vector<ge::Operator> device_inodes;
  std::vector<ge::Operator> device_onodes;
89
  for (auto& input_name : input_names_) {
90 91 92
    if (graph.Has(input_name)) {
      if (graph.Get(input_name)->is_data()) {
        device_inodes.push_back(*graph.Get(input_name)->data());
93 94 95
        device_inames_.push_back(input_name);
      } else {
        LOG(WARNING) << "[NPU] Input node " << input_name
96
                     << " is ignored because it is not a data node.";
97 98 99
      }
    } else {
      LOG(WARNING) << "[NPU] Input node " << input_name
100
                   << " is ignored because it does not exist.";
101
    }
102 103
  }
  for (auto& output_name : output_names_) {
104 105
    if (graph.Has(output_name)) {
      device_onodes.push_back(*graph.Get(output_name)->data());
106 107 108
      device_onames_.push_back(output_name);
    } else {
      LOG(WARNING) << "[NPU] Output node " << output_name
109
                   << " is ignored because it does not exist.";
110
    }
111
  }
112 113 114 115
  CHECK(!device_inames_.empty())
      << "[NPU] No input nodes found for building NPU model";
  CHECK(!device_onames_.empty())
      << "[NPU] No output nodes found for building NPU model";
116

117
  // Build the HiAI IR graph to HiAI om model as the device program
118 119 120
  if (device_program_map_.count(inputs_shape_) > 0) {
    return status;
  }
121 122 123
  std::string model_cache_full_dir =
      model_cache_dir_.empty() ? "" : model_cache_dir_ + "/" +
                                          GenerateModelCacheName();
124
  auto device_client = lite::npu::Device::Global().Build(
125
      model_name_, device_inodes, device_onodes, model_cache_full_dir);
126
  if (device_client == nullptr) {
127 128 129
    LOG(WARNING) << "[NPU] Build model failed!";
    return subgraph::FAILED;
  }
130 131
  auto device_program = std::make_shared<device_program_t>(device_client);
  device_program_map_[inputs_shape_] = device_program;
132

133
  // Query and check the dimensions of valid input and output tensors
134
  std::vector<hiai::TensorDimension> device_idims, device_odims;
135
  if (device_program->client->GetModelIOTensorDim(
136 137 138 139 140
          model_name_, device_idims, device_odims) != hiai::AI_SUCCESS) {
    LOG(WARNING)
        << "[NPU] Get the dimensions of input and output tensors failed!";
    return subgraph::FAILED;
  }
141 142 143
  device_program->device_idims = device_idims;
  device_program->device_odims = device_odims;

144 145 146 147 148 149 150 151
  CHECK_EQ(device_idims.size(), device_inames_.size());
  CHECK_EQ(device_odims.size(), device_onames_.size());
  origin_idims_.resize(device_inames_.size());
  origin_itensors_.resize(device_inames_.size());
  device_itensors_.resize(device_inames_.size());
  origin_odims_.resize(device_onames_.size());
  origin_otensors_.resize(device_onames_.size());
  device_otensors_.resize(device_onames_.size());
152

153
  for (int i = 0; i < device_inames_.size(); i++) {
154 155 156
    auto node = graph.Get(device_inames_[i]);
    auto precision = node->precision();
    auto layout = node->layout();
157
    origin_itensors_[i] = scope_->FindMutableTensor(device_inames_[i]);
158 159
    CHECK(origin_itensors_[i]);
    origin_idims_[i] = origin_itensors_[i]->dims();
160 161
    VLOG(3) << "[NPU] Inputs[" << i << "] name: " << device_inames_[i]
            << " precision: " << PrecisionToStr(precision)
162 163 164
            << " layout: " << DataLayoutToStr(layout) << " dims: {"
            << device_idims[i].GetNumber() << ","
            << device_idims[i].GetChannel() << ","
165 166
            << device_idims[i].GetHeight() << "," << device_idims[i].GetWidth()
            << "}";
167
    // Prepare the device input tensors
168 169 170
    CHECK_EQ(origin_idims_[i].production(),
             device_idims[i].GetNumber() * device_idims[i].GetChannel() *
                 device_idims[i].GetHeight() * device_idims[i].GetWidth());
171 172 173
    device_itensors_[i].reset(new hiai::AiTensor);
    device_itensors_[i]->Init(&(device_idims[i]));
  }
174 175
  device_program->origin_idims = origin_idims_;

176
  for (int i = 0; i < device_onames_.size(); i++) {
177 178 179
    auto node = graph.Get(device_onames_[i]);
    auto precision = node->precision();
    auto layout = node->layout();
180
    origin_otensors_[i] = scope_->FindMutableTensor(device_onames_[i]);
181 182
    CHECK(origin_otensors_[i]);
    origin_odims_[i] = origin_otensors_[i]->dims();
183 184
    VLOG(3) << "[NPU] Outputs[" << i << "] name: " << device_onames_[i]
            << " precision: " << PrecisionToStr(precision)
185
            << " layout: " << DataLayoutToStr(layout) << " dims: {"
186 187 188 189
            << device_odims[i].GetNumber() << ","
            << device_odims[i].GetChannel() << ","
            << device_odims[i].GetHeight() << "," << device_odims[i].GetWidth()
            << "}";
190 191 192 193 194
    // Prepare the device output tensors
    switch (precision) {
      case PRECISION(kFloat):
        origin_otensors_[i]->mutable_data<float>();
        break;
195 196 197
      case PRECISION(kBool):
        origin_otensors_[i]->mutable_data<bool>();
        break;
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
      case PRECISION(kInt8):
        origin_otensors_[i]->mutable_data<int8_t>();
        break;
      case PRECISION(kInt16):
        origin_otensors_[i]->mutable_data<int16_t>();
        break;
      case PRECISION(kInt32):
        origin_otensors_[i]->mutable_data<int32_t>();
        break;
      case PRECISION(kInt64):
        origin_otensors_[i]->mutable_data<int64_t>();
        break;
      default:
        LOG(FATAL) << "[NPU] " << device_onames_[i]
                   << " can't mutable data with precision type "
                   << PrecisionToStr(precision);
        break;
    }
216 217
    device_program->origin_odims = origin_odims_;

218 219 220
    CHECK_EQ(origin_odims_[i].production(),
             device_odims[i].GetNumber() * device_odims[i].GetChannel() *
                 device_odims[i].GetHeight() * device_odims[i].GetWidth());
221 222 223 224 225 226 227 228
    device_otensors_[i].reset(new hiai::AiTensor);
    device_otensors_[i]->Init(&(device_odims[i]));
  }
  return status;
}

int SubgraphEngine::LaunchDeviceProgram() {
  // Copy the data of origin input tensors to the buffer of input HiAI tensors
229 230 231
  // init device_itensors_, device_otensors_, origin_otensors_
  auto device_program = device_program_map_[inputs_shape_];

232 233
  // Run the HiAI model by name
  std::string key = "model_name";  // Note: key seems must be model_name
234 235
  hiai::AiContext model_context;
  model_context.AddPara(key, model_name_);
236 237 238 239 240 241 242
  auto GetCurrentUS = []() -> double {
    struct timeval time;
    gettimeofday(&time, NULL);
    return 1e+6 * time.tv_sec + time.tv_usec;
  };
  int istamp;
  auto start_time = GetCurrentUS();
243 244 245
  CHECK_EQ(device_program->client->Process(
               model_context, device_itensors_, device_otensors_, 1000, istamp),
           hiai::AI_SUCCESS);
246
  VLOG(3) << "[NPU] Process cost " << GetCurrentUS() - start_time << " us";
247

248 249 250
  return 0;
}

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
int SubgraphEngine::Build() {
  if (device_program_map_.count(inputs_shape_) > 0) {
    return subgraph::SUCCESS;
  }
  // In order to attach all of the ops of the block desc, we need to build the
  // original program firstly.
  BuildOriginProgram();
  // Run InferShape() of all of ops, and convert Paddle ops to NPU/XPU IR graph
  build_device_program_status_ = BuildDeviceProgram();
  return build_device_program_status_;
}

void SubgraphEngine::InitDeviceTensor() {
  auto device_program = device_program_map_[inputs_shape_];
  for (size_t i = 0; i < device_itensors_.size(); i++) {
266 267 268 269 270 271 272 273 274 275 276 277 278 279
    if (device_itensors_[i]->GetBuffer() != origin_itensors_[i]->raw_data()) {
      VLOG(3) << "init device_itensors and share input tensor buf between "
                 "device and host";
      device_itensors_[i]->Init(&(device_program->device_idims[i]));
      std::memcpy(device_itensors_[i]->GetBuffer(),
                  origin_itensors_[i]->raw_data(),
                  origin_itensors_[i]->memory_size());
      // share data buf between device_itensor and origin_itensor
      std::shared_ptr<Buffer> buffer =
          std::make_shared<Buffer>(device_itensors_[i]->GetBuffer(),
                                   lite_api::TargetType::kHost,
                                   device_itensors_[i]->GetSize());
      origin_itensors_[i]->ResetBuffer(buffer, device_itensors_[i]->GetSize());
    }
280 281
  }
  for (size_t i = 0; i < device_otensors_.size(); i++) {
282 283 284 285 286 287 288 289 290 291 292 293
    if (device_otensors_[i]->GetBuffer() != origin_otensors_[i]->raw_data()) {
      VLOG(3) << "init device_otensors and share output tensor buf between "
                 "device and host";
      device_otensors_[i]->Init(&(device_program->device_odims[i]));
      // share data buf between device_itensor and origin_itensor
      origin_otensors_[i]->Resize(device_program->origin_odims[i]);
      std::shared_ptr<Buffer> buffer =
          std::make_shared<Buffer>(device_otensors_[i]->GetBuffer(),
                                   lite_api::TargetType::kHost,
                                   device_otensors_[i]->GetSize());
      origin_otensors_[i]->ResetBuffer(buffer, device_otensors_[i]->GetSize());
    }
294 295 296
  }
}

297 298 299 300 301
bool SubgraphEngine::InputShapeChanged() {
  std::vector<std::vector<int64_t>> new_shape;
  for (auto origin_itensor : origin_itensors_) {
    new_shape.push_back(origin_itensor->dims().Vectorize());
  }
302
  if (inputs_shape_ == new_shape) {
303 304
    return false;
  }
305
  inputs_shape_ = new_shape;
306 307 308
  return true;
}

309 310
void SubgraphCompute::PrepareForRun() {
  auto& param = this->Param<param_t>();
311 312
  engine_.reset(new SubgraphEngine(ctx_.get(),
                                   param.sub_block_idx,
313 314 315
                                   param.sub_block_desc,
                                   param.input_data_names,
                                   param.output_data_names,
316 317
                                   param.scope,
                                   NPUContext::SubgraphModelCacheDir()));
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
  CHECK(engine_);
  engine_->Build();
}

void SubgraphCompute::Run() {
  CHECK(engine_);
  engine_->Launch();
}

}  // namespace npu
}  // namespace kernels
}  // namespace lite
}  // namespace paddle

REGISTER_LITE_KERNEL(subgraph,
                     kNPU,
334
                     kAny,
335 336 337
                     kNCHW,
                     paddle::lite::kernels::npu::SubgraphCompute,
                     def)
338 339 340 341
    .BindInput("Inputs",
               {LiteType::GetTensorTy(TARGET(kHost), PRECISION(kAny))})
    .BindOutput("Outputs",
                {LiteType::GetTensorTy(TARGET(kHost), PRECISION(kAny))})
342
    .Finalize();