-
Notifications
You must be signed in to change notification settings - Fork 583
Add portable rand kernel implementation #11127
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -315,6 +315,8 @@ | |
|
||
- op: prod.out | ||
|
||
- op: rand.out | ||
|
||
- op: reciprocal.out | ||
|
||
- op: relu.out | ||
|
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,50 @@ | ||
/* | ||
* 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 <c10/util/irange.h> | ||
|
||
#include <executorch/kernels/portable/cpu/scalar_utils.h> | ||
#include <executorch/runtime/kernel/kernel_includes.h> | ||
|
||
#include <random> | ||
|
||
namespace torch { | ||
namespace executor { | ||
namespace native { | ||
|
||
using executorch::aten::IntArrayRef; | ||
using Tensor = executorch::aten::Tensor; | ||
using ScalarType = executorch::aten::ScalarType; | ||
|
||
Tensor& | ||
rand_out(KernelRuntimeContext& ctx, const IntArrayRef sizes, Tensor& out) { | ||
(void)ctx; | ||
|
||
std::mt19937 gen((std::random_device())()); | ||
std::uniform_real_distribution<double> dist(0.0, 1.0); | ||
|
||
// Resize for dynamic shape | ||
ET_KERNEL_CHECK_MSG( | ||
ctx, | ||
resize_tensor(out, sizes) == Error::Ok, | ||
InvalidArgument, | ||
out, | ||
"Failed to resize output tensor."); | ||
|
||
ET_SWITCH_FLOATHBF16_TYPES(out.scalar_type(), ctx, "randn.out", CTYPE, [&] { | ||
auto data_out = out.mutable_data_ptr<CTYPE>(); | ||
for (const auto i : c10::irange(out.numel())) { | ||
data_out[i] = static_cast<CTYPE>(dist(gen)); | ||
} | ||
}); | ||
|
||
return out; | ||
} | ||
|
||
} // namespace native | ||
} // namespace executor | ||
} // namespace torch |
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 <c10/util/irange.h> | ||
#include <executorch/kernels/test/FunctionHeaderWrapper.h> // Declares the operator | ||
#include <executorch/kernels/test/TestUtil.h> | ||
#include <executorch/runtime/core/exec_aten/exec_aten.h> | ||
#include <executorch/runtime/core/exec_aten/testing_util/tensor_factory.h> | ||
#include <executorch/runtime/core/exec_aten/testing_util/tensor_util.h> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <cmath> | ||
#include <numeric> | ||
|
||
using executorch::aten::IntArrayRef; | ||
using executorch::aten::ScalarType; | ||
using executorch::aten::Tensor; | ||
using torch::executor::testing::TensorFactory; | ||
|
||
class OpRandTest : public OperatorTest { | ||
protected: | ||
void op_rand_out(const IntArrayRef sizes, Tensor& out) { | ||
torch::executor::aten::rand_outf(context_, sizes, out); | ||
} | ||
|
||
template <typename CTYPE, ScalarType DTYPE> | ||
void test_rand(std::vector<int64_t>& sizes) { | ||
TensorFactory<DTYPE> tf; | ||
|
||
// Tensor factory wants int32 scales, op kernel wants int64. | ||
std::vector<int32_t> sizes_i32; | ||
std::transform( | ||
sizes.begin(), | ||
sizes.end(), | ||
std::back_inserter(sizes_i32), | ||
[](int64_t s) { return static_cast<int32_t>(s); }); | ||
Tensor out = tf.zeros(sizes_i32); | ||
|
||
IntArrayRef sizes_ref(sizes.data(), sizes.size()); | ||
op_rand_out(sizes_ref, out); | ||
|
||
// Check mean and standard deviation. To avoid flaky CI, test pretty | ||
// loosely. | ||
auto out_data = out.const_data_ptr<CTYPE>(); | ||
double mean = | ||
std::accumulate( | ||
out_data, | ||
out_data + out.numel(), | ||
0.0, | ||
[](double acc, CTYPE n) { return acc + static_cast<double>(n); }) / | ||
out.numel(); | ||
double var = std::accumulate( | ||
out_data, | ||
out_data + out.numel(), | ||
0.0, | ||
[=](double acc, CTYPE n) { | ||
return acc + std::pow(static_cast<double>(n) - mean, 2); | ||
}) / | ||
out.numel(); | ||
auto stdev = std::sqrt(var); | ||
|
||
// These are very rough thresholds. A better test implementation would | ||
// probably do a proper statistical test to compare the generated empirical | ||
// data to the reference distribution, but this should do. | ||
|
||
// Expected mean is 0.5 | ||
EXPECT_NEAR(mean, 0.5, 5.0 / std::sqrt(out.numel())); | ||
// Expected stdev is 1/sqrt(12) ~= 0.289 | ||
EXPECT_NEAR(stdev, 1.0 / std::sqrt(12), 0.1); | ||
EXPECT_GT(stdev, 0); | ||
} | ||
}; | ||
|
||
TEST_F(OpRandTest, SmokeTest) { | ||
std::vector<int64_t> sizes = {2, 3, 4, 128}; | ||
|
||
#define TEST_ENTRY(ctype, dtype) test_rand<ctype, ScalarType::dtype>(sizes); | ||
ET_FORALL_FLOATHBF16_TYPES(TEST_ENTRY); | ||
#undef TEST_ENTRY | ||
} | ||
|
||
TEST_F(OpRandTest, Rank) { | ||
std::vector<int64_t> sizes = {1024}; | ||
|
||
for (int64_t i = 0; i < 4; i++) { | ||
sizes.push_back(i + 1); | ||
test_rand<float, executorch::aten::ScalarType::Float>(sizes); | ||
} | ||
} |
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.
This may not be available in embedded devices. Foe example, zephyr has different API https://docs.zephyrproject.org/apidoc/latest/group__random__api.html
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.
Yeah, this is generally true, though FYI Scott W has already been using std random for other ops. I'm open to changing it. But we might need to provide implementations as part of the Cortex M workstream.