pytorch/test/cpp/api/dispatch.cpp
Richard Barnes afb742382a use irange for loops 10 (#69394)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/69394

Modified loops in files under fbsource/fbcode/caffe2/ from the format
```
for(TYPE var=x0;var<x_max;x++)
```
to the format
```
for(const auto var: irange(xmax))
```

This was achieved by running r-barnes's loop upgrader script (D28874212) with some modification to exclude all files under /torch/jit and a number of reversions or unused variable suppression warnings added by hand.

Test Plan: Sandcastle

Reviewed By: malfet

Differential Revision: D32837991

fbshipit-source-id: fc7c4f76d2f32a17a0faf329294b3fe7cb81df32
2021-12-09 09:49:34 -08:00

64 lines
1.8 KiB
C++

#include <gtest/gtest.h>
#include <torch/torch.h>
#include <ATen/native/Pow.h>
#include <c10/util/irange.h>
#include <torch/types.h>
#include <torch/utils.h>
#include <test/cpp/api/support.h>
#include <iostream>
#include <vector>
#include <type_traits>
#include <cstdlib>
struct DispatchTest : torch::test::SeedingFixture {};
TEST_F(DispatchTest, TestAVX2) {
const std::vector<int> ints {1, 2, 3, 4};
const std::vector<int> result {1, 4, 27, 256};
const auto vals_tensor = torch::tensor(ints);
const auto pows_tensor = torch::tensor(ints);
#ifdef _WIN32
_putenv("ATEN_CPU_CAPABILITY=avx2");
#else
setenv("ATEN_CPU_CAPABILITY", "avx2", 1);
#endif
const auto actual_pow_avx2 = vals_tensor.pow(pows_tensor);
for (const auto i : c10::irange(4)) {
ASSERT_EQ(result[i], actual_pow_avx2[i].item<int>());
}
}
TEST_F(DispatchTest, TestAVX512) {
const std::vector<int> ints {1, 2, 3, 4};
const std::vector<int> result {1, 4, 27, 256};
const auto vals_tensor = torch::tensor(ints);
const auto pows_tensor = torch::tensor(ints);
#ifdef _WIN32
_putenv("ATEN_CPU_CAPABILITY=avx512");
#else
setenv("ATEN_CPU_CAPABILITY", "avx512", 1);
#endif
const auto actual_pow_avx512 = vals_tensor.pow(pows_tensor);
for (const auto i : c10::irange(4)) {
ASSERT_EQ(result[i], actual_pow_avx512[i].item<int>());
}
}
TEST_F(DispatchTest, TestDefault) {
const std::vector<int> ints {1, 2, 3, 4};
const std::vector<int> result {1, 4, 27, 256};
const auto vals_tensor = torch::tensor(ints);
const auto pows_tensor = torch::tensor(ints);
#ifdef _WIN32
_putenv("ATEN_CPU_CAPABILITY=default");
#else
setenv("ATEN_CPU_CAPABILITY", "default", 1);
#endif
const auto actual_pow_default = vals_tensor.pow(pows_tensor);
for (const auto i : c10::irange(4)) {
ASSERT_EQ(result[i], actual_pow_default[i].item<int>());
}
}