Created
December 4, 2019 01:51
-
-
Save bddppq/216fc8abfa8b68fa6b861974e977ded6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <benchmark/benchmark.h> | |
#include <torch/torch.h> | |
namespace { | |
template <int row, int col> | |
class Contiguous : public benchmark::Fixture { | |
void SetUp(const ::benchmark::State& /* unused */) override { | |
input = torch::randn({row, col}).to(at::kFloat); | |
indexes = torch::randint(0, row, {row / 4}).to(at::kLong); | |
} | |
public: | |
torch::Tensor input; | |
torch::Tensor indexes; | |
}; | |
template <int row, int col> | |
class NonContiguous : public benchmark::Fixture { | |
void SetUp(const ::benchmark::State& /* unused */) override { | |
input = torch::randn({col, row}).to(at::kFloat).transpose(0, 1); | |
indexes = torch::randint(0, row, {row / 4}).to(at::kLong); | |
} | |
public: | |
torch::Tensor input; | |
torch::Tensor indexes; | |
}; | |
} // namespace | |
#define BenchSize(row, col) \ | |
BENCHMARK_TEMPLATE_F(Contiguous, index_##row##x##col, row, col) \ | |
(benchmark::State & state) { \ | |
for (auto _ : state) { \ | |
benchmark::DoNotOptimize(input.index(indexes)); \ | |
} \ | |
} \ | |
BENCHMARK_TEMPLATE_F(Contiguous, index_select_##row##x##col, row, col) \ | |
(benchmark::State & state) { \ | |
for (auto _ : state) { \ | |
benchmark::DoNotOptimize(input.index_select(0, indexes)); \ | |
} \ | |
} \ | |
BENCHMARK_TEMPLATE_F(NonContiguous, index_##row##x##col, row, col) \ | |
(benchmark::State & state) { \ | |
for (auto _ : state) { \ | |
benchmark::DoNotOptimize(input.index(indexes)); \ | |
} \ | |
} \ | |
BENCHMARK_TEMPLATE_F(NonContiguous, index_select_##row##x##col, row, col) \ | |
(benchmark::State & state) { \ | |
for (auto _ : state) { \ | |
benchmark::DoNotOptimize(input.index_select(0, indexes)); \ | |
} \ | |
} | |
BenchSize(64, 16); | |
BenchSize(128, 16); | |
BenchSize(256, 16); | |
BenchSize(512, 16); | |
BenchSize(1024, 16); | |
BenchSize(2048, 16); | |
BenchSize(64, 32); | |
BenchSize(128, 32); | |
BenchSize(256, 32); | |
BenchSize(512, 32); | |
BenchSize(1024, 32); | |
BenchSize(2048, 32); | |
BenchSize(64, 64); | |
BenchSize(128, 64); | |
BenchSize(256, 64); | |
BenchSize(512, 64); | |
BenchSize(1024, 64); | |
BenchSize(2048, 64); | |
BenchSize(64, 65); | |
BenchSize(128, 65); | |
BenchSize(256, 65); | |
BenchSize(512, 65); | |
BenchSize(1024, 65); | |
BenchSize(2048, 65); | |
BenchSize(64, 80); | |
BenchSize(128, 80); | |
BenchSize(256, 80); | |
BenchSize(512, 80); | |
BenchSize(1024, 80); | |
BenchSize(2048, 80); | |
BenchSize(64, 90); | |
BenchSize(128, 90); | |
BenchSize(256, 90); | |
BenchSize(512, 90); | |
BenchSize(1024, 90); | |
BenchSize(2048, 90); | |
BenchSize(64, 100); | |
BenchSize(128, 100); | |
BenchSize(256, 100); | |
BenchSize(512, 100); | |
BenchSize(1024, 100); | |
BenchSize(2048, 100); | |
BenchSize(64, 128); | |
BenchSize(128, 128); | |
BenchSize(256, 128); | |
BenchSize(512, 128); | |
BenchSize(1024, 128); | |
BenchSize(2048, 128); | |
BenchSize(64, 256); | |
BenchSize(128, 256); | |
BenchSize(256, 256); | |
BenchSize(512, 256); | |
BenchSize(1024, 256); | |
BenchSize(2048, 256); | |
#undef BenchSize | |
// Run the benchmark | |
BENCHMARK_MAIN(); |
Author
bddppq
commented
Dec 4, 2019
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment