Skip to content

Commit

Permalink
Rename namespace vec256 to vec, struct Vec256 to Vectorized (…
Browse files Browse the repository at this point in the history
…and other related classes/structs) (pytorch#58438)

Summary:
In order to make it more convenient for maintainers to review the ATen AVX512 implementation, the namespace `vec256` is being renamed to `vec` in this PR, as modifying 77 files & creating 2 new files only took a few minutes, as these changes aren't significant, so fewer files would've to be reviewed while reviewing pytorch#56992.
The struct `Vec256` is not being renamed to `Vec`, but `Vectorized` instead, because there are some `using Vec=` statements in the codebase, so renaming it to `Vectorized` was more convenient. However, I can still rename it to `Vec`, if required.

### Changes made in this PR -
Created `aten/src/ATen/cpu/vec` with subdirectory `vec256` (vec512 would be added via pytorch#56992).
The changes were made in this manner -

1. First, a script was run to rename `vec256` to `vec` & `Vec` to `Vectorized` -
```
# Ref: https://stackoverflow.com/a/20721292
cd aten/src
grep -rli 'vec256\/vec256\.h' * | xargs -i@ sed -i 's/vec256\/vec256\.h/vec\/vec\.h/g' @
grep -rli 'vec256\/functional\.h' * | xargs -i@ sed -i 's/vec256\/functional\.h/vec\/functional\.h/g' @
grep -rli 'vec256\/intrinsics\.h' * | xargs -i@ sed -i 's/vec256\/intrinsics\.h/vec\/vec256\/intrinsics\.h/g' @
grep -rli 'namespace vec256' * | xargs -i@ sed -i 's/namespace vec256/namespace vec/g' @
grep -rli 'Vec256' * | xargs -i@ sed -i 's/Vec256/Vectorized/g' @
grep -rli 'vec256\:\:' * | xargs -i@ sed -i 's/vec256\:\:/vec\:\:/g' @
grep -rli 'at\:\:vec256' * | xargs -i@ sed -i 's/at\:\:vec256/at\:\:vec/g' @
cd ATen/cpu
mkdir vec
mv vec256 vec
cd vec/vec256
grep -rli 'cpu\/vec256\/' * | xargs -i@ sed -i 's/cpu\/vec256\//cpu\/vec\/vec256\//g' @
grep -rli 'vec\/vec\.h' * | xargs -i@ sed -i 's/vec\/vec\.h/vec\/vec256\.h/g' @
```

2. `vec256` & `VEC256` were replaced with `vec` & `VEC` respectively in 4 CMake files.

3. In `pytorch_vec/aten/src/ATen/test/`, `vec256_test_all_types.h` & `vec256_test_all_types.cpp` were renamed.

4. `pytorch_vec/aten/src/ATen/cpu/vec/vec.h` & `pytorch_vec/aten/src/ATen/cpu/vec/functional.h` were created.
Both currently have one line each & would have 5 when AVX512 support would be added for ATen.

Pull Request resolved: pytorch#58438

Reviewed By: malfet

Differential Revision: D28509615

Pulled By: ezyang

fbshipit-source-id: 63840df5f23b3b59e203d25816e2977c6a901780
  • Loading branch information
imaginary-person authored and facebook-github-bot committed May 19, 2021
1 parent a6b358d commit 47c566e
Show file tree
Hide file tree
Showing 79 changed files with 3,394 additions and 3,391 deletions.
2 changes: 1 addition & 1 deletion aten/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ set(ATen_HIP_TEST_SRCS ${ATen_HIP_TEST_SRCS} PARENT_SCOPE)
set(ATen_VULKAN_TEST_SRCS ${ATen_VULKAN_TEST_SRCS} PARENT_SCOPE)
set(ATen_MOBILE_BENCHMARK_SRCS ${ATen_MOBILE_BENCHMARK_SRCS} PARENT_SCOPE)
set(ATen_MOBILE_TEST_SRCS ${ATen_MOBILE_TEST_SRCS} PARENT_SCOPE)
set(ATen_VEC256_TEST_SRCS ${ATen_VEC256_TEST_SRCS} PARENT_SCOPE)
set(ATen_VEC_TEST_SRCS ${ATen_VEC_TEST_SRCS} PARENT_SCOPE)
set(ATen_CPU_INCLUDE ${ATen_CPU_INCLUDE} PARENT_SCOPE)
set(ATen_CUDA_INCLUDE ${ATen_CUDA_INCLUDE} PARENT_SCOPE)
set(ATen_HIP_INCLUDE ${ATen_HIP_INCLUDE} PARENT_SCOPE)
Expand Down
4 changes: 2 additions & 2 deletions aten/src/ATen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ if(NOT BUILD_LITE_INTERPRETER)
endif()
EXCLUDE(ATen_CORE_SRCS "${ATen_CORE_SRCS}" ${ATen_CORE_TEST_SRCS})

file(GLOB base_h "*.h" "detail/*.h" "cpu/*.h" "cpu/vec256/*.h" "quantized/*.h")
file(GLOB base_h "*.h" "detail/*.h" "cpu/*.h" "cpu/vec/vec256/*.h" "cpu/vec/*.h" "quantized/*.h")
file(GLOB base_cpp "*.cpp" "detail/*.cpp" "cpu/*.cpp")
file(GLOB cuda_h "cuda/*.h" "cuda/detail/*.h" "cuda/*.cuh" "cuda/detail/*.cuh")
file(GLOB cuda_cpp "cuda/*.cpp" "cuda/detail/*.cpp")
Expand Down Expand Up @@ -497,7 +497,7 @@ set(ATen_HIP_TEST_SRCS ${ATen_HIP_TEST_SRCS} PARENT_SCOPE)
set(ATen_VULKAN_TEST_SRCS ${ATen_VULKAN_TEST_SRCS} PARENT_SCOPE)
set(ATen_MOBILE_BENCHMARK_SRCS ${ATen_MOBILE_BENCHMARK_SRCS} PARENT_SCOPE)
set(ATen_MOBILE_TEST_SRCS ${ATen_MOBILE_TEST_SRCS} ${ATen_VULKAN_TEST_SRCS} PARENT_SCOPE)
set(ATen_VEC256_TEST_SRCS ${ATen_VEC256_TEST_SRCS} PARENT_SCOPE)
set(ATen_VEC_TEST_SRCS ${ATen_VEC_TEST_SRCS} PARENT_SCOPE)
set(ATen_QUANTIZED_TEST_SRCS ${ATen_QUANTIZED_TEST_SRCS} PARENT_SCOPE)
set(ATen_CPU_INCLUDE ${ATen_CPU_INCLUDE} PARENT_SCOPE)
set(ATen_THIRD_PARTY_INCLUDE ${ATen_THIRD_PARTY_INCLUDE} PARENT_SCOPE)
Expand Down
2 changes: 1 addition & 1 deletion aten/src/ATen/cpu/FlushDenormal.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <ATen/cpu/FlushDenormal.h>

#include <ATen/cpu/vec256/intrinsics.h>
#include <ATen/cpu/vec/vec256/intrinsics.h>
#include <cpuinfo.h>

namespace at { namespace cpu {
Expand Down
1 change: 1 addition & 0 deletions aten/src/ATen/cpu/vec/functional.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include <ATen/cpu/vec/vec256/functional.h>
1 change: 1 addition & 0 deletions aten/src/ATen/cpu/vec/vec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include <ATen/cpu/vec/vec256/vec256.h>
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
// DO NOT DEFINE STATIC DATA IN THIS HEADER!
// See Note [Do not compile initializers with AVX]

#include <ATen/cpu/vec256/vec256.h>
#include <ATen/cpu/vec/vec256/vec256.h>

namespace at { namespace vec256 {
namespace at { namespace vec {

// TODO: Make this more efficient
template <typename scalar_t, typename Op>
inline scalar_t vec_reduce_all(
const Op& vec_fun,
vec256::Vec256<scalar_t> acc_vec,
vec::Vectorized<scalar_t> acc_vec,
int64_t size) {
using Vec = vec256::Vec256<scalar_t>;
using Vec = vec::Vectorized<scalar_t>;
scalar_t acc_arr[Vec::size()];
acc_vec.store(acc_arr);
for (int64_t i = 1; i < size; i++) {
Expand All @@ -28,7 +28,7 @@ inline scalar_t vec_reduce_all(

template <typename scalar_t, typename Op>
inline scalar_t reduce_all(const Op& vec_fun, const scalar_t* data, int64_t size) {
using Vec = vec256::Vec256<scalar_t>;
using Vec = vec::Vectorized<scalar_t>;
if (size < Vec::size())
return vec_reduce_all(vec_fun, Vec::loadu(data, size), size);
int64_t d = Vec::size();
Expand All @@ -48,7 +48,7 @@ inline scalar_t reduce_all(const Op& vec_fun, const scalar_t* data, int64_t size
template <typename scalar_t, typename Op1, typename Op2>
inline std::pair<scalar_t, scalar_t> reduce2_all(const Op1& vec_fun1, const Op2& vec_fun2,
const scalar_t* data, int64_t size) {
using Vec = vec256::Vec256<scalar_t>;
using Vec = vec::Vectorized<scalar_t>;
if (size < Vec::size()) {
auto loaded_data = Vec::loadu(data, size);
return std::pair<scalar_t, scalar_t>(
Expand Down Expand Up @@ -79,7 +79,7 @@ inline scalar_t map_reduce_all(
const ReduceOp& red_fun,
scalar_t* data,
int64_t size) {
using Vec = vec256::Vec256<scalar_t>;
using Vec = vec::Vectorized<scalar_t>;
if (size < Vec::size())
return vec_reduce_all(red_fun, map_fun(Vec::loadu(data, size)), size);
int64_t d = Vec::size();
Expand All @@ -104,7 +104,7 @@ inline scalar_t map2_reduce_all(
const scalar_t* data,
const scalar_t* data2,
int64_t size) {
using Vec = vec256::Vec256<scalar_t>;
using Vec = vec::Vectorized<scalar_t>;
if (size < Vec::size()) {
Vec data_vec = Vec::loadu(data, size);
Vec data2_vec = Vec::loadu(data2, size);
Expand Down Expand Up @@ -136,7 +136,7 @@ inline scalar_t map3_reduce_all(
const scalar_t* data2,
const scalar_t* data3,
int64_t size) {
using Vec = vec256::Vec256<scalar_t>;
using Vec = vec::Vectorized<scalar_t>;
if (size < Vec::size()) {
Vec data_vec = Vec::loadu(data, size);
Vec data2_vec = Vec::loadu(data2, size);
Expand Down Expand Up @@ -170,7 +170,7 @@ inline void map(
scalar_t* output_data,
const scalar_t* input_data,
int64_t size) {
using Vec = vec256::Vec256<scalar_t>;
using Vec = vec::Vectorized<scalar_t>;
int64_t d = 0;
for (; d < size - (size % Vec::size()); d += Vec::size()) {
Vec output_vec = vec_fun(Vec::loadu(input_data + d));
Expand All @@ -189,7 +189,7 @@ inline void map2(
const scalar_t* input_data,
const scalar_t* input_data2,
int64_t size) {
using Vec = vec256::Vec256<scalar_t>;
using Vec = vec::Vectorized<scalar_t>;
int64_t d = 0;
for (; d < size - (size % Vec::size()); d += Vec::size()) {
Vec data_vec = Vec::loadu(input_data + d);
Expand All @@ -213,7 +213,7 @@ inline void map3(
const scalar_t* input_data2,
const scalar_t* input_data3,
int64_t size) {
using Vec = vec256::Vec256<scalar_t>;
using Vec = vec::Vectorized<scalar_t>;
int64_t d = 0;
for (; d < size - (size % Vec::size()); d += Vec::size()) {
Vec data_vec1 = Vec::loadu(input_data1 + d);
Expand All @@ -231,4 +231,4 @@ inline void map3(
}
}

}} // namespace at::vec256
}} // namespace at::vec
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
/* GCC-compatible compiler, targeting ARM with NEON */
#include <arm_neon.h>
#if defined (MISSING_ARM_VLD1)
#include <ATen/cpu/vec256/missing_vld1_neon.h>
#include <ATen/cpu/vec/vec256/missing_vld1_neon.h>
#elif defined (MISSING_ARM_VST1)
#include <ATen/cpu/vec256/missing_vst1_neon.h>
#include <ATen/cpu/vec/vec256/missing_vst1_neon.h>
#endif
#elif defined(__GNUC__) && defined(__IWMMXT__)
/* GCC-compatible compiler, targeting ARM with WMMX */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
// DO NOT DEFINE STATIC DATA IN THIS HEADER!
// See Note [Do not compile initializers with AVX]

#include <ATen/cpu/vec256/intrinsics.h>
#include <ATen/cpu/vec/vec256/intrinsics.h>

#include <ATen/cpu/vec256/vec256_base.h>
#include <ATen/cpu/vec/vec256/vec256_base.h>
#if !defined(__VSX__) || !defined(CPU_CAPABILITY_VSX)
#include <ATen/cpu/vec256/vec256_float.h>
#include <ATen/cpu/vec256/vec256_float_neon.h>
#include <ATen/cpu/vec256/vec256_bfloat16.h>
#include <ATen/cpu/vec256/vec256_double.h>
#include <ATen/cpu/vec256/vec256_int.h>
#include <ATen/cpu/vec256/vec256_qint.h>
#include <ATen/cpu/vec256/vec256_complex_float.h>
#include <ATen/cpu/vec256/vec256_complex_double.h>
#include <ATen/cpu/vec/vec256/vec256_float.h>
#include <ATen/cpu/vec/vec256/vec256_float_neon.h>
#include <ATen/cpu/vec/vec256/vec256_bfloat16.h>
#include <ATen/cpu/vec/vec256/vec256_double.h>
#include <ATen/cpu/vec/vec256/vec256_int.h>
#include <ATen/cpu/vec/vec256/vec256_qint.h>
#include <ATen/cpu/vec/vec256/vec256_complex_float.h>
#include <ATen/cpu/vec/vec256/vec256_complex_double.h>
#else
#include <ATen/cpu/vec256/vsx/vec256_common_vsx.h>
#include <ATen/cpu/vec/vec256/vsx/vec256_common_vsx.h>
#endif

#include <algorithm>
Expand All @@ -26,7 +26,7 @@
#include <iostream>

namespace at {
namespace vec256 {
namespace vec {

// Note [Acceptable use of anonymous namespace in header]
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -53,11 +53,11 @@ namespace {
}

template <typename T>
std::ostream& operator<<(std::ostream& stream, const Vec256<T>& vec) {
T buf[Vec256<T>::size()];
std::ostream& operator<<(std::ostream& stream, const Vectorized<T>& vec) {
T buf[Vectorized<T>::size()];
vec.store(buf);
stream << "vec[";
for (int i = 0; i != Vec256<T>::size(); i++) {
for (int i = 0; i != Vectorized<T>::size(); i++) {
if (i != 0) {
stream << ", ";
}
Expand All @@ -73,12 +73,12 @@ std::ostream& operator<<(std::ostream& stream, const Vec256<T>& vec) {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CAST (AVX) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

template<>
inline Vec256<float> cast<float, double>(const Vec256<double>& src) {
inline Vectorized<float> cast<float, double>(const Vectorized<double>& src) {
return _mm256_castpd_ps(src);
}

template<>
inline Vec256<double> cast<double, float>(const Vec256<float>& src) {
inline Vectorized<double> cast<double, float>(const Vectorized<float>& src) {
return _mm256_castps_pd(src);
}

Expand All @@ -88,11 +88,11 @@ inline Vec256<double> cast<double, float>(const Vec256<float>& src) {

#define DEFINE_FLOAT_INT_CAST(int_t, float_t, float_ch) \
template<> \
inline Vec256<int_t> cast<int_t, float_t>(const Vec256<float_t>& src) { \
inline Vectorized<int_t> cast<int_t, float_t>(const Vectorized<float_t>& src) { \
return _mm256_castp ## float_ch ## _si256(src); \
} \
template<> \
inline Vec256<float_t> cast<float_t, int_t>(const Vec256<int_t>& src) { \
inline Vectorized<float_t> cast<float_t, int_t>(const Vectorized<int_t>& src) { \
return _mm256_castsi256_p ## float_ch (src); \
}

Expand All @@ -108,30 +108,30 @@ DEFINE_FLOAT_INT_CAST(int16_t, float, s)
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GATHER ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

template<int64_t scale = 1>
std::enable_if_t<scale == 1 || scale == 2 || scale == 4 || scale == 8, Vec256<double>>
inline gather(const double* base_addr, const Vec256<int64_t>& vindex) {
std::enable_if_t<scale == 1 || scale == 2 || scale == 4 || scale == 8, Vectorized<double>>
inline gather(const double* base_addr, const Vectorized<int64_t>& vindex) {
return _mm256_i64gather_pd(base_addr, vindex, scale);
}

template<int64_t scale = 1>
std::enable_if_t<scale == 1 || scale == 2 || scale == 4 || scale == 8, Vec256<float>>
inline gather(const float* base_addr, const Vec256<int32_t>& vindex) {
std::enable_if_t<scale == 1 || scale == 2 || scale == 4 || scale == 8, Vectorized<float>>
inline gather(const float* base_addr, const Vectorized<int32_t>& vindex) {
return _mm256_i32gather_ps(base_addr, vindex, scale);
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MASK GATHER ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

template<int64_t scale = 1>
std::enable_if_t<scale == 1 || scale == 2 || scale == 4 || scale == 8, Vec256<double>>
inline mask_gather(const Vec256<double>& src, const double* base_addr,
const Vec256<int64_t>& vindex, const Vec256<double>& mask) {
std::enable_if_t<scale == 1 || scale == 2 || scale == 4 || scale == 8, Vectorized<double>>
inline mask_gather(const Vectorized<double>& src, const double* base_addr,
const Vectorized<int64_t>& vindex, const Vectorized<double>& mask) {
return _mm256_mask_i64gather_pd(src, base_addr, vindex, mask, scale);
}

template<int64_t scale = 1>
std::enable_if_t<scale == 1 || scale == 2 || scale == 4 || scale == 8, Vec256<float>>
inline mask_gather(const Vec256<float>& src, const float* base_addr,
const Vec256<int32_t>& vindex, const Vec256<float>& mask) {
std::enable_if_t<scale == 1 || scale == 2 || scale == 4 || scale == 8, Vectorized<float>>
inline mask_gather(const Vectorized<float>& src, const float* base_addr,
const Vectorized<int32_t>& vindex, const Vectorized<float>& mask) {
return _mm256_mask_i32gather_ps(src, base_addr, vindex, mask, scale);
}

Expand All @@ -140,8 +140,8 @@ inline mask_gather(const Vec256<float>& src, const float* base_addr,
// Only works for inputs in the range: [-2^51, 2^51]
// From: https://stackoverflow.com/a/41148578
template<>
Vec256<int64_t>
inline convert_to_int_of_same_size<double>(const Vec256<double> &src) {
Vectorized<int64_t>
inline convert_to_int_of_same_size<double>(const Vectorized<double> &src) {
auto x = _mm256_add_pd(src, _mm256_set1_pd(0x0018000000000000));
return _mm256_sub_epi64(
_mm256_castpd_si256(x),
Expand All @@ -150,16 +150,16 @@ inline convert_to_int_of_same_size<double>(const Vec256<double> &src) {
}

template<>
Vec256<int32_t>
inline convert_to_int_of_same_size<float>(const Vec256<float> &src) {
Vectorized<int32_t>
inline convert_to_int_of_same_size<float>(const Vectorized<float> &src) {
return _mm256_cvttps_epi32(src);
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ INTERLEAVE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

template <>
std::pair<Vec256<double>, Vec256<double>>
inline interleave2<double>(const Vec256<double>& a, const Vec256<double>& b) {
std::pair<Vectorized<double>, Vectorized<double>>
inline interleave2<double>(const Vectorized<double>& a, const Vectorized<double>& b) {
// inputs:
// a = {a0, a1, a3, a3}
// b = {b0, b1, b2, b3}
Expand All @@ -178,8 +178,8 @@ inline interleave2<double>(const Vec256<double>& a, const Vec256<double>& b) {
}

template <>
std::pair<Vec256<float>, Vec256<float>>
inline interleave2<float>(const Vec256<float>& a, const Vec256<float>& b) {
std::pair<Vectorized<float>, Vectorized<float>>
inline interleave2<float>(const Vectorized<float>& a, const Vectorized<float>& b) {
// inputs:
// a = {a0, a1, a2, a3, a4, a5, a6, a7}
// b = {b0, b1, b2, b3, b4, b5, b6, b7}
Expand All @@ -202,8 +202,8 @@ inline interleave2<float>(const Vec256<float>& a, const Vec256<float>& b) {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ DEINTERLEAVE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

template <>
std::pair<Vec256<double>, Vec256<double>>
inline deinterleave2<double>(const Vec256<double>& a, const Vec256<double>& b) {
std::pair<Vectorized<double>, Vectorized<double>>
inline deinterleave2<double>(const Vectorized<double>& a, const Vectorized<double>& b) {
// inputs:
// a = {a0, b0, a1, b1}
// b = {a2, b2, a3, b3}
Expand All @@ -222,8 +222,8 @@ inline deinterleave2<double>(const Vec256<double>& a, const Vec256<double>& b) {
}

template <>
std::pair<Vec256<float>, Vec256<float>>
inline deinterleave2<float>(const Vec256<float>& a, const Vec256<float>& b) {
std::pair<Vectorized<float>, Vectorized<float>>
inline deinterleave2<float>(const Vectorized<float>& a, const Vectorized<float>& b) {
// inputs:
// a = {a0, b0, a1, b1, a2, b2, a3, b3}
// b = {a4, b4, a5, b5, a6, b6, a7, b7}
Expand Down
Loading

0 comments on commit 47c566e

Please sign in to comment.