Created
December 3, 2019 00:08
-
-
Save bddppq/508555897a185ad9a85574eed00b93f6 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> | |
template <int size> | |
class Contiguous : public benchmark::Fixture { | |
void SetUp(const ::benchmark::State& state) { | |
input = torch::randn({size, size}).to(at::kFloat); | |
indexes = torch::randint(0, size, {size / 4}).to(at::kLong); | |
} | |
public: | |
torch::Tensor input; | |
torch::Tensor indexes; | |
}; | |
template <int size> | |
class NonContiguous : public benchmark::Fixture { | |
void SetUp(const ::benchmark::State& state) { | |
input = torch::randn({size, size}).to(at::kFloat).transpose(0, 1); | |
indexes = torch::randint(0, size, {size / 4}).to(at::kLong); | |
} | |
public: | |
torch::Tensor input; | |
torch::Tensor indexes; | |
}; | |
#define BenchSize(size) \ | |
BENCHMARK_TEMPLATE_DEFINE_F(Contiguous, index_##size, size) \ | |
(benchmark::State & state) { \ | |
for (auto _ : state) { \ | |
benchmark::DoNotOptimize(input.index(indexes)); \ | |
} \ | |
} \ | |
BENCHMARK_REGISTER_F(Contiguous, index_##size) \ | |
->Unit(benchmark::kNanosecond); \ | |
BENCHMARK_TEMPLATE_DEFINE_F(Contiguous, index_select_##size, size) \ | |
(benchmark::State & state) { \ | |
for (auto _ : state) { \ | |
benchmark::DoNotOptimize(input.index_select(0, indexes)); \ | |
} \ | |
} \ | |
BENCHMARK_REGISTER_F(Contiguous, index_select_##size) \ | |
->Unit(benchmark::kNanosecond); \ | |
BENCHMARK_TEMPLATE_DEFINE_F(NonContiguous, index_##size, size) \ | |
(benchmark::State & state) { \ | |
for (auto _ : state) { \ | |
benchmark::DoNotOptimize(input.index(indexes)); \ | |
} \ | |
} \ | |
BENCHMARK_REGISTER_F(NonContiguous, index_##size) \ | |
->Unit(benchmark::kNanosecond); \ | |
BENCHMARK_TEMPLATE_DEFINE_F(NonContiguous, index_select_##size, size) \ | |
(benchmark::State & state) { \ | |
for (auto _ : state) { \ | |
benchmark::DoNotOptimize(input.index_select(0, indexes)); \ | |
} \ | |
} \ | |
BENCHMARK_REGISTER_F(NonContiguous, index_select_##size) \ | |
->Unit(benchmark::kNanosecond); | |
BenchSize(64); | |
BenchSize(128); | |
BenchSize(256); | |
BenchSize(512); | |
BenchSize(1024); | |
BenchSize(2048); | |
#undef BenchSize | |
// Run the benchmark | |
BENCHMARK_MAIN(); |
Author
bddppq
commented
Dec 3, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment