|
| 1 | +#include "PaddleCAPI.h" |
| 2 | +#include "PaddleCAPIPrivate.h" |
| 3 | +#include "paddle/gserver/gradientmachines/NeuralNetwork.h" |
| 4 | + |
| 5 | +#define cast(v) paddle::capi::cast<paddle::capi::CGradientMachine>(v) |
| 6 | + |
| 7 | +enum GradientMatchineCreateMode { |
| 8 | + CREATE_MODE_NORMAL = 0, |
| 9 | + CREATE_MODE_TESTING = 4 |
| 10 | +}; |
| 11 | + |
| 12 | +namespace paddle { |
| 13 | + |
| 14 | +class MyNeuralNetwork : public NeuralNetwork { |
| 15 | +public: |
| 16 | + MyNeuralNetwork(const std::string& name, NeuralNetwork* network) |
| 17 | + : NeuralNetwork(name, network) {} |
| 18 | +}; |
| 19 | + |
| 20 | +NeuralNetwork* newCustomNerualNetwork(const std::string& name, |
| 21 | + NeuralNetwork* network) { |
| 22 | + return new MyNeuralNetwork(name, network); |
| 23 | +} |
| 24 | +} |
| 25 | + |
| 26 | +extern "C" { |
| 27 | +int PDGradientMachineCreateForPredict(PD_GradiemtMachine* machine, |
| 28 | + void* modelConfigProtobuf, |
| 29 | + int size) { |
| 30 | + if (modelConfigProtobuf == nullptr) return PD_NULLPTR; |
| 31 | + paddle::ModelConfig config; |
| 32 | + if (!config.ParseFromArray(modelConfigProtobuf, size) || |
| 33 | + !config.IsInitialized()) { |
| 34 | + return PD_PROTOBUF_ERROR; |
| 35 | + } |
| 36 | + |
| 37 | + auto ptr = new paddle::capi::CGradientMachine(); |
| 38 | + ptr->machine.reset(paddle::GradientMachine::create( |
| 39 | + config, CREATE_MODE_TESTING, {paddle::PARAMETER_VALUE})); |
| 40 | + *machine = ptr; |
| 41 | + return PD_NO_ERROR; |
| 42 | +} |
| 43 | + |
| 44 | +int PDGradientMachineDestroy(PD_GradiemtMachine machine) { |
| 45 | + delete cast(machine); |
| 46 | + return PD_NO_ERROR; |
| 47 | +} |
| 48 | +} |
0 commit comments