subgraph_compute.cc 8.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// 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/xpu/subgraph_compute.h"
#include <sys/time.h>
#include <time.h>
#include <utility>
#include "lite/backends/xpu/device.h"
#include "lite/core/op_registry.h"
#include "lite/kernels/xpu/bridges/graph.h"
#include "lite/kernels/xpu/bridges/paddle_use_bridges.h"
23
#include "lite/kernels/xpu/bridges/utility.h"
24 25 26 27 28 29 30 31

namespace paddle {
namespace lite {
namespace kernels {
namespace xpu {

int SubgraphEngine::BuildDeviceProgram() {
  int status = 0;
32 33
  // Convert all of ops and their input vars and weights and added into the XPU
  // IR graph
34 35 36
  subgraph::xpu::Graph graph;
  const auto& bridges = subgraph::Registry::Instance();
  for (auto& inst : origin_program_) {
Z
zhupengyang 已提交
37
    auto op = const_cast<OpLite*>(inst.op());
38 39 40 41
    CHECK(op);
    op->CheckShape();
    op->InferShape();
    std::string op_type = op->op_info()->Type();
42
    if (!bridges.Exists(op_type, TARGET(kXPU))) {
43 44
      return subgraph::FAILED;
    }
45
    auto kernel = inst.kernel();
Z
zhupengyang 已提交
46 47
    status |= bridges.Select(op_type, TARGET(kXPU))(
        reinterpret_cast<void*>(&graph), op, const_cast<KernelBase*>(kernel));
48 49 50 51
    if (subgraph::CHECK_FAILED(status)) {
      return subgraph::FAILED;
    }
  }
52
  // Obtain the output nodes of the XPU IR graph and build the graph to the XPU
53
  // runtime
54 55 56 57 58
  device_inames_.clear();
  device_onames_.clear();
  std::vector<xtcl::xExpr*> device_inodes;
  std::vector<xtcl::xExpr*> device_onodes;
  for (auto& input_name : input_names_) {
59 60 61
    if (graph.Has(input_name)) {
      if (graph.Get(input_name)->is_data()) {
        device_inodes.push_back(graph.Get(input_name)->data().get());
62 63 64
        device_inames_.push_back(input_name);
      } else {
        LOG(WARNING) << "[XPU] Input node " << input_name
65
                     << " is ignored because it is not a data node.";
66 67 68
      }
    } else {
      LOG(WARNING) << "[XPU] Input node " << input_name
69
                   << " is ignored because it does not exist.";
70 71
    }
  }
72
  for (auto& output_name : output_names_) {
73 74
    if (graph.Has(output_name)) {
      device_onodes.push_back(graph.Get(output_name)->data().get());
75 76 77
      device_onames_.push_back(output_name);
    } else {
      LOG(WARNING) << "[XPU] Output node " << output_name
78
                   << " is ignored because it does not exist.";
79
    }
80
  }
81 82 83 84
  CHECK(!device_inames_.empty())
      << "[XPU] No input nodes found for building XPU model";
  CHECK(!device_onames_.empty())
      << "[XPU] No output nodes found for building XPU model";
85
  device_program_ = lite::xpu::Device::Global().Build(
86
      &graph.builder_, &graph.params_, &device_onodes);
87 88 89 90 91 92
  if (device_program_ == nullptr) {
    LOG(WARNING) << "[XPU] Build model failed!";
    return subgraph::FAILED;
  }

  // Query and check the dimensions of input and output tensors
93 94 95 96 97 98 99
  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());
  for (int i = 0; i < device_inames_.size(); i++) {
100 101 102
    auto node = graph.Get(device_inames_[i]);
    auto precision = node->precision();
    auto layout = node->layout();
103
    origin_itensors_[i] = scope_->FindMutableTensor(device_inames_[i]);
104 105
    CHECK(origin_itensors_[i]);
    origin_idims_[i] = origin_itensors_[i]->dims();
106 107
    VLOG(3) << "[XPU] Inputs[" << i << "] name: " << device_inames_[i]
            << " precision: " << PrecisionToStr(precision)
108 109 110 111 112 113 114 115 116 117 118 119 120 121
            << " layout: " << DataLayoutToStr(layout)
            << " dims: " << origin_idims_[i];
    // Prepare the device input tensors which share data with the origin input
    // tensors
    device_itensors_[i].data = nullptr;
    device_itensors_[i].ctx.device_type =
        subgraph::xpu::CvtDLDeviceType(TARGET(kHost));
    device_itensors_[i].ctx.device_id = 0;
    device_itensors_[i].ndim = origin_idims_[i].size();
    device_itensors_[i].dtype = subgraph::xpu::CvtDLDataType(precision);
    device_itensors_[i].shape = const_cast<int64_t*>(
        static_cast<const int64_t*>(origin_idims_[i].data().data()));
    device_itensors_[i].strides = nullptr;
    device_itensors_[i].byte_offset = 0;
122
  }
123
  for (int i = 0; i < device_onames_.size(); i++) {
124 125 126
    auto node = graph.Get(device_onames_[i]);
    auto precision = node->precision();
    auto layout = node->layout();
127
    origin_otensors_[i] = scope_->FindMutableTensor(device_onames_[i]);
128 129
    CHECK(origin_otensors_[i]);
    origin_odims_[i] = origin_otensors_[i]->dims();
130 131
    VLOG(3) << "[XPU] Outputs[" << i << "] name: " << device_onames_[i]
            << " precision: " << PrecisionToStr(precision)
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
            << " layout: " << DataLayoutToStr(layout)
            << " dims: " << origin_odims_[i];
    // Prepare the device output tensors which share data with the origin output
    // tensors
    switch (precision) {
      case PRECISION(kFloat):
        origin_otensors_[i]->mutable_data<float>();
        break;
      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) << "[XPU] " << device_onames_[i]
                   << " can't mutable data with precision type "
                   << PrecisionToStr(precision);
        break;
    }
    device_otensors_[i].data = nullptr;
    device_otensors_[i].ctx.device_type =
        subgraph::xpu::CvtDLDeviceType(TARGET(kHost));
    device_otensors_[i].ctx.device_id = 0;
    device_otensors_[i].ndim = origin_odims_[i].size();
    device_otensors_[i].dtype = subgraph::xpu::CvtDLDataType(precision);
    device_otensors_[i].shape = const_cast<int64_t*>(
        static_cast<const int64_t*>(origin_odims_[i].data().data()));
    device_otensors_[i].strides = nullptr;
    device_otensors_[i].byte_offset = 0;
168 169 170 171 172
  }
  return status;
}

int SubgraphEngine::LaunchDeviceProgram() {
173 174 175 176
  for (size_t i = 0; i < device_itensors_.size(); i++) {
    // Update the data pointer of DLTensor to track the origin input tensors
    device_itensors_[i].data =
        const_cast<void*>(origin_itensors_[i]->raw_data());
177
    device_program_->SetInput(device_inames_[i], &device_itensors_[i]);
178 179 180 181 182 183 184 185 186 187
  }
  // Run the XPU model
  auto GetCurrentUS = []() -> double {
    struct timeval time;
    gettimeofday(&time, NULL);
    return 1e+6 * time.tv_sec + time.tv_usec;
  };
  auto start_time = GetCurrentUS();
  device_program_->Run();
  VLOG(3) << "[XPU] Process cost " << GetCurrentUS() - start_time << " us";
188 189 190 191 192
  for (size_t i = 0; i < device_otensors_.size(); i++) {
    // Update the data pointer of DLTensor to track the origin output tensors
    device_otensors_[i].data =
        const_cast<void*>(origin_otensors_[i]->raw_data());
    device_program_->CopyOutputTo(i, &device_otensors_[i]);
193 194 195 196 197 198
  }
  return 0;
}

void SubgraphCompute::PrepareForRun() {
  auto& param = this->Param<param_t>();
199 200
  engine_.reset(new SubgraphEngine(ctx_.get(),
                                   param.sub_block_idx,
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
                                   param.sub_block_desc,
                                   param.input_data_names,
                                   param.output_data_names,
                                   param.scope));
  CHECK(engine_);
  engine_->Build();
}

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

}  // namespace xpu
}  // namespace kernels
}  // namespace lite
}  // namespace paddle

REGISTER_LITE_KERNEL(subgraph,
                     kXPU,
221
                     kAny,
222 223 224
                     kNCHW,
                     paddle::lite::kernels::xpu::SubgraphCompute,
                     def)
225 226 227 228
    .BindInput("Inputs",
               {LiteType::GetTensorTy(TARGET(kHost), PRECISION(kAny))})
    .BindOutput("Outputs",
                {LiteType::GetTensorTy(TARGET(kHost), PRECISION(kAny))})
229
    .Finalize();