|
| 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 | +using executorch::aten::ScalarType; |
| 20 | +using executorch::aten::Tensor; |
| 21 | +using torch::executor::testing::TensorFactory; |
| 22 | + |
| 23 | +class OpNativeDropoutTest : public OperatorTest { |
| 24 | + protected: |
| 25 | + void op_native_dropout_out( |
| 26 | + const Tensor& self, |
| 27 | + double prob, |
| 28 | + executorch::aten::optional<bool> train, |
| 29 | + Tensor& out, |
| 30 | + Tensor& mask) { |
| 31 | + torch::executor::aten::native_dropout_outf( |
| 32 | + context_, self, prob, train, out, mask); |
| 33 | + } |
| 34 | + |
| 35 | + template <typename CTYPE, ScalarType DTYPE> |
| 36 | + void test_dropout() { |
| 37 | + TensorFactory<DTYPE> tf; |
| 38 | + TensorFactory<ScalarType::Bool> tf_bool; |
| 39 | + const std::vector<int32_t> sizes = {3, 2}; |
| 40 | + Tensor in = tf.make(sizes, {1, 2, 3, 4, 5, 6}); |
| 41 | + Tensor out = tf.zeros(sizes); |
| 42 | + Tensor mask = tf_bool.zeros(sizes); |
| 43 | + |
| 44 | + bool* const mask_data = mask.mutable_data_ptr<bool>(); |
| 45 | + auto expect_no_drops = [&]() { |
| 46 | + EXPECT_TENSOR_CLOSE(out, in); |
| 47 | + for (const auto ii : c10::irange(mask.numel())) { |
| 48 | + EXPECT_TRUE(mask_data[ii]); |
| 49 | + mask_data[ii] = false; |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + op_native_dropout_out(in, 0, true, out, mask); |
| 54 | + expect_no_drops(); |
| 55 | + |
| 56 | + op_native_dropout_out(in, 0, false, out, mask); |
| 57 | + expect_no_drops(); |
| 58 | + |
| 59 | + op_native_dropout_out(in, 1, false, out, mask); |
| 60 | + expect_no_drops(); |
| 61 | + |
| 62 | + op_native_dropout_out(in, 1, true, out, mask); |
| 63 | + auto* const out_data = out.mutable_data_ptr<CTYPE>(); |
| 64 | + for (const auto ii : c10::irange(out.numel())) { |
| 65 | + EXPECT_EQ(out_data[ii], CTYPE(0)); |
| 66 | + } |
| 67 | + for (const auto ii : c10::irange(mask.numel())) { |
| 68 | + EXPECT_FALSE(mask_data[ii]); |
| 69 | + mask_data[ii] = 0; |
| 70 | + } |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | +TEST_F(OpNativeDropoutTest, Basic) { |
| 75 | +#define TEST_ENTRY(ctype, dtype) test_dropout<ctype, ScalarType::dtype>(); |
| 76 | + ET_FORALL_FLOATHBF16_TYPES(TEST_ENTRY); |
| 77 | +#undef TEST_ENTRY |
| 78 | +} |
| 79 | + |
| 80 | +TEST_F(OpNativeDropoutTest, ProbabilityRangeCheck) { |
| 81 | + TensorFactory<ScalarType::Float> tf_float; |
| 82 | + TensorFactory<ScalarType::Bool> tf_bool; |
| 83 | + const std::vector<int32_t> sizes = {2, 3}; |
| 84 | + Tensor a = tf_float.ones(sizes); |
| 85 | + Tensor out = tf_float.zeros(sizes); |
| 86 | + Tensor mask = tf_bool.zeros(sizes); |
| 87 | + ET_EXPECT_KERNEL_FAILURE( |
| 88 | + context_, op_native_dropout_out(a, -1, true, out, mask)); |
| 89 | +} |
| 90 | + |
| 91 | +TEST_F(OpNativeDropoutTest, MaskBoolCheck) { |
| 92 | + TensorFactory<ScalarType::Float> tf_float; |
| 93 | + TensorFactory<ScalarType::Byte> tf_byte; |
| 94 | + const std::vector<int32_t> sizes = {2, 3}; |
| 95 | + Tensor a = tf_float.ones(sizes); |
| 96 | + Tensor out = tf_float.zeros(sizes); |
| 97 | + Tensor mask_byte = tf_byte.zeros(sizes); |
| 98 | + Tensor mask_float = tf_float.zeros(sizes); |
| 99 | + ET_EXPECT_KERNEL_FAILURE( |
| 100 | + context_, op_native_dropout_out(a, 0.5, true, out, mask_byte)); |
| 101 | + ET_EXPECT_KERNEL_FAILURE( |
| 102 | + context_, op_native_dropout_out(a, 0.5, true, out, mask_float)); |
| 103 | +} |
0 commit comments