File: dispatch.cpp

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (62 lines) | stat: -rw-r--r-- 1,841 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <gtest/gtest.h>

#include <ATen/native/Pow.h>
#include <c10/util/irange.h>
#include <test/cpp/api/support.h>
#include <torch/torch.h>
#include <torch/types.h>
#include <torch/utils.h>
#include <cstdlib>
#include <iostream>
#include <type_traits>
#include <vector>

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>());
  }
}