|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include "paddle/fluid/framework/async_executor.h" |
| 16 | +#include "google/protobuf/io/zero_copy_stream_impl.h" |
| 17 | +#include "google/protobuf/message.h" |
| 18 | +#include "google/protobuf/text_format.h" |
| 19 | + |
| 20 | +#include "gflags/gflags.h" |
| 21 | +#include "paddle/fluid/framework/data_feed_factory.h" |
| 22 | +#include "paddle/fluid/framework/executor_thread_worker.h" |
| 23 | +#include "paddle/fluid/framework/feed_fetch_method.h" |
| 24 | +#include "paddle/fluid/framework/feed_fetch_type.h" |
| 25 | +#include "paddle/fluid/framework/lod_rank_table.h" |
| 26 | +#include "paddle/fluid/framework/lod_tensor_array.h" |
| 27 | +#include "paddle/fluid/framework/op_registry.h" |
| 28 | +#include "paddle/fluid/framework/reader.h" |
| 29 | +#include "paddle/fluid/inference/io.h" |
| 30 | +#include "paddle/fluid/platform/place.h" |
| 31 | +#include "paddle/fluid/pybind/pybind.h" |
| 32 | + |
| 33 | +namespace paddle { |
| 34 | +namespace framework { |
| 35 | +AsyncExecutor::AsyncExecutor(Scope* scope, const platform::Place& place) |
| 36 | + : root_scope_(scope), place_(place) {} |
| 37 | + |
| 38 | +void AsyncExecutor::CreateThreads( |
| 39 | + ExecutorThreadWorker* worker, const ProgramDesc& main_program, |
| 40 | + const std::shared_ptr<DataFeed>& reader, |
| 41 | + const std::vector<std::string>& fetch_var_names, Scope* root_scope, |
| 42 | + const int thread_index, const bool debug) { |
| 43 | + worker->SetThreadId(thread_index); |
| 44 | + worker->SetDebug(debug); |
| 45 | + worker->SetRootScope(root_scope); |
| 46 | + worker->CreateThreadResource(main_program, place_); |
| 47 | + worker->SetDataFeed(reader); |
| 48 | + worker->SetFetchVarNames(fetch_var_names); |
| 49 | + worker->BindingDataFeedMemory(); |
| 50 | +} |
| 51 | + |
| 52 | +void PrepareReaders(std::vector<std::shared_ptr<DataFeed>>& readers, // NOLINT |
| 53 | + const int thread_num, const DataFeedDesc& data_feed_desc, |
| 54 | + const std::vector<std::string>& filelist) { |
| 55 | + readers.resize(thread_num); |
| 56 | + for (size_t i = 0; i < readers.size(); ++i) { |
| 57 | + readers[i] = DataFeedFactory::CreateDataFeed(data_feed_desc.name()); |
| 58 | + readers[i]->Init(data_feed_desc); // set batch_size and queue_size here |
| 59 | + } |
| 60 | + readers[0]->SetFileList(filelist); |
| 61 | +} |
| 62 | + |
| 63 | +void AsyncExecutor::RunFromFile(const ProgramDesc& main_program, |
| 64 | + const std::string& data_feed_desc_str, |
| 65 | + const std::vector<std::string>& filelist, |
| 66 | + const int thread_num, |
| 67 | + const std::vector<std::string>& fetch_var_names, |
| 68 | + const bool debug) { |
| 69 | + std::vector<std::thread> threads; |
| 70 | + |
| 71 | + auto& block = main_program.Block(0); |
| 72 | + for (auto var_name : fetch_var_names) { |
| 73 | + auto var_desc = block.FindVar(var_name); |
| 74 | + auto shapes = var_desc->GetShape(); |
| 75 | + PADDLE_ENFORCE(shapes[shapes.size() - 1] == 1, |
| 76 | + "var %s: Fetched var has wrong shape, " |
| 77 | + "only variables with the last dimension size 1 supported", |
| 78 | + var_name); |
| 79 | + } |
| 80 | + |
| 81 | + DataFeedDesc data_feed_desc; |
| 82 | + google::protobuf::TextFormat::ParseFromString(data_feed_desc_str, |
| 83 | + &data_feed_desc); |
| 84 | + |
| 85 | + int actual_thread_num = thread_num; |
| 86 | + int file_cnt = filelist.size(); |
| 87 | + PADDLE_ENFORCE(file_cnt > 0, "File list cannot be empty"); |
| 88 | + |
| 89 | + if (actual_thread_num > file_cnt) { |
| 90 | + VLOG(1) << "Thread num = " << thread_num << ", file num = " << file_cnt |
| 91 | + << ". Changing thread_num = " << file_cnt; |
| 92 | + actual_thread_num = file_cnt; |
| 93 | + } |
| 94 | + |
| 95 | + /* |
| 96 | + readerDesc: protobuf description for reader initlization |
| 97 | + argument: class_name, batch_size, use_slot, queue_size, buffer_size, |
| 98 | + padding_index |
| 99 | +
|
| 100 | + reader: |
| 101 | + 1) each thread has a reader, reader will read input data and |
| 102 | + put it into input queue |
| 103 | + 2) each reader has a Next() iterface, that can fetch an instance |
| 104 | + from the input queue |
| 105 | + */ |
| 106 | + // todo: should be factory method for creating datafeed |
| 107 | + std::vector<std::shared_ptr<DataFeed>> readers; |
| 108 | + PrepareReaders(readers, actual_thread_num, data_feed_desc, filelist); |
| 109 | + |
| 110 | + std::vector<std::shared_ptr<ExecutorThreadWorker>> workers; |
| 111 | + workers.resize(actual_thread_num); |
| 112 | + for (auto& worker : workers) { |
| 113 | + worker.reset(new ExecutorThreadWorker); |
| 114 | + } |
| 115 | + |
| 116 | + // prepare thread resource here |
| 117 | + for (int thidx = 0; thidx < actual_thread_num; ++thidx) { |
| 118 | + CreateThreads(workers[thidx].get(), main_program, readers[thidx], |
| 119 | + fetch_var_names, root_scope_, thidx, debug); |
| 120 | + } |
| 121 | + |
| 122 | + // start executing ops in multiple threads |
| 123 | + for (int thidx = 0; thidx < actual_thread_num; ++thidx) { |
| 124 | + threads.push_back( |
| 125 | + std::thread(&ExecutorThreadWorker::TrainFiles, workers[thidx].get())); |
| 126 | + } |
| 127 | + |
| 128 | + for (auto& th : threads) { |
| 129 | + th.join(); |
| 130 | + } |
| 131 | + |
| 132 | + root_scope_->DropKids(); |
| 133 | + |
| 134 | + return; |
| 135 | +} |
| 136 | + |
| 137 | +} // einit_modelnd namespace framework |
| 138 | +} // end namespace paddle |
0 commit comments