neuron_adapter.h 10.1 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
/* 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. */

#pragma once

#include "NeuronAdapter.h"  // NOLINT
#include "lite/utils/cp_logging.h"

namespace paddle {
namespace lite {

class NeuronAdapter final {
 public:
  static NeuronAdapter *Global();
  // Platform APIs
  using Neuron_getVersion_Type = int (*)(uint32_t *);
  using NeuronModel_create_Type = int (*)(NeuronModel **);
  using NeuronModel_free_Type = void (*)(NeuronModel *);
  using NeuronModel_finish_Type = int (*)(NeuronModel *);
  using NeuronModel_addOperand_Type = int (*)(NeuronModel *,
                                              const NeuronOperandType *);
  using NeuronModel_setOperandValue_Type = int (*)(NeuronModel *,
                                                   int32_t,
                                                   const void *,
                                                   size_t);
  using NeuronModel_setOperandSymmPerChannelQuantParams_Type =
      int (*)(NeuronModel *, int32_t, const NeuronSymmPerChannelQuantParams *);
  using NeuronModel_addOperation_Type = int (*)(NeuronModel *,
                                                NeuronOperationType,
                                                uint32_t,
                                                const uint32_t *,
                                                uint32_t,
                                                const uint32_t *);
45 46 47 48 49 50 51 52
  using NeuronModel_addOperationExtension_Type = int (*)(NeuronModel *,
                                                         const char *,
                                                         const char *,
                                                         const NeuronDevice *,
                                                         uint32_t,
                                                         const uint32_t *,
                                                         uint32_t,
                                                         const uint32_t *);
53 54 55 56 57 58
  using NeuronModel_identifyInputsAndOutputs_Type = int (*)(
      NeuronModel *, uint32_t, const uint32_t *, uint32_t, const uint32_t *);
  using NeuronCompilation_create_Type = int (*)(NeuronModel *,
                                                NeuronCompilation **);
  using NeuronCompilation_free_Type = void (*)(NeuronCompilation *);
  using NeuronCompilation_finish_Type = int (*)(NeuronCompilation *);
59 60 61 62 63
  using NeuronCompilation_createForDevices_Type =
      int (*)(NeuronModel *,
              const NeuronDevice *const *,
              uint32_t,
              NeuronCompilation **);
64 65 66 67 68 69 70 71 72 73 74
  using NeuronExecution_create_Type = int (*)(NeuronCompilation *,
                                              NeuronExecution **);
  using NeuronExecution_free_Type = void (*)(NeuronExecution *);
  using NeuronExecution_setInput_Type = int (*)(NeuronExecution *,
                                                int32_t,
                                                const NeuronOperandType *,
                                                const void *,
                                                size_t);
  using NeuronExecution_setOutput_Type = int (*)(
      NeuronExecution *, int32_t, const NeuronOperandType *, void *, size_t);
  using NeuronExecution_compute_Type = int (*)(NeuronExecution *);
75 76 77 78
  using Neuron_getDeviceCount_Type = int (*)(uint32_t *);
  using Neuron_getDevice_Type = int (*)(uint32_t, NeuronDevice **);
  using NeuronDevice_getName_Type = int (*)(const NeuronDevice *,
                                            const char **);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 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

  Neuron_getVersion_Type Neuron_getVersion() {
    CHECK(Neuron_getVersion_ != nullptr) << "Cannot load Neuron_getVersion!";
    return Neuron_getVersion_;
  }

  NeuronModel_create_Type NeuronModel_create() {
    CHECK(NeuronModel_create_ != nullptr) << "Cannot load NeuronModel_create!";
    return NeuronModel_create_;
  }

  NeuronModel_free_Type NeuronModel_free() {
    CHECK(NeuronModel_free_ != nullptr) << "Cannot load NeuronModel_free!";
    return NeuronModel_free_;
  }

  NeuronModel_finish_Type NeuronModel_finish() {
    CHECK(NeuronModel_finish_ != nullptr) << "Cannot load NeuronModel_finish!";
    return NeuronModel_finish_;
  }

  NeuronModel_addOperand_Type NeuronModel_addOperand() {
    CHECK(NeuronModel_addOperand_ != nullptr)
        << "Cannot load NeuronModel_addOperand!";
    return NeuronModel_addOperand_;
  }

  NeuronModel_setOperandValue_Type NeuronModel_setOperandValue() {
    CHECK(NeuronModel_setOperandValue_ != nullptr)
        << "Cannot load NeuronModel_setOperandValue!";
    return NeuronModel_setOperandValue_;
  }

  NeuronModel_setOperandSymmPerChannelQuantParams_Type
  NeuronModel_setOperandSymmPerChannelQuantParams() {
    CHECK(NeuronModel_setOperandSymmPerChannelQuantParams_ != nullptr)
        << "Cannot load NeuronModel_setOperandSymmPerChannelQuantParams!";
    return NeuronModel_setOperandSymmPerChannelQuantParams_;
  }

  NeuronModel_addOperation_Type NeuronModel_addOperation() {
    CHECK(NeuronModel_addOperation_ != nullptr)
        << "Cannot load NeuronModel_addOperation!";
    return NeuronModel_addOperation_;
  }

125 126 127 128 129 130
  NeuronModel_addOperationExtension_Type NeuronModel_addOperationExtension() {
    CHECK(NeuronModel_addOperationExtension_ != nullptr)
        << "Cannot load NeuronModel_addOperationExtension!";
    return NeuronModel_addOperationExtension_;
  }

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
  NeuronModel_identifyInputsAndOutputs_Type
  NeuronModel_identifyInputsAndOutputs() {
    CHECK(NeuronModel_identifyInputsAndOutputs_ != nullptr)
        << "Cannot load NeuronModel_identifyInputsAndOutputs!";
    return NeuronModel_identifyInputsAndOutputs_;
  }

  NeuronCompilation_create_Type NeuronCompilation_create() {
    CHECK(NeuronCompilation_create_ != nullptr)
        << "Cannot load NeuronCompilation_create!";
    return NeuronCompilation_create_;
  }

  NeuronCompilation_free_Type NeuronCompilation_free() {
    CHECK(NeuronCompilation_free_ != nullptr)
        << "Cannot load NeuronCompilation_free!";
    return NeuronCompilation_free_;
  }

  NeuronCompilation_finish_Type NeuronCompilation_finish() {
    CHECK(NeuronCompilation_finish_ != nullptr)
        << "Cannot load NeuronCompilation_finish!";
    return NeuronCompilation_finish_;
  }

156 157 158 159 160 161
  NeuronCompilation_createForDevices_Type NeuronCompilation_createForDevices() {
    CHECK(NeuronCompilation_createForDevices_ != nullptr)
        << "Cannot load NeuronCompilation_createForDevices!";
    return NeuronCompilation_createForDevices_;
  }

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
  NeuronExecution_create_Type NeuronExecution_create() {
    CHECK(NeuronExecution_create_ != nullptr)
        << "Cannot load NeuronExecution_create!";
    return NeuronExecution_create_;
  }

  NeuronExecution_free_Type NeuronExecution_free() {
    CHECK(NeuronExecution_free_ != nullptr)
        << "Cannot load NeuronExecution_free!";
    return NeuronExecution_free_;
  }

  NeuronExecution_setInput_Type NeuronExecution_setInput() {
    CHECK(NeuronExecution_setInput_ != nullptr)
        << "Cannot loadcl NeuronExecution_setInput!";
    return NeuronExecution_setInput_;
  }

  NeuronExecution_setOutput_Type NeuronExecution_setOutput() {
    CHECK(NeuronExecution_setOutput_ != nullptr)
        << "Cannot load NeuronExecution_setOutput!";
    return NeuronExecution_setOutput_;
  }

  NeuronExecution_compute_Type NeuronExecution_compute() {
    CHECK(NeuronExecution_compute_ != nullptr)
        << "Cannot load NeuronExecution_compute!";
    return NeuronExecution_compute_;
  }

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
  Neuron_getDeviceCount_Type Neuron_getDeviceCount() {
    CHECK(Neuron_getDeviceCount_ != nullptr)
        << "Cannot load Neuron_getDeviceCount!";
    return Neuron_getDeviceCount_;
  }

  Neuron_getDevice_Type Neuron_getDevice() {
    CHECK(Neuron_getDevice_ != nullptr) << "Cannot load Neuron_getDevice!";
    return Neuron_getDevice_;
  }

  NeuronDevice_getName_Type NeuronDevice_getName() {
    CHECK(NeuronDevice_getName_ != nullptr)
        << "Cannot load NeuronDevice_getName!";
    return NeuronDevice_getName_;
  }

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
 private:
  NeuronAdapter();
  NeuronAdapter(const NeuronAdapter &) = delete;
  NeuronAdapter &operator=(const NeuronAdapter &) = delete;
  bool InitHandle();
  void InitFunctions();
  void *handle_{nullptr};
  Neuron_getVersion_Type Neuron_getVersion_{nullptr};
  NeuronModel_create_Type NeuronModel_create_{nullptr};
  NeuronModel_free_Type NeuronModel_free_{nullptr};
  NeuronModel_finish_Type NeuronModel_finish_{nullptr};
  NeuronModel_addOperand_Type NeuronModel_addOperand_{nullptr};
  NeuronModel_setOperandValue_Type NeuronModel_setOperandValue_{nullptr};
  NeuronModel_setOperandSymmPerChannelQuantParams_Type
      NeuronModel_setOperandSymmPerChannelQuantParams_{nullptr};
  NeuronModel_addOperation_Type NeuronModel_addOperation_{nullptr};
225 226
  NeuronModel_addOperationExtension_Type NeuronModel_addOperationExtension_{
      nullptr};
227 228 229 230 231
  NeuronModel_identifyInputsAndOutputs_Type
      NeuronModel_identifyInputsAndOutputs_{nullptr};
  NeuronCompilation_create_Type NeuronCompilation_create_{nullptr};
  NeuronCompilation_free_Type NeuronCompilation_free_{nullptr};
  NeuronCompilation_finish_Type NeuronCompilation_finish_{nullptr};
232 233
  NeuronCompilation_createForDevices_Type NeuronCompilation_createForDevices_{
      nullptr};
234 235 236 237 238
  NeuronExecution_create_Type NeuronExecution_create_{nullptr};
  NeuronExecution_free_Type NeuronExecution_free_{nullptr};
  NeuronExecution_setInput_Type NeuronExecution_setInput_{nullptr};
  NeuronExecution_setOutput_Type NeuronExecution_setOutput_{nullptr};
  NeuronExecution_compute_Type NeuronExecution_compute_{nullptr};
239 240 241
  Neuron_getDeviceCount_Type Neuron_getDeviceCount_{nullptr};
  Neuron_getDevice_Type Neuron_getDevice_{nullptr};
  NeuronDevice_getName_Type NeuronDevice_getName_{nullptr};
242 243 244
};
}  // namespace lite
}  // namespace paddle