es_agent.h 2.8 KB
Newer Older
Z
zenghsh3 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//   Copyright (c) 2020 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.

#ifndef DEEPES_PADDLE_ES_AGENT_H_
#define DEEPES_PADDLE_ES_AGENT_H_

#include "paddle_api.h"
B
Bo Zhou 已提交
19
#include "optimizer_factory.h"
Z
zenghsh3 已提交
20 21 22 23 24 25 26 27 28 29
#include "utils.h"
#include "gaussian_sampling.h"
#include "deepes.pb.h"
#include <vector>


namespace DeepES {

typedef paddle::lite_api::PaddlePredictor PaddlePredictor;

Z
zenghsh3 已提交
30 31 32 33 34 35 36 37 38 39
/**
 * @brief DeepES agent for PaddleLite.
 *
 * Users use `clone` fucntion to clone a sampling agent, which can call `add_noise`
 * function to add noise to copied parameters and call `get_predictor` fucntion to 
 * get a paddle predictor with added noise.
 *
 * Then can use `update` function to update parameters based on ES algorithm.
 * Note: parameters of cloned agents will also be updated.
 */
Z
zenghsh3 已提交
40 41 42 43 44 45 46 47 48 49
class ESAgent {
 public:
  ESAgent();

  ~ESAgent();

  ESAgent(
      std::shared_ptr<PaddlePredictor> predictor,
      std::string config_path);
  
Z
zenghsh3 已提交
50 51 52 53 54 55 56
  /** 
   * @breif Clone a sampling agent
   *
   * Only cloned ESAgent can call `add_noise` function.
   * Each cloned ESAgent will have a copy of original parameters.
   * (support sampling in multi-thread way)
   */
Z
zenghsh3 已提交
57 58
  std::shared_ptr<ESAgent> clone();
  
Z
zenghsh3 已提交
59 60 61 62 63 64
  /**
   * @brief Update parameters of predictor based on ES algorithm.
   *
   * Only not cloned ESAgent can call `update` function.
   * Parameters of cloned agents will also be updated.
   */
Z
zenghsh3 已提交
65
  bool update(
Z
zhoubo01 已提交
66
      std::vector<SamplingInfo>& noisy_info,
Z
zenghsh3 已提交
67 68
      std::vector<float>& noisy_rewards);
  
Z
zenghsh3 已提交
69
  // copied parameters = original parameters + noise
Z
zhoubo01 已提交
70
  bool add_noise(SamplingInfo& sampling_info);
Z
zenghsh3 已提交
71

Z
zenghsh3 已提交
72 73 74 75 76 77
  /**
   * @brief Get paddle predict
   *
   * if _is_sampling_agent is true, will return predictor with added noise;
   * if _is_sampling_agent is false, will return predictor without added noise.
   */
78
  std::shared_ptr<PaddlePredictor> get_predictor();
Z
zenghsh3 已提交
79 80

 private:
Z
zenghsh3 已提交
81 82
  int64_t _calculate_param_size();

Z
zenghsh3 已提交
83
  std::shared_ptr<PaddlePredictor> _predictor;
Z
zenghsh3 已提交
84
  std::shared_ptr<PaddlePredictor> _sampling_predictor;
85
  bool _is_sampling_agent;
Z
zenghsh3 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98
  std::shared_ptr<SamplingMethod> _sampling_method;
  std::shared_ptr<Optimizer> _optimizer;
  std::shared_ptr<DeepESConfig> _config;
  int64_t _param_size;
  std::vector<std::string> _param_names;
  // malloc memory of noise and neg_gradients in advance.
  float* _noise;
  float* _neg_gradients;
};

}

#endif /* DEEPES_PADDLE_ES_AGENT_H_ */