Skip to content

Commit

Permalink
Merge pull request #83 from sguada/net_speed
Browse files Browse the repository at this point in the history
Add benchmarks
  • Loading branch information
shelhamer committed Feb 7, 2014
2 parents 92e76c8 + 9775cc2 commit 47ddd04
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions examples/net_speed_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,35 @@
using namespace caffe;

int main(int argc, char** argv) {
cudaSetDevice(0);
Caffe::set_mode(Caffe::GPU);
Caffe::set_phase(Caffe::TRAIN);
int repeat = 100;

int total_iter = 50;

if (argc < 2) {
LOG(ERROR) << "net_speed_benchmark net_proto [iterations=50] [CPU/GPU] [Device_id=0]";
return 0;
}

if (argc >=3) {
total_iter = atoi(argv[2]);
}

LOG(ERROR) << "Testing for " << total_iter << "Iterations.";

if (argc >= 4 && strcmp(argv[3], "GPU") == 0) {
LOG(ERROR) << "Using GPU";
uint device_id = 0;
if (argc >= 5 && strcmp(argv[3], "GPU") == 0) {
device_id = atoi(argv[4]);
}
LOG(ERROR) << "Using Device_id=" << device_id;
Caffe::SetDevice(device_id);
Caffe::set_mode(Caffe::GPU);
} else {
LOG(ERROR) << "Using CPU";
Caffe::set_mode(Caffe::CPU);
}

Caffe::set_phase(Caffe::TRAIN);
NetParameter net_param;
ReadProtoFromTextFile(argv[1],
&net_param);
Expand All @@ -40,24 +64,29 @@ int main(int argc, char** argv) {
vector<vector<Blob<float>*> >& bottom_vecs = caffe_net.bottom_vecs();
vector<vector<Blob<float>*> >& top_vecs = caffe_net.top_vecs();
LOG(ERROR) << "*** Benchmark begins ***";
clock_t forward_start = clock();
for (int i = 0; i < layers.size(); ++i) {
const string& layername = layers[i]->layer_param().name();
clock_t start = clock();
for (int j = 0; j < repeat; ++j) {
for (int j = 0; j < total_iter; ++j) {
layers[i]->Forward(bottom_vecs[i], &top_vecs[i]);
}
LOG(ERROR) << layername << "\tforward: "
<< float(clock() - start) / CLOCKS_PER_SEC << " seconds.";
}
LOG(ERROR) << "Forward pass: " << float(clock() - forward_start) / CLOCKS_PER_SEC << " seconds.";
clock_t backward_start = clock();
for (int i = layers.size() - 1; i >= 0; --i) {
const string& layername = layers[i]->layer_param().name();
clock_t start = clock();
for (int j = 0; j < repeat; ++j) {
for (int j = 0; j < total_iter; ++j) {
layers[i]->Backward(top_vecs[i], true, &bottom_vecs[i]);
}
LOG(ERROR) << layername << "\tbackward: "
<< float(clock() - start) / CLOCKS_PER_SEC << " seconds.";
}
LOG(ERROR) << "Backward pass: " << float(clock() - backward_start) / CLOCKS_PER_SEC << " seconds.";
LOG(ERROR) << "Total Time: " << float(clock() - forward_start) / CLOCKS_PER_SEC << " seconds.";
LOG(ERROR) << "*** Benchmark ends ***";
return 0;
}

0 comments on commit 47ddd04

Please sign in to comment.