|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <c10/util/irange.h> |
| 10 | +#include <executorch/kernels/test/FunctionHeaderWrapper.h> // Declares the operator |
| 11 | +#include <executorch/kernels/test/TestUtil.h> |
| 12 | +#include <executorch/kernels/test/supported_features.h> |
| 13 | +#include <executorch/runtime/core/exec_aten/exec_aten.h> |
| 14 | +#include <executorch/runtime/core/exec_aten/testing_util/tensor_factory.h> |
| 15 | +#include <executorch/runtime/core/exec_aten/testing_util/tensor_util.h> |
| 16 | + |
| 17 | +#include <gtest/gtest.h> |
| 18 | + |
| 19 | +#include <cmath> |
| 20 | +#include <numeric> |
| 21 | + |
| 22 | +using executorch::aten::IntArrayRef; |
| 23 | +using executorch::aten::ScalarType; |
| 24 | +using executorch::aten::Tensor; |
| 25 | +using torch::executor::testing::TensorFactory; |
| 26 | + |
| 27 | +class OpRandnTest : public OperatorTest { |
| 28 | + protected: |
| 29 | + void op_randn_out(const IntArrayRef sizes, Tensor& out) { |
| 30 | + torch::executor::aten::randn_outf(context_, sizes, out); |
| 31 | + } |
| 32 | + |
| 33 | + template <typename CTYPE, ScalarType DTYPE> |
| 34 | + void test_randn(std::vector<int64_t>& sizes) { |
| 35 | + TensorFactory<DTYPE> tf; |
| 36 | + |
| 37 | + // Tensor factory wants int32 scales, op kernel wants int64. |
| 38 | + std::vector<int32_t> sizes_i32; |
| 39 | + std::transform( |
| 40 | + sizes.begin(), |
| 41 | + sizes.end(), |
| 42 | + std::back_inserter(sizes_i32), |
| 43 | + [](int64_t s) { return static_cast<int32_t>(s); }); |
| 44 | + Tensor out = tf.zeros(sizes_i32); |
| 45 | + |
| 46 | + IntArrayRef sizes_ref(sizes.data(), sizes.size()); |
| 47 | + op_randn_out(sizes_ref, out); |
| 48 | + |
| 49 | + // Check mean and standard deviation. To avoid flaky CI, test pretty |
| 50 | + // loosely. |
| 51 | + auto out_data = out.const_data_ptr<CTYPE>(); |
| 52 | + double mean = |
| 53 | + std::accumulate( |
| 54 | + out_data, |
| 55 | + out_data + out.numel(), |
| 56 | + 0.0, |
| 57 | + [](double acc, CTYPE n) { return acc + static_cast<double>(n); }) / |
| 58 | + out.numel(); |
| 59 | + double var = std::accumulate( |
| 60 | + out_data, |
| 61 | + out_data + out.numel(), |
| 62 | + 0.0, |
| 63 | + [=](double acc, CTYPE n) { |
| 64 | + return acc + std::pow(static_cast<double>(n) - mean, 2); |
| 65 | + }) / |
| 66 | + out.numel(); |
| 67 | + auto stdev = std::sqrt(var); |
| 68 | + |
| 69 | + // These are very rough thresholds. A better test implementation would |
| 70 | + // probably do a proper statistical test to compare the generated empirical |
| 71 | + // data to the reference distribution, but this should do. |
| 72 | + EXPECT_LE(std::abs(mean), 5.0 / std::sqrt(out.numel())); |
| 73 | + EXPECT_LE(std::abs(stdev - 1.0), 0.1); |
| 74 | + EXPECT_GT(stdev, 0); |
| 75 | + } |
| 76 | +}; |
| 77 | + |
| 78 | +TEST_F(OpRandnTest, SmokeTest) { |
| 79 | + std::vector<int64_t> sizes = {2, 3, 4, 128}; |
| 80 | + |
| 81 | +#define TEST_ENTRY(ctype, dtype) test_randn<ctype, ScalarType::dtype>(sizes); |
| 82 | + ET_FORALL_FLOATHBF16_TYPES(TEST_ENTRY); |
| 83 | +#undef TEST_ENTRY |
| 84 | +} |
| 85 | + |
| 86 | +TEST_F(OpRandnTest, Rank) { |
| 87 | + std::vector<int64_t> sizes = {1024}; |
| 88 | + |
| 89 | + for (int64_t i = 0; i < 4; i++) { |
| 90 | + sizes.push_back(i + 1); |
| 91 | + test_randn<float, executorch::aten::ScalarType::Float>(sizes); |
| 92 | + } |
| 93 | +} |
0 commit comments