|
| 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 <cuda_runtime.h> |
| 10 | +#include <executorch/backends/aoti/slim/c10/core/DeviceType.h> |
| 11 | +#include <executorch/backends/aoti/slim/c10/core/ScalarType.h> |
| 12 | +#include <executorch/backends/cuda/runtime/shims/index.h> |
| 13 | +#include <executorch/backends/cuda/runtime/shims/memory.h> |
| 14 | +#include <executorch/runtime/core/error.h> |
| 15 | +#include <executorch/runtime/platform/platform.h> |
| 16 | +#include <gtest/gtest.h> |
| 17 | + |
| 18 | +#include <cstdint> |
| 19 | +#include <vector> |
| 20 | + |
| 21 | +using executorch::backends::cuda::aoti_torch_cuda_masked_select; |
| 22 | +using executorch::backends::cuda::aoti_torch_delete_tensor_object; |
| 23 | +using executorch::backends::cuda::aoti_torch_empty_strided; |
| 24 | +using executorch::runtime::Error; |
| 25 | +using Tensor = executorch::backends::aoti::slim::SlimTensor; |
| 26 | +namespace slim_c10 = executorch::backends::aoti::slim::c10; |
| 27 | + |
| 28 | +class AOTITorchCudaMaskedSelectTest : public ::testing::Test { |
| 29 | + protected: |
| 30 | + void SetUp() override { |
| 31 | + et_pal_init(); |
| 32 | + int device_count = 0; |
| 33 | + if (cudaGetDeviceCount(&device_count) != cudaSuccess || device_count == 0) { |
| 34 | + GTEST_SKIP() << "CUDA not available, skipping CUDA tests"; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + void TearDown() override { |
| 39 | + for (Tensor* tensor : tensors_) { |
| 40 | + aoti_torch_delete_tensor_object(tensor); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + template <typename T> |
| 45 | + Tensor* to_device( |
| 46 | + const std::vector<T>& values, |
| 47 | + const std::vector<int64_t>& sizes, |
| 48 | + slim_c10::ScalarType dtype, |
| 49 | + const std::vector<int64_t>& strides = {}) { |
| 50 | + Tensor* tensor = nullptr; |
| 51 | + EXPECT_EQ( |
| 52 | + aoti_torch_empty_strided( |
| 53 | + static_cast<int64_t>(sizes.size()), |
| 54 | + sizes.data(), |
| 55 | + strides.empty() ? nullptr : strides.data(), |
| 56 | + static_cast<int32_t>(dtype), |
| 57 | + static_cast<int32_t>(slim_c10::DeviceType::CUDA), |
| 58 | + 0, |
| 59 | + &tensor), |
| 60 | + Error::Ok); |
| 61 | + EXPECT_EQ( |
| 62 | + cudaMemcpy( |
| 63 | + tensor->data_ptr(), |
| 64 | + values.data(), |
| 65 | + values.size() * sizeof(T), |
| 66 | + cudaMemcpyHostToDevice), |
| 67 | + cudaSuccess); |
| 68 | + tensors_.push_back(tensor); |
| 69 | + return tensor; |
| 70 | + } |
| 71 | + |
| 72 | + template <typename T> |
| 73 | + std::vector<T> masked_select(Tensor* self, Tensor* mask) { |
| 74 | + Tensor* result = nullptr; |
| 75 | + EXPECT_EQ(aoti_torch_cuda_masked_select(self, mask, &result), Error::Ok); |
| 76 | + if (result == nullptr) { |
| 77 | + return {}; |
| 78 | + } |
| 79 | + tensors_.push_back(result); |
| 80 | + EXPECT_EQ(result->dim(), 1); |
| 81 | + std::vector<T> host(result->numel()); |
| 82 | + EXPECT_EQ(cudaDeviceSynchronize(), cudaSuccess); |
| 83 | + EXPECT_EQ( |
| 84 | + cudaMemcpy( |
| 85 | + host.data(), |
| 86 | + result->data_ptr(), |
| 87 | + host.size() * sizeof(T), |
| 88 | + cudaMemcpyDeviceToHost), |
| 89 | + cudaSuccess); |
| 90 | + return host; |
| 91 | + } |
| 92 | + |
| 93 | + std::vector<Tensor*> tensors_; |
| 94 | +}; |
| 95 | + |
| 96 | +TEST_F(AOTITorchCudaMaskedSelectTest, SameShape) { |
| 97 | + Tensor* self = to_device<float>( |
| 98 | + {1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f}, {2, 3}, slim_c10::kFloat); |
| 99 | + Tensor* mask = to_device<uint8_t>({1, 0, 1, 0, 1, 0}, {2, 3}, slim_c10::kBool); |
| 100 | + |
| 101 | + const std::vector<float> expected = {1.0f, 3.0f, 5.0f}; |
| 102 | + EXPECT_EQ(masked_select<float>(self, mask), expected); |
| 103 | +} |
| 104 | + |
| 105 | +TEST_F(AOTITorchCudaMaskedSelectTest, MaskBroadcastsOverRows) { |
| 106 | + Tensor* self = |
| 107 | + to_device<int64_t>({10, 11, 12, 20, 21, 22}, {2, 3}, slim_c10::kLong); |
| 108 | + Tensor* mask = to_device<uint8_t>({0, 1, 1}, {3}, slim_c10::kBool); |
| 109 | + |
| 110 | + const std::vector<int64_t> expected = {11, 12, 21, 22}; |
| 111 | + EXPECT_EQ(masked_select<int64_t>(self, mask), expected); |
| 112 | +} |
| 113 | + |
| 114 | +TEST_F(AOTITorchCudaMaskedSelectTest, SelfBroadcastsToMask) { |
| 115 | + // self is a column, so each of its values repeats along the mask's rows. |
| 116 | + Tensor* self = to_device<int32_t>({7, 8}, {2, 1}, slim_c10::kInt); |
| 117 | + Tensor* mask = to_device<uint8_t>({1, 0, 1, 1, 1, 0}, {2, 3}, slim_c10::kByte); |
| 118 | + |
| 119 | + const std::vector<int32_t> expected = {7, 7, 8, 8}; |
| 120 | + EXPECT_EQ(masked_select<int32_t>(self, mask), expected); |
| 121 | +} |
| 122 | + |
| 123 | +TEST_F(AOTITorchCudaMaskedSelectTest, NonContiguousSelf) { |
| 124 | + // Stored column-major, so the logical tensor is {{1, 2}, {3, 4}}. |
| 125 | + Tensor* self = |
| 126 | + to_device<float>({1.0f, 3.0f, 2.0f, 4.0f}, {2, 2}, slim_c10::kFloat, {1, 2}); |
| 127 | + Tensor* mask = to_device<uint8_t>({0, 1, 1, 0}, {2, 2}, slim_c10::kBool); |
| 128 | + |
| 129 | + const std::vector<float> expected = {2.0f, 3.0f}; |
| 130 | + EXPECT_EQ(masked_select<float>(self, mask), expected); |
| 131 | +} |
| 132 | + |
| 133 | +TEST_F(AOTITorchCudaMaskedSelectTest, ZeroDimSelfAndMask) { |
| 134 | + Tensor* self = to_device<float>({4.0f}, {}, slim_c10::kFloat); |
| 135 | + Tensor* set = to_device<uint8_t>({1}, {}, slim_c10::kBool); |
| 136 | + Tensor* unset = to_device<uint8_t>({0}, {}, slim_c10::kBool); |
| 137 | + |
| 138 | + EXPECT_EQ(masked_select<float>(self, set), std::vector<float>{4.0f}); |
| 139 | + EXPECT_EQ(masked_select<float>(self, unset), std::vector<float>{}); |
| 140 | +} |
| 141 | + |
| 142 | +TEST_F(AOTITorchCudaMaskedSelectTest, IncompatibleShapesAreRejected) { |
| 143 | + Tensor* self = to_device<float>({1.0f, 2.0f, 3.0f}, {3}, slim_c10::kFloat); |
| 144 | + Tensor* mask = to_device<uint8_t>({1, 0}, {2}, slim_c10::kBool); |
| 145 | + Tensor* result = nullptr; |
| 146 | + |
| 147 | + EXPECT_EQ( |
| 148 | + aoti_torch_cuda_masked_select(self, mask, &result), |
| 149 | + Error::InvalidArgument); |
| 150 | +} |
0 commit comments