-
Notifications
You must be signed in to change notification settings - Fork 577
Add vectorized_math.h #11204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
swolchok
wants to merge
4
commits into
main
Choose a base branch
from
gh/swolchok/438/head
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add vectorized_math.h #11204
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#include <executorch/kernels/portable/cpu/util/vectorized_math.h> | ||
|
||
#include <c10/util/irange.h> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <cstdint> | ||
|
||
#ifndef ET_USE_PYTORCH_HEADERS | ||
#error "This test requires ET_USE_PYTORCH_HEADERS!" | ||
#endif // ET_USE_PYTORCH_HEADERS | ||
|
||
TEST(VectorizedMathTest, BasicUnary) { | ||
__at_align__ float result_floats[at::vec::Vectorized<float>::size()] = {0}; | ||
const auto x_vec = at::vec::Vectorized<float>::arange(0, 1); | ||
const auto result_vec = executorch::math::exp(x_vec); | ||
result_vec.store(result_floats); | ||
for (const auto ii : c10::irange(at::vec::Vectorized<float>::size())) { | ||
EXPECT_FLOAT_EQ(result_floats[ii], std::exp(ii)); | ||
} | ||
} | ||
|
||
namespace { | ||
template <typename T> | ||
void test_unary_t_to_float() { | ||
__at_align__ float result_floats[at::vec::Vectorized<T>::size()] = {0}; | ||
const auto x_vec = at::vec::Vectorized<T>::arange(0, 1); | ||
const auto result_vec = executorch::math::exp(x_vec); | ||
static_assert(decltype(result_vec)::size() >= at::vec::Vectorized<T>::size()); | ||
result_vec.store(result_floats, at::vec::Vectorized<T>::size()); | ||
for (const auto ii : c10::irange(at::vec::Vectorized<T>::size())) { | ||
EXPECT_EQ(result_floats[ii], std::exp((float)ii)) << ii; | ||
} | ||
} | ||
|
||
} // namespace | ||
|
||
TEST(VectorizedMathTest, UnaryInt16ToFloat) { | ||
test_unary_t_to_float<std::uint16_t>(); | ||
} | ||
|
||
TEST(VectorizedMathTest, UnaryInt32ToFloat) { | ||
test_unary_t_to_float<std::uint32_t>(); | ||
} | ||
|
||
TEST(VectorizedMathTest, UnaryInt64ToFloat) { | ||
test_unary_t_to_float<std::uint64_t>(); | ||
} | ||
|
||
TEST(VectorizedMathTest, BasicBinary) { | ||
__at_align__ float result_floats[at::vec::Vectorized<float>::size()] = {0}; | ||
const auto x_vec = at::vec::Vectorized<float>::arange(0, 1); | ||
const auto y_vec = at::vec::Vectorized<float>(2); | ||
const auto result_vec = executorch::math::pow(x_vec, y_vec); | ||
result_vec.store(result_floats); | ||
for (const auto ii : c10::irange(at::vec::Vectorized<float>::size())) { | ||
EXPECT_FLOAT_EQ(result_floats[ii], std::pow((float)ii, 2.0f)); | ||
} | ||
} | ||
|
||
namespace { | ||
template <typename T> | ||
void test_binary_t_to_float() { | ||
__at_align__ float result_floats[at::vec::Vectorized<T>::size()] = {0}; | ||
const auto x_vec = at::vec::Vectorized<T>::arange(0, 1); | ||
const auto y_vec = at::vec::Vectorized<T>(2); | ||
const auto result_vec = executorch::math::pow(x_vec, y_vec); | ||
static_assert(decltype(result_vec)::size() >= at::vec::Vectorized<T>::size()); | ||
result_vec.store(result_floats, at::vec::Vectorized<T>::size()); | ||
for (const auto ii : c10::irange(at::vec::Vectorized<T>::size())) { | ||
EXPECT_EQ(result_floats[ii], std::pow((float)ii, 2.0f)) << ii; | ||
} | ||
} | ||
|
||
TEST(VectorizedMathTest, BinaryInt16ToFloat) { | ||
test_binary_t_to_float<std::int16_t>(); | ||
} | ||
|
||
TEST(VectorizedMathTest, BinaryInt32ToFloat) { | ||
test_binary_t_to_float<std::int32_t>(); | ||
} | ||
|
||
TEST(VectorizedMathTest, BinaryInt64ToFloat) { | ||
test_binary_t_to_float<std::uint64_t>(); | ||
} | ||
|
||
} // namespace |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h> | ||
|
||
#ifdef ET_USE_PYTORCH_HEADERS | ||
#include <ATen/cpu/vec/vec.h> | ||
#endif // ET_USE_PYTORCH_HEADERS | ||
|
||
#include <iostream> | ||
#include <type_traits> | ||
|
||
#ifdef ET_USE_PYTORCH_HEADERS | ||
namespace executorch { | ||
inline namespace math { | ||
namespace internal { | ||
template <typename T> | ||
auto convert_to_vectorized_n_of_float(at::vec::Vectorized<T> vec) { | ||
static constexpr auto float_vec_size = at::vec::Vectorized<float>::size(); | ||
static constexpr auto t_vec_size = at::vec::Vectorized<T>::size(); | ||
static constexpr auto result_size = | ||
t_vec_size < float_vec_size ? 1 : t_vec_size / float_vec_size; | ||
static_assert(result_size >= 1); | ||
return at::vec::convert<float, result_size, T, 1, /*keep=*/true>( | ||
at::vec::VectorizedN<T, 1>(vec)); | ||
} | ||
} // namespace internal | ||
} // namespace math | ||
} // namespace executorch | ||
#endif // ET_USE_PYTORCH_HEADERS | ||
|
||
#define _ET_INTERNAL_STD_MATH_FUNC(name) \ | ||
namespace executorch { \ | ||
inline namespace math { \ | ||
using std::name; \ | ||
} \ | ||
} // namespace executorch | ||
|
||
#ifdef ET_USE_PYTORCH_HEADERS | ||
/** | ||
* Internal-usage macro for making a vectorized variant of a unary | ||
* function available in the executorch::math namespace. | ||
*/ | ||
#define ET_INTERNAL_VECTORIZED_FLOAT_UNARY_FUNC(func_name) \ | ||
namespace executorch { \ | ||
inline namespace math { \ | ||
template <typename T> \ | ||
auto func_name(at::vec::Vectorized<T> vec) { \ | ||
if constexpr (!::executorch::runtime::is_floating_point<T>::value) { \ | ||
return internal::convert_to_vectorized_n_of_float(vec).func_name(); \ | ||
} else { \ | ||
return vec.func_name(); \ | ||
} \ | ||
} \ | ||
} \ | ||
} | ||
|
||
#define ET_INTERNAL_VECTORIZED_FLOAT_BINARY_FUNC(func_name) \ | ||
namespace executorch { \ | ||
inline namespace math { \ | ||
template <typename T> \ | ||
auto func_name(at::vec::Vectorized<T> vec0, at::vec::Vectorized<T> vec1) { \ | ||
if constexpr (!::executorch::runtime::is_floating_point<T>::value) { \ | ||
const auto vec_float0 = \ | ||
internal::convert_to_vectorized_n_of_float(vec0); \ | ||
const auto vec_float1 = \ | ||
internal::convert_to_vectorized_n_of_float(vec1); \ | ||
return vec_float0.func_name(vec_float1); \ | ||
} else { \ | ||
return vec0.func_name(vec1); \ | ||
} \ | ||
} \ | ||
} \ | ||
} | ||
|
||
/** | ||
* Internal-usage macro for making a C++ standard library | ||
* floating-point function and a vectorized variant of it available in | ||
* the c10::math namespace. Should be used with functions where the | ||
* corresponding operator is a "float op" in TensorIterator parlance | ||
* (i.e., uses something like build_borrowing_binary_float_op()), | ||
* because it converts non-floating-point arguments to floating point. | ||
*/ | ||
#define ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(func_name) \ | ||
_ET_INTERNAL_STD_MATH_FUNC(func_name) \ | ||
ET_INTERNAL_VECTORIZED_FLOAT_UNARY_FUNC(func_name) | ||
|
||
#define ET_INTERNAL_VECTORIZED_STD_FLOAT_BINARY_FUNC(func_name) \ | ||
_ET_INTERNAL_STD_MATH_FUNC(func_name) \ | ||
ET_INTERNAL_VECTORIZED_FLOAT_BINARY_FUNC(func_name) | ||
|
||
#else // ET_USE_PYTORCH_HEADERS | ||
#define ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(name) \ | ||
_ET_INTERNAL_STD_MATH_FUNC(name) | ||
#define ET_INTERNAL_VECTORIZED_STD_FLOAT_BINARY_FUNC(name) \ | ||
_ET_INTERNAL_STD_MATH_FUNC(name) | ||
#endif // ET_USE_PYTORCH_HEADERS | ||
|
||
// To simplify client code, we provide coverage for a bunch of float ops (the | ||
// same ones listed in ATen vml.h) here. | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(abs) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(acos) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(asin) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(atan) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(ceil) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(cos) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(cosh) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(erf) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(erfc) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(exp) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(expm1) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(floor) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(log) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(log10) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(log1p) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(log2) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(sin) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(sinh) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(sqrt) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(round) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(tan) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(tanh) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(trunc) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(lgamma) | ||
|
||
#ifdef ET_USE_PYTORCH_HEADERS | ||
ET_INTERNAL_VECTORIZED_FLOAT_BINARY_FUNC(rsqrt) | ||
#endif // ET_USE_PYTORCH_HEADERS | ||
|
||
namespace executorch { | ||
inline namespace math { | ||
template <typename T, std::enable_if_t<std::is_floating_point_v<T>>> | ||
T rsqrt(T x) { | ||
return T(1) / std::sqrt(x); | ||
} | ||
} // namespace math | ||
} // namespace executorch | ||
|
||
ET_INTERNAL_VECTORIZED_STD_FLOAT_BINARY_FUNC(atan2) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_BINARY_FUNC(fmod) | ||
ET_INTERNAL_VECTORIZED_STD_FLOAT_BINARY_FUNC(pow) |
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are all these tests using
arange(0, 1)
, i.e. a single number instead of an actual vector of numbers?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's not a single number; the arguments are start and step, not start and end. https://github.com/pytorch/pytorch/blob/main/aten/src/ATen/cpu/vec/vec_base.h#L255