|
| 1 | +/** |
| 2 | + * Copyright (c) 2016-present, Facebook, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <string> |
| 18 | + |
| 19 | +#include "binaries/benchmark_helper.h" |
| 20 | +#include "caffe2/core/blob_serialization.h" |
| 21 | +#ifdef __CUDA_ARCH__ |
| 22 | +#include "caffe2/core/context_gpu.h" |
| 23 | +#endif |
| 24 | +#include "caffe2/core/init.h" |
| 25 | +#include "caffe2/core/logging.h" |
| 26 | +#include "caffe2/core/net.h" |
| 27 | +#include "caffe2/core/operator.h" |
| 28 | +#include "caffe2/utils/string_utils.h" |
| 29 | +#include "observers/net_observer_reporter_print.h" |
| 30 | +#include "observers/observer_config.h" |
| 31 | +#include "observers/perf_observer.h" |
| 32 | + |
| 33 | +using std::shared_ptr; |
| 34 | +using std::string; |
| 35 | +using std::unique_ptr; |
| 36 | +using std::vector; |
| 37 | + |
| 38 | +void observerConfig() { |
| 39 | + caffe2::ClearGlobalNetObservers(); |
| 40 | + caffe2::AddGlobalNetObserverCreator([](caffe2::NetBase* subject) { |
| 41 | + return caffe2::make_unique<caffe2::PerfNetObserver>(subject); |
| 42 | + }); |
| 43 | + caffe2::ObserverConfig::setReporter( |
| 44 | + caffe2::make_unique<caffe2::NetObserverReporterPrint>()); |
| 45 | +} |
| 46 | + |
| 47 | +bool backendCudaSet(const string& backend) { |
| 48 | + bool run_on_gpu = false; |
| 49 | + if (backend == "cuda") { |
| 50 | +#ifdef __CUDA_ARCH__ |
| 51 | + if (caffe2::HasCudaGPU()) { |
| 52 | + run_on_gpu = true; |
| 53 | + } else { |
| 54 | + CAFFE_THROW("NO GPU support on this host machine"); |
| 55 | + } |
| 56 | +#else |
| 57 | + CAFFE_THROW("NO GPU support"); |
| 58 | +#endif |
| 59 | + } |
| 60 | + return run_on_gpu; |
| 61 | +} |
| 62 | + |
| 63 | +void setDeviceType(caffe2::NetDef* net_def, caffe2::DeviceType& run_dev) { |
| 64 | + for (int j = 0; j < net_def->op_size(); j++) { |
| 65 | + caffe2::OperatorDef* op = net_def->mutable_op(j); |
| 66 | + op->mutable_device_option()->set_device_type(run_dev); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +void setOperatorEngine(caffe2::NetDef* net_def, const string& backend) { |
| 71 | + if (backend != "builtin") { |
| 72 | + string engine = backend == "nnpack" ? "NNPACK" |
| 73 | + : backend == "eigen" ? "EIGEN" |
| 74 | + : backend == "mkl" |
| 75 | + ? "MKLDNN" |
| 76 | + : backend == "cuda" ? "CUDA" |
| 77 | + : backend == "default" ? "" : "NONE"; |
| 78 | + CAFFE_ENFORCE(engine != "NONE", "Backend is not supported"); |
| 79 | + for (int i = 0; i < net_def->op_size(); i++) { |
| 80 | + caffe2::OperatorDef* op_def = net_def->mutable_op(i); |
| 81 | + op_def->set_engine(engine); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +void loadInput( |
| 87 | + shared_ptr<caffe2::Workspace> workspace, |
| 88 | + const bool run_on_gpu, |
| 89 | + const string& input, |
| 90 | + const string& input_file, |
| 91 | + const string& input_dims, |
| 92 | + const string& input_type) { |
| 93 | + // Load input. |
| 94 | + if (input.size()) { |
| 95 | + vector<string> input_names = caffe2::split(',', input); |
| 96 | + if (input_file.size()) { |
| 97 | + vector<string> input_files = caffe2::split(',', input_file); |
| 98 | + CAFFE_ENFORCE_EQ( |
| 99 | + input_names.size(), |
| 100 | + input_files.size(), |
| 101 | + "Input name and file should have the same number."); |
| 102 | + for (int i = 0; i < input_names.size(); ++i) { |
| 103 | + caffe2::BlobProto blob_proto; |
| 104 | + CAFFE_ENFORCE(caffe2::ReadProtoFromFile(input_files[i], &blob_proto)); |
| 105 | + workspace->CreateBlob(input_names[i])->Deserialize(blob_proto); |
| 106 | + } |
| 107 | + } else if (input_dims.size() || input_type.size()) { |
| 108 | + CAFFE_ENFORCE_GE( |
| 109 | + input_dims.size(), |
| 110 | + 0, |
| 111 | + "Input dims must be specified when input tensors are used."); |
| 112 | + CAFFE_ENFORCE_GE( |
| 113 | + input_type.size(), |
| 114 | + 0, |
| 115 | + "Input type must be specified when input tensors are used."); |
| 116 | + |
| 117 | + vector<string> input_dims_list = caffe2::split(';', input_dims); |
| 118 | + CAFFE_ENFORCE_EQ( |
| 119 | + input_names.size(), |
| 120 | + input_dims_list.size(), |
| 121 | + "Input name and dims should have the same number of items."); |
| 122 | + vector<string> input_type_list = caffe2::split(';', input_type); |
| 123 | + CAFFE_ENFORCE_EQ( |
| 124 | + input_names.size(), |
| 125 | + input_type_list.size(), |
| 126 | + "Input name and type should have the same number of items."); |
| 127 | + for (size_t i = 0; i < input_names.size(); ++i) { |
| 128 | + vector<string> input_dims_str = caffe2::split(',', input_dims_list[i]); |
| 129 | + vector<int> input_dims; |
| 130 | + for (const string& s : input_dims_str) { |
| 131 | + input_dims.push_back(caffe2::stoi(s)); |
| 132 | + } |
| 133 | + caffe2::Blob* blob = workspace->GetBlob(input_names[i]); |
| 134 | + if (blob == nullptr) { |
| 135 | + blob = workspace->CreateBlob(input_names[i]); |
| 136 | + } |
| 137 | + if (run_on_gpu) { |
| 138 | + LOG(INFO) << "Running on GPU."; |
| 139 | +#ifdef __CUDA_ARCH__ |
| 140 | + caffe2::TensorCUDA* tensor = blob->GetMutable<caffe2::TensorCUDA>(); |
| 141 | + CHECK_NOTNULL(tensor); |
| 142 | + tensor->Resize(input_dims); |
| 143 | + if (input_type_list[i] == "uint8_t") { |
| 144 | + tensor->mutable_data<uint8_t>(); |
| 145 | + } else if (input_type_list[i] == "float") { |
| 146 | + tensor->mutable_data<float>(); |
| 147 | + } else { |
| 148 | + CAFFE_THROW("Unsupported input type: ", input_type_list[i]); |
| 149 | + } |
| 150 | +#else |
| 151 | + CAFFE_THROW("Not support GPU on mobile."); |
| 152 | +#endif |
| 153 | + } else { |
| 154 | + caffe2::TensorCPU* tensor = blob->GetMutable<caffe2::TensorCPU>(); |
| 155 | + CHECK_NOTNULL(tensor); |
| 156 | + tensor->Resize(input_dims); |
| 157 | + if (input_type_list[i] == "uint8_t") { |
| 158 | + tensor->mutable_data<uint8_t>(); |
| 159 | + } else if (input_type_list[i] == "float") { |
| 160 | + tensor->mutable_data<float>(); |
| 161 | + } else { |
| 162 | + CAFFE_THROW("Unsupported input type: ", input_type_list[i]); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + } else { |
| 167 | + CAFFE_THROW( |
| 168 | + "You requested input tensors, but neither input_file nor " |
| 169 | + "input_dims is set."); |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +void runNetwork( |
| 175 | + shared_ptr<caffe2::Workspace> workspace, |
| 176 | + caffe2::NetDef& net_def, |
| 177 | + const bool run_individual, |
| 178 | + const int warmup, |
| 179 | + const int iter) { |
| 180 | + if (!net_def.has_name()) { |
| 181 | + net_def.set_name("benchmark"); |
| 182 | + } |
| 183 | + |
| 184 | + caffe2::NetBase* net = workspace->CreateNet(net_def); |
| 185 | + CHECK_NOTNULL(net); |
| 186 | + |
| 187 | + LOG(INFO) << "Starting benchmark."; |
| 188 | + caffe2::ObserverConfig::initSampleRate(1, 1, 1, run_individual, warmup); |
| 189 | + LOG(INFO) << "Running warmup runs."; |
| 190 | + for (int i = 0; i < warmup; ++i) { |
| 191 | + CAFFE_ENFORCE(net->Run(), "Warmup run ", i, " has failed."); |
| 192 | + } |
| 193 | + |
| 194 | + LOG(INFO) << "Main runs."; |
| 195 | + CAFFE_ENFORCE( |
| 196 | + iter >= 0, |
| 197 | + "Number of main runs should be non negative, provided ", |
| 198 | + iter, |
| 199 | + "."); |
| 200 | + for (int i = 0; i < iter; ++i) { |
| 201 | + caffe2::ObserverConfig::initSampleRate(1, 1, 1, 0, warmup); |
| 202 | + CAFFE_ENFORCE(net->Run(), "Main run ", i, " has failed."); |
| 203 | + if (run_individual) { |
| 204 | + caffe2::ObserverConfig::initSampleRate(1, 1, 1, 1, warmup); |
| 205 | + CAFFE_ENFORCE(net->Run(), "Main run ", i, " with operator has failed."); |
| 206 | + } |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +void writeOutput( |
| 211 | + shared_ptr<caffe2::Workspace> workspace, |
| 212 | + const bool run_on_gpu, |
| 213 | + const string& output, |
| 214 | + const string& output_folder, |
| 215 | + const bool text_output) { |
| 216 | + string output_prefix = output_folder.size() ? output_folder + "/" : ""; |
| 217 | + if (output.size()) { |
| 218 | + vector<string> output_names = caffe2::split(',', output); |
| 219 | + if (output == "*") { |
| 220 | + output_names = workspace->Blobs(); |
| 221 | + } |
| 222 | + for (const string& name : output_names) { |
| 223 | + CAFFE_ENFORCE( |
| 224 | + workspace->HasBlob(name), |
| 225 | + "You requested a non-existing blob: ", |
| 226 | + name); |
| 227 | + if (text_output) { |
| 228 | + if (run_on_gpu) { |
| 229 | +#ifdef __CUDA_ARCH__ |
| 230 | + writeTextOutput<caffe2::CUDAContext, caffe2::TensorCUDA>( |
| 231 | + workspace->GetBlob(name)->GetMutable<caffe2::TensorCUDA>(), |
| 232 | + output_prefix, |
| 233 | + name); |
| 234 | +#else |
| 235 | + CAFFE_THROW("Not support GPU."); |
| 236 | +#endif |
| 237 | + } else { |
| 238 | + writeTextOutput<caffe2::CPUContext, caffe2::TensorCPU>( |
| 239 | + workspace->GetBlob(name)->GetMutable<caffe2::TensorCPU>(), |
| 240 | + output_prefix, |
| 241 | + name); |
| 242 | + } |
| 243 | + } else { |
| 244 | + string serialized = workspace->GetBlob(name)->Serialize(name); |
| 245 | + string output_filename = output_prefix + name; |
| 246 | + caffe2::WriteStringToFile(serialized, output_filename.c_str()); |
| 247 | + } |
| 248 | + } |
| 249 | + } |
| 250 | +} |
0 commit comments