|
| 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 <gtest/gtest.h> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +#include <executorch/backends/aoti/common_shims_slim.h> |
| 14 | +#include <executorch/backends/aoti/slim/c10/core/Device.h> |
| 15 | +#include <executorch/backends/aoti/slim/c10/core/ScalarType.h> |
| 16 | +#include <executorch/backends/cuda/runtime/shims/memory.h> |
| 17 | +#include <executorch/runtime/core/error.h> |
| 18 | +#include <executorch/runtime/platform/platform.h> |
| 19 | + |
| 20 | +using namespace executorch::backends::cuda; |
| 21 | +using namespace executorch::backends::aoti; |
| 22 | +using executorch::runtime::Error; |
| 23 | + |
| 24 | +namespace slim_c10 = executorch::backends::aoti::slim::c10; |
| 25 | + |
| 26 | +class AOTITorchZeroTest : public ::testing::Test { |
| 27 | + protected: |
| 28 | + void SetUp() override { |
| 29 | + et_pal_init(); |
| 30 | + int device_count = 0; |
| 31 | + if (cudaGetDeviceCount(&device_count) != cudaSuccess || device_count == 0) { |
| 32 | + GTEST_SKIP() << "CUDA not available"; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + Tensor* create( |
| 37 | + const std::vector<int64_t>& sizes, |
| 38 | + const std::vector<int64_t>& strides, |
| 39 | + slim_c10::DeviceType device_type) { |
| 40 | + Tensor* tensor = nullptr; |
| 41 | + EXPECT_EQ( |
| 42 | + aoti_torch_empty_strided( |
| 43 | + sizes.size(), |
| 44 | + sizes.data(), |
| 45 | + strides.data(), |
| 46 | + static_cast<int32_t>(slim_c10::ScalarType::Float), |
| 47 | + static_cast<int32_t>(device_type), |
| 48 | + 0, |
| 49 | + &tensor), |
| 50 | + Error::Ok); |
| 51 | + return tensor; |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +TEST_F(AOTITorchZeroTest, ZerosContiguousCudaTensor) { |
| 56 | + Tensor* tensor = create({2, 3}, {3, 1}, slim_c10::DeviceType::CUDA); |
| 57 | + const std::vector<float> ones(6, 1.0f); |
| 58 | + ASSERT_EQ( |
| 59 | + cudaMemcpy( |
| 60 | + tensor->data_ptr(), |
| 61 | + ones.data(), |
| 62 | + sizeof(float) * ones.size(), |
| 63 | + cudaMemcpyHostToDevice), |
| 64 | + cudaSuccess); |
| 65 | + |
| 66 | + EXPECT_EQ(aoti_torch_zero_(tensor), Error::Ok); |
| 67 | + |
| 68 | + std::vector<float> actual(6, 1.0f); |
| 69 | + ASSERT_EQ( |
| 70 | + cudaMemcpy( |
| 71 | + actual.data(), |
| 72 | + tensor->data_ptr(), |
| 73 | + sizeof(float) * actual.size(), |
| 74 | + cudaMemcpyDeviceToHost), |
| 75 | + cudaSuccess); |
| 76 | + EXPECT_EQ(actual, std::vector<float>(6, 0.0f)); |
| 77 | + aoti_torch_delete_tensor_object(tensor); |
| 78 | +} |
| 79 | + |
| 80 | +TEST_F(AOTITorchZeroTest, RejectsNullAndNonContiguousTensors) { |
| 81 | + EXPECT_EQ(aoti_torch_zero_(nullptr), Error::InvalidArgument); |
| 82 | + Tensor* tensor = create({2, 3}, {1, 2}, slim_c10::DeviceType::CUDA); |
| 83 | + EXPECT_EQ(aoti_torch_zero_(tensor), Error::InvalidArgument); |
| 84 | + aoti_torch_delete_tensor_object(tensor); |
| 85 | +} |
0 commit comments