ctr_prediction_op.h 2.4 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2019 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 <vector>
G
guru4elephant 已提交
17
#include "examples/demo-serving/ctr_prediction.pb.h"
B
barrierye 已提交
18
#include "paddle_inference_api.h"  // NOLINT
W
wangguibao 已提交
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

namespace baidu {
namespace paddle_serving {
namespace serving {

static const char* CTR_PREDICTION_MODEL_NAME = "ctr_prediction";

/**
 * CTRPredictionOp: Serve CTR prediction requests.
 *
 * Original model can be found here:
 * https://github.com/PaddlePaddle/models/tree/develop/PaddleRec/ctr
 *
 * NOTE:
 *
 * The main purpose of this OP is to demonstrate usage of large-scale sparse
 * parameter service (RocksDB for local, mCube for distributed service). To
 * achieve this, we modified the orginal model slightly:
 * 1) Function ctr_dnn_model() returns feed_vars and fetch_vars
 * 2) Use fluid.io.save_inference_model using feed_vars and fetch_vars
 * returned from ctr_dnn_model(), instead of fluid.io.save_persistables
 * 3) Further, feed_vars were specified to be inputs of concat layer. Then in
 * the process of save_inference_model(), the generated inference program will
 * have the inputs of concat layer as feed targets.
 * 4) Weight values for the embedding layer will be fetched from sparse param
 * server for each sample
 *
 * Please refer to doc/CTR_PREDICTION.md for details on the original model
 * and modifications we made
 *
 */
W
wangguibao 已提交
50

W
wangguibao 已提交
51 52 53 54 55 56 57 58 59
class CTRPredictionOp
    : public baidu::paddle_serving::predictor::OpWithChannel<
          baidu::paddle_serving::predictor::ctr_prediction::Response> {
 public:
  typedef std::vector<paddle::PaddleTensor> TensorVector;

  DECLARE_OP(CTRPredictionOp);

  int inference();
W
wangguibao 已提交
60 61 62 63 64 65

 private:
  static bthread::Mutex mutex_;
  static int64_t cube_time_us_;
  static int32_t cube_req_num_;
  static int32_t cube_req_key_num_;
W
wangguibao 已提交
66 67 68 69 70
};

}  // namespace serving
}  // namespace paddle_serving
}  // namespace baidu