MCPcopy Create free account
hub / github.com/BVLC/caffe / time

Function time

tools/caffe.cpp:339–426  ·  view source on GitHub ↗

Time: benchmark the execution time of a model.

Source from the content-addressed store, hash-verified

337
338// Time: benchmark the execution time of a model.
339int time() {
340 CHECK_GT(FLAGS_model.size(), 0) << "Need a model definition to time.";
341 caffe::Phase phase = get_phase_from_flags(caffe::TRAIN);
342 vector<string> stages = get_stages_from_flags();
343
344 // Set device id and mode
345 vector<int> gpus;
346 get_gpus(&gpus);
347 if (gpus.size() != 0) {
348 LOG(INFO) << "Use GPU with device ID " << gpus[0];
349 Caffe::SetDevice(gpus[0]);
350 Caffe::set_mode(Caffe::GPU);
351 } else {
352 LOG(INFO) << "Use CPU.";
353 Caffe::set_mode(Caffe::CPU);
354 }
355 // Instantiate the caffe net.
356 Net<float> caffe_net(FLAGS_model, phase, FLAGS_level, &stages);
357
358 // Do a clean forward and backward pass, so that memory allocation are done
359 // and future iterations will be more stable.
360 LOG(INFO) << "Performing Forward";
361 // Note that for the speed benchmark, we will assume that the network does
362 // not take any input blobs.
363 float initial_loss;
364 caffe_net.Forward(&initial_loss);
365 LOG(INFO) << "Initial loss: " << initial_loss;
366 LOG(INFO) << "Performing Backward";
367 caffe_net.Backward();
368
369 const vector<shared_ptr<Layer<float> > >& layers = caffe_net.layers();
370 const vector<vector<Blob<float>*> >& bottom_vecs = caffe_net.bottom_vecs();
371 const vector<vector<Blob<float>*> >& top_vecs = caffe_net.top_vecs();
372 const vector<vector<bool> >& bottom_need_backward =
373 caffe_net.bottom_need_backward();
374 LOG(INFO) << "*** Benchmark begins ***";
375 LOG(INFO) << "Testing for " << FLAGS_iterations << " iterations.";
376 Timer total_timer;
377 total_timer.Start();
378 Timer forward_timer;
379 Timer backward_timer;
380 Timer timer;
381 std::vector<double> forward_time_per_layer(layers.size(), 0.0);
382 std::vector<double> backward_time_per_layer(layers.size(), 0.0);
383 double forward_time = 0.0;
384 double backward_time = 0.0;
385 for (int j = 0; j < FLAGS_iterations; ++j) {
386 Timer iter_timer;
387 iter_timer.Start();
388 forward_timer.Start();
389 for (int i = 0; i < layers.size(); ++i) {
390 timer.Start();
391 layers[i]->Forward(bottom_vecs[i], top_vecs[i]);
392 forward_time_per_layer[i] += timer.MicroSeconds();
393 }
394 forward_time += forward_timer.MicroSeconds();
395 backward_timer.Start();
396 for (int i = layers.size() - 1; i >= 0; --i) {

Callers 1

cluster_seedgenFunction · 0.50

Calls 10

get_phase_from_flagsFunction · 0.85
get_stages_from_flagsFunction · 0.85
get_gpusFunction · 0.85
ForwardMethod · 0.80
StartMethod · 0.80
MicroSecondsMethod · 0.80
MilliSecondsMethod · 0.80
StopMethod · 0.80
sizeMethod · 0.45
BackwardMethod · 0.45

Tested by

no test coverage detected