neuron_adapter.cc 9.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 23 24 25 26 27 28 29 30 31 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/* Copyright (c) 2018 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/backends/apu/neuron_adapter.h"
#include <dlfcn.h>
#include <string>
#include <vector>

namespace paddle {
namespace lite {
NeuronAdapter* NeuronAdapter::Global() {
  static NeuronAdapter adapter;
  return &adapter;
}

NeuronAdapter::NeuronAdapter() {
  CHECK(InitHandle()) << "Fail to initialize the Neuron Adapter library!";
  InitFunctions();
}

bool NeuronAdapter::InitHandle() {
  const std::vector<std::string> paths = {
    "libneuron_adapter.so",
#if defined(__aarch64__)
    "/vendor/lib64/libneuron_adapter.so",
    "/system/lib64/libneuron_adapter.so",
    "/system/vendor/lib64/libneuron_adapter.so",
#else
    "/vendor/lib/libneuron_adapter.so",
    "/system/lib/libneuron_adapter.so",
    "/system/vendor/lib/libneuron_adapter.so",
#endif
  };
  std::string target_lib = "Unknown";
  for (auto path : paths) {
    handle_ = dlopen(path.c_str(), RTLD_LAZY);
    if (handle_ != nullptr) {
      target_lib = path;
      break;
    }
  }
  VLOG(4) << "Load the Neuron Adapter library from " << target_lib;
  if (handle_ != nullptr) {
    return true;
  } else {
    return false;
  }
}

void NeuronAdapter::InitFunctions() {
  CHECK(handle_ != nullptr) << "The library handle can't be null!";

#define PADDLE_DLSYM(neuron_adapter_func)                                 \
  do {                                                                    \
    neuron_adapter_func##_ =                                              \
        (neuron_adapter_func##_Type)dlsym(handle_, #neuron_adapter_func); \
    if (neuron_adapter_func##_ == nullptr) {                              \
      LOG(FATAL) << "Cannot find the " << #neuron_adapter_func            \
                 << " symbol in libneuron_adapter.so!";                   \
      break;                                                              \
    }                                                                     \
    VLOG(4) << "Loaded the " << #neuron_adapter_func                      \
            << " symbol successfully.";                                   \
  } while (false)

  PADDLE_DLSYM(Neuron_getVersion);
  PADDLE_DLSYM(NeuronModel_create);
  PADDLE_DLSYM(NeuronModel_free);
  PADDLE_DLSYM(NeuronModel_finish);
  PADDLE_DLSYM(NeuronModel_addOperand);
  PADDLE_DLSYM(NeuronModel_setOperandValue);
  PADDLE_DLSYM(NeuronModel_setOperandSymmPerChannelQuantParams);
  PADDLE_DLSYM(NeuronModel_addOperation);
85
  PADDLE_DLSYM(NeuronModel_addOperationExtension);
86 87 88 89
  PADDLE_DLSYM(NeuronModel_identifyInputsAndOutputs);
  PADDLE_DLSYM(NeuronCompilation_create);
  PADDLE_DLSYM(NeuronCompilation_free);
  PADDLE_DLSYM(NeuronCompilation_finish);
90
  PADDLE_DLSYM(NeuronCompilation_createForDevices);
91 92 93 94 95
  PADDLE_DLSYM(NeuronExecution_create);
  PADDLE_DLSYM(NeuronExecution_free);
  PADDLE_DLSYM(NeuronExecution_setInput);
  PADDLE_DLSYM(NeuronExecution_setOutput);
  PADDLE_DLSYM(NeuronExecution_compute);
96 97 98
  PADDLE_DLSYM(Neuron_getDeviceCount);
  PADDLE_DLSYM(Neuron_getDevice);
  PADDLE_DLSYM(NeuronDevice_getName);
99 100 101 102 103 104 105 106 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
#undef PADDLE_DLSYM
}

}  // namespace lite
}  // namespace paddle

int Neuron_getVersion(uint32_t* version) {
  return paddle::lite::NeuronAdapter::Global()->Neuron_getVersion()(version);
}

int NeuronModel_create(NeuronModel** model) {
  return paddle::lite::NeuronAdapter::Global()->NeuronModel_create()(model);
}

void NeuronModel_free(NeuronModel* model) {
  return paddle::lite::NeuronAdapter::Global()->NeuronModel_free()(model);
}

int NeuronModel_finish(NeuronModel* model) {
  return paddle::lite::NeuronAdapter::Global()->NeuronModel_finish()(model);
}

int NeuronModel_addOperand(NeuronModel* model, const NeuronOperandType* type) {
  return paddle::lite::NeuronAdapter::Global()->NeuronModel_addOperand()(model,
                                                                         type);
}

int NeuronModel_setOperandValue(NeuronModel* model,
                                int32_t index,
                                const void* buffer,
                                size_t length) {
  return paddle::lite::NeuronAdapter::Global()->NeuronModel_setOperandValue()(
      model, index, buffer, length);
}

int NeuronModel_setOperandSymmPerChannelQuantParams(
    NeuronModel* model,
    int32_t index,
    const NeuronSymmPerChannelQuantParams* channelQuant) {
  return paddle::lite::NeuronAdapter::Global()
      ->NeuronModel_setOperandSymmPerChannelQuantParams()(
          model, index, channelQuant);
}

int NeuronModel_addOperation(NeuronModel* model,
                             NeuronOperationType type,
                             uint32_t inputCount,
                             const uint32_t* inputs,
                             uint32_t outputCount,
                             const uint32_t* outputs) {
  return paddle::lite::NeuronAdapter::Global()->NeuronModel_addOperation()(
      model, type, inputCount, inputs, outputCount, outputs);
}

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
int NeuronModel_addOperationExtension(NeuronModel* model,
                                      const char* name,
                                      const char* vendor,
                                      const NeuronDevice* device,
                                      uint32_t inputCount,
                                      const uint32_t* inputs,
                                      uint32_t outputCount,
                                      const uint32_t* outputs) {
  return paddle::lite::NeuronAdapter::Global()
      ->NeuronModel_addOperationExtension()(model,
                                            name,
                                            vendor,
                                            device,
                                            inputCount,
                                            inputs,
                                            outputCount,
                                            outputs);
}

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
int NeuronModel_identifyInputsAndOutputs(NeuronModel* model,
                                         uint32_t inputCount,
                                         const uint32_t* inputs,
                                         uint32_t outputCount,
                                         const uint32_t* outputs) {
  return paddle::lite::NeuronAdapter::Global()
      ->NeuronModel_identifyInputsAndOutputs()(
          model, inputCount, inputs, outputCount, outputs);
}

int NeuronCompilation_create(NeuronModel* model,
                             NeuronCompilation** compilation) {
  return paddle::lite::NeuronAdapter::Global()->NeuronCompilation_create()(
      model, compilation);
}

void NeuronCompilation_free(NeuronCompilation* compilation) {
  return paddle::lite::NeuronAdapter::Global()->NeuronCompilation_free()(
      compilation);
}

int NeuronCompilation_finish(NeuronCompilation* compilation) {
  return paddle::lite::NeuronAdapter::Global()->NeuronCompilation_finish()(
      compilation);
}

198 199 200 201 202 203 204 205 206
int NeuronCompilation_createForDevices(NeuronModel* model,
                                       const NeuronDevice* const* devices,
                                       uint32_t numDevices,
                                       NeuronCompilation** compilation) {
  return paddle::lite::NeuronAdapter::Global()
      ->NeuronCompilation_createForDevices()(
          model, devices, numDevices, compilation);
}

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
int NeuronExecution_create(NeuronCompilation* compilation,
                           NeuronExecution** execution) {
  return paddle::lite::NeuronAdapter::Global()->NeuronExecution_create()(
      compilation, execution);
}

void NeuronExecution_free(NeuronExecution* execution) {
  return paddle::lite::NeuronAdapter::Global()->NeuronExecution_free()(
      execution);
}

int NeuronExecution_setInput(NeuronExecution* execution,
                             int32_t index,
                             const NeuronOperandType* type,
                             const void* buffer,
                             size_t length) {
  return paddle::lite::NeuronAdapter::Global()->NeuronExecution_setInput()(
      execution, index, type, buffer, length);
}

int NeuronExecution_setOutput(NeuronExecution* execution,
                              int32_t index,
                              const NeuronOperandType* type,
                              void* buffer,
                              size_t length) {
  return paddle::lite::NeuronAdapter::Global()->NeuronExecution_setOutput()(
      execution, index, type, buffer, length);
}

int NeuronExecution_compute(NeuronExecution* execution) {
  return paddle::lite::NeuronAdapter::Global()->NeuronExecution_compute()(
      execution);
}
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

int Neuron_getDeviceCount(uint32_t* numDevices) {
  return paddle::lite::NeuronAdapter::Global()->Neuron_getDeviceCount()(
      numDevices);
}

int Neuron_getDevice(uint32_t devIndex, NeuronDevice** device) {
  return paddle::lite::NeuronAdapter::Global()->Neuron_getDevice()(devIndex,
                                                                   device);
}

int NeuronDevice_getName(const NeuronDevice* device, const char** name) {
  return paddle::lite::NeuronAdapter::Global()->NeuronDevice_getName()(device,
                                                                       name);
}