gradient_machine.cpp 6.0 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yu Yang 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

Y
Yu Yang 已提交
15 16
#include "gradient_machine.h"
#include "capi_private.h"
X
Xin Pan 已提交
17
#include "paddle/legacy/gserver/gradientmachines/NeuralNetwork.h"
Y
Yu Yang 已提交
18 19 20 21 22 23 24 25 26 27 28

#define cast(v) paddle::capi::cast<paddle::capi::CGradientMachine>(v)

enum GradientMatchineCreateMode {
  CREATE_MODE_NORMAL = 0,
  CREATE_MODE_TESTING = 4
};

namespace paddle {

class MyNeuralNetwork : public NeuralNetwork {
W
Wu Yi 已提交
29
 public:
Y
Yu Yang 已提交
30 31 32 33 34 35 36 37
  MyNeuralNetwork(const std::string& name, NeuralNetwork* network)
      : NeuralNetwork(name, network) {}
};

NeuralNetwork* newCustomNerualNetwork(const std::string& name,
                                      NeuralNetwork* network) {
  return new MyNeuralNetwork(name, network);
}
Y
Yu Yang 已提交
38
}  // namespace paddle
Y
Yu Yang 已提交
39 40

extern "C" {
Y
Yu Yang 已提交
41 42
paddle_error paddle_gradient_machine_create_for_inference(
    paddle_gradient_machine* machine, void* modelConfigProtobuf, int size) {
Y
Yu Yang 已提交
43
  if (modelConfigProtobuf == nullptr) return kPD_NULLPTR;
Y
Yu Yang 已提交
44 45 46
  paddle::ModelConfig config;
  if (!config.ParseFromArray(modelConfigProtobuf, size) ||
      !config.IsInitialized()) {
Y
Yu Yang 已提交
47
    return kPD_PROTOBUF_ERROR;
Y
Yu Yang 已提交
48 49 50 51 52 53
  }

  auto ptr = new paddle::capi::CGradientMachine();
  ptr->machine.reset(paddle::GradientMachine::create(
      config, CREATE_MODE_TESTING, {paddle::PARAMETER_VALUE}));
  *machine = ptr;
Y
Yu Yang 已提交
54
  return kPD_NO_ERROR;
Y
Yu Yang 已提交
55 56
}

57 58 59 60 61 62 63 64 65 66
paddle_error paddle_gradient_machine_create_for_inference_with_parameters(
    paddle_gradient_machine* machine, void* mergedModel, uint64_t size) {
  if (mergedModel == nullptr) return kPD_NULLPTR;
  std::istringstream is(std::string(static_cast<char*>(mergedModel), size));
  int64_t modelConfigSize = 0;
  is.read((char*)(&modelConfigSize), sizeof(modelConfigSize));
  std::string modelConfigProtobuf;
  modelConfigProtobuf.resize(modelConfigSize);
  is.read(&modelConfigProtobuf[0], modelConfigSize);
  paddle::TrainerConfig config;
67
  paddle::ModelConfig modelConfig;
68
  if (!config.ParseFromString(modelConfigProtobuf) || !config.IsInitialized()) {
69 70 71 72 73 74
    if (!modelConfig.ParseFromString(modelConfigProtobuf) ||
        !modelConfig.IsInitialized()) {
      return kPD_PROTOBUF_ERROR;
    }
  } else {
    modelConfig = config.model_config();
75 76 77
  }
  auto ptr = new paddle::capi::CGradientMachine();
  ptr->machine.reset(paddle::GradientMachine::create(
78
      modelConfig, CREATE_MODE_TESTING, {paddle::PARAMETER_VALUE}));
79 80 81 82 83 84 85 86 87
  std::vector<paddle::ParameterPtr>& parameters = ptr->machine->getParameters();
  for (auto& para : parameters) {
    para->load(is);
  }

  *machine = ptr;
  return kPD_NO_ERROR;
}

Y
Yu Yang 已提交
88
paddle_error paddle_gradient_machine_destroy(paddle_gradient_machine machine) {
Y
Yu Yang 已提交
89
  delete cast(machine);
Y
Yu Yang 已提交
90 91 92
  return kPD_NO_ERROR;
}

Y
Yu Yang 已提交
93 94
paddle_error paddle_gradient_machine_load_parameter_from_disk(
    paddle_gradient_machine machine, const char* path) {
Y
Yu Yang 已提交
95 96 97 98 99 100 101
  auto m = cast(machine);
  if (m == nullptr || path == nullptr || m->machine == nullptr)
    return kPD_NULLPTR;
  m->machine->loadParameters(path);
  return kPD_NO_ERROR;
}

Y
Yu Yang 已提交
102 103 104 105
paddle_error paddle_gradient_machine_forward(paddle_gradient_machine machine,
                                             paddle_arguments inArgs,
                                             paddle_arguments outArgs,
                                             bool isTrain) {
Y
Yu Yang 已提交
106 107 108 109 110 111 112 113 114 115
  auto m = cast(machine);
  auto in = paddle::capi::cast<paddle::capi::CArguments>(inArgs);
  auto out = paddle::capi::cast<paddle::capi::CArguments>(outArgs);
  if (m == nullptr || in == nullptr || out == nullptr || m->machine == nullptr)
    return kPD_NULLPTR;
  m->machine->forward(
      in->args, &out->args, isTrain ? paddle::PASS_TRAIN : paddle::PASS_TEST);
  return kPD_NO_ERROR;
}

Y
Yu Yang 已提交
116 117 118 119 120
paddle_error paddle_gradient_machine_create_shared_param(
    paddle_gradient_machine origin,
    void* modelConfigProtobuf,
    int size,
    paddle_gradient_machine* slave) {
Y
Yu Yang 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  auto o = cast(origin);
  if (origin == nullptr || slave == nullptr || o->machine == nullptr) {
    return kPD_NULLPTR;
  }
  paddle::ModelConfig config;
  if (!config.ParseFromArray(modelConfigProtobuf, size) ||
      !config.IsInitialized()) {
    return kPD_PROTOBUF_ERROR;
  }

  std::unique_ptr<paddle::capi::CGradientMachine> ptr(
      new paddle::capi::CGradientMachine());
  auto nn = paddle::NeuralNetwork::create(config);
  nn->init(config,
           [&o](int paramId, paddle::Parameter* param) {
             auto p = o->machine->getParameters()[paramId];
             param->enableSharedType(paddle::PARAMETER_VALUE,
                                     p->getBuf(paddle::PARAMETER_VALUE));
           },
           {paddle::PARAMETER_VALUE},
           false);
  ptr->machine.reset(nn);
  *slave = ptr.release();
  return kPD_NO_ERROR;
Y
Yu Yang 已提交
145 146
}
}
Y
Yu Yang 已提交
147 148 149 150 151 152 153 154

paddle_error paddle_gradient_machine_randomize_param(
    paddle_gradient_machine machine) {
  auto m = cast(machine);
  if (m == nullptr || m->machine == nullptr) return kPD_NULLPTR;
  m->machine->randParameters();
  return kPD_NO_ERROR;
}
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

paddle_error paddle_gradient_machine_get_layer_output(
    paddle_gradient_machine machine,
    const char* layerName,
    paddle_arguments args) {
  auto m = cast(machine);
  auto out = paddle::capi::cast<paddle::capi::CArguments>(args);
  if (m == nullptr || layerName == nullptr || out == nullptr ||
      m->machine == nullptr) {
    return kPD_NULLPTR;
  }

  auto layerOutput = m->machine->getLayerOutput(layerName);
  out->args.push_back(layerOutput);
  return kPD_NO_ERROR;
}
171 172 173 174 175 176 177 178 179 180

paddle_error paddle_gradient_machine_release_layer_output(
    paddle_gradient_machine machine) {
  auto m = cast(machine);
  if (m == nullptr || m->machine == nullptr) {
    return kPD_NULLPTR;
  }
  m->machine->releaseOutput();
  return kPD_NO_ERROR;
}