| 174 | |
| 175 | template <typename Place, bool CreateVars = true, bool PrepareContext = false> |
| 176 | void TestInference( |
| 177 | const std::string& dirname, |
| 178 | const std::vector<phi::DenseTensor*>& cpu_feeds, |
| 179 | const std::vector<paddle::framework::FetchType*>& cpu_fetches, |
| 180 | const int repeat = 1, |
| 181 | const bool is_combined = false) { |
| 182 | // 1. Define place, executor, scope |
| 183 | auto place = Place(); |
| 184 | auto executor = paddle::framework::Executor(place); |
| 185 | auto* scope = new paddle::framework::Scope(); |
| 186 | |
| 187 | // Profile the performance |
| 188 | paddle::platform::ProfilerState state; |
| 189 | if (phi::is_cpu_place(place)) { |
| 190 | state = paddle::platform::ProfilerState::kCPU; |
| 191 | } else { |
| 192 | #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) |
| 193 | state = paddle::platform::ProfilerState::kAll; |
| 194 | // The default device_id of phi::GPUPlace is 0. |
| 195 | // Users can get the device_id using: |
| 196 | // int device_id = place.GetDeviceId(); |
| 197 | paddle::platform::SetDeviceId(0); |
| 198 | #else |
| 199 | PADDLE_THROW(common::errors::Unavailable( |
| 200 | "'CUDAPlace' is not supported in CPU only device.")); |
| 201 | #endif |
| 202 | } |
| 203 | |
| 204 | // 2. Initialize the inference_program and load parameters |
| 205 | std::unique_ptr<paddle::framework::ProgramDesc> inference_program; |
| 206 | |
| 207 | // Enable the profiler |
| 208 | paddle::platform::EnableProfiler(state); |
| 209 | { |
| 210 | phi::RecordEvent record_event("init_program"); |
| 211 | inference_program = InitProgram(&executor, scope, dirname, is_combined); |
| 212 | } |
| 213 | |
| 214 | // Disable the profiler and print the timing information |
| 215 | paddle::platform::DisableProfiler(paddle::platform::EventSortingKey::kDefault, |
| 216 | "load_program_profiler"); |
| 217 | paddle::platform::ResetProfiler(); |
| 218 | |
| 219 | // 3. Get the feed_target_names and fetch_target_names |
| 220 | const std::vector<std::string>& feed_target_names = |
| 221 | inference_program->GetFeedTargetNames(); |
| 222 | const std::vector<std::string>& fetch_target_names = |
| 223 | inference_program->GetFetchTargetNames(); |
| 224 | |
| 225 | // 4. Prepare inputs: set up maps for feed targets |
| 226 | std::map<std::string, const phi::DenseTensor*> feed_targets; |
| 227 | for (size_t i = 0; i < feed_target_names.size(); ++i) { |
| 228 | // Please make sure that cpu_feeds[i] is right for feed_target_names[i] |
| 229 | feed_targets[feed_target_names[i]] = cpu_feeds[i]; |
| 230 | } |
| 231 | |
| 232 | // 5. Define Tensor to get the outputs: set up maps for fetch targets |
| 233 | std::map<std::string, paddle::framework::FetchType*> fetch_targets; |
nothing calls this directly
no test coverage detected