simple_on_word2vec.cc 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* 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. */

/*
 * This file contains a simple demo for how to take a model for inference.
 */

#include <glog/logging.h>
#include <gtest/gtest.h>
#include <memory>
T
tensor-tang 已提交
22
#include <thread>
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 50 51 52 53 54 55 56
#include "paddle/contrib/inference/paddle_inference_api.h"
namespace paddle {
namespace demo {

DEFINE_string(dirname, "", "Directory of the inference model.");

void Main(bool use_gpu) {
  //# 1. Create PaddlePredictor with a config.
  NativeConfig config;
  config.model_dir = FLAGS_dirname + "word2vec.inference.model";
  config.use_gpu = use_gpu;
  config.fraction_of_gpu_memory = 0.15;
  config.device = 0;
  auto predictor =
      CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(config);

  for (int batch_id = 0; batch_id < 3; batch_id++) {
    //# 2. Prepare input.
    int64_t data[4] = {1, 2, 3, 4};

    PaddleBuf buf{.data = data, .length = sizeof(data)};
    PaddleTensor tensor{.name = "",
                        .shape = std::vector<int>({4, 1}),
                        .data = buf,
                        .dtype = PaddleDType::INT64};

    // For simplicity, we set all the slots with the same data.
    std::vector<PaddleTensor> slots(4, tensor);

    //# 3. Run
    std::vector<PaddleTensor> outputs;
    CHECK(predictor->Run(slots, &outputs));

    //# 4. Get output.
Y
Yan Chunwei 已提交
57
    ASSERT_EQ(outputs.size(), 1UL);
58 59 60 61 62 63
    LOG(INFO) << "output buffer size: " << outputs.front().data.length;
    const size_t num_elements = outputs.front().data.length / sizeof(float);
    // The outputs' buffers are in CPU memory.
    for (size_t i = 0; i < std::min(5UL, num_elements); i++) {
      LOG(INFO) << static_cast<float*>(outputs.front().data.data)[i];
    }
T
tensor-tang 已提交
64 65
    // TODO(Superjomn): this is should be free automatically
    free(outputs[0].data.data);
66 67 68
  }
}

T
tensor-tang 已提交
69
void MainThreads(int num_threads, bool use_gpu) {
T
tensor-tang 已提交
70 71 72 73
  // Multi-threads only support on CPU
  // 0. Create PaddlePredictor with a config.
  NativeConfig config;
  config.model_dir = FLAGS_dirname + "word2vec.inference.model";
T
tensor-tang 已提交
74
  config.use_gpu = use_gpu;
T
tensor-tang 已提交
75 76
  config.fraction_of_gpu_memory = 0.15;
  config.device = 0;
T
tensor-tang 已提交
77 78 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
  auto main_predictor =
      CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(config);

  std::vector<std::thread> threads;
  for (int tid = 0; tid < num_threads; ++tid) {
    threads.emplace_back([&, tid]() {
      // 1. clone a predictor which shares the same parameters
      auto predictor = main_predictor->Clone();
      constexpr int num_batches = 3;
      for (int batch_id = 0; batch_id < num_batches; ++batch_id) {
        // 2. Dummy Input Data
        int64_t data[4] = {1, 2, 3, 4};
        PaddleBuf buf{.data = data, .length = sizeof(data)};
        PaddleTensor tensor{.name = "",
                            .shape = std::vector<int>({4, 1}),
                            .data = buf,
                            .dtype = PaddleDType::INT64};
        std::vector<PaddleTensor> inputs(4, tensor);
        std::vector<PaddleTensor> outputs;
        // 3. Run
        CHECK(predictor->Run(inputs, &outputs));

        // 4. Get output.
        ASSERT_EQ(outputs.size(), 1UL);
        LOG(INFO) << "TID: " << tid << ", "
                  << "output buffer size: " << outputs.front().data.length;
        const size_t num_elements = outputs.front().data.length / sizeof(float);
        // The outputs' buffers are in CPU memory.
        for (size_t i = 0; i < std::min(5UL, num_elements); i++) {
          LOG(INFO) << static_cast<float*>(outputs.front().data.data)[i];
        }
T
tensor-tang 已提交
108
        free(outputs[0].data.data);
T
tensor-tang 已提交
109 110 111 112 113 114 115 116
      }
    });
  }
  for (int i = 0; i < num_threads; ++i) {
    threads[i].join();
  }
}

117
TEST(demo, word2vec_cpu) { Main(false /*use_gpu*/); }
T
tensor-tang 已提交
118 119
TEST(demo_multi_threads, word2vec_cpu_1) { MainThreads(1, false /*use_gpu*/); }
TEST(demo_multi_threads, word2vec_cpu_4) { MainThreads(4, false /*use_gpu*/); }
T
tensor-tang 已提交
120 121

#ifdef PADDLE_WITH_CUDA
122
TEST(demo, word2vec_gpu) { Main(true /*use_gpu*/); }
T
tensor-tang 已提交
123 124
TEST(demo_multi_threads, word2vec_gpu_1) { MainThreads(1, true /*use_gpu*/); }
TEST(demo_multi_threads, word2vec_gpu_4) { MainThreads(4, true /*use_gpu*/); }
T
tensor-tang 已提交
125
#endif
126 127 128

}  // namespace demo
}  // namespace paddle