Created
December 4, 2019 00:54
-
-
Save bddppq/96b8f5407e42bc6ec20beed34190562b to your computer and use it in GitHub Desktop.
This file contains 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, 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