Skip to content

Commit 8e70dd2

Browse files
[slimtensor] Add CUDA slimtensor creation with basic functionality (pytorch#16799)
This PR was created by the merge bot to help merge the original PR into the main branch. ghstack PR number: pytorch#16770 by @Gasoonjia ^ Please use this as the source of truth for the PR details, comments, and reviews ghstack PR base: https://github.com/pytorch/executorch/tree/gh/gasoonjia/110/base ghstack PR head: https://github.com/pytorch/executorch/tree/gh/gasoonjia/110/head Merge bot PR base: https://github.com/pytorch/executorch/tree/gh/gasoonjia/109/orig Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/gasoonjia/110/orig Differential Revision: [D91202897](https://our.internmc.facebook.com/intern/diff/D91202897/) @diff-train-skip-merge --------- Co-authored-by: gasoonjia <gasoonjia@icloud.com> Co-authored-by: Gasoonjia <gasoonjia@meta.com>
1 parent c0d948a commit 8e70dd2

7 files changed

Lines changed: 337 additions & 27 deletions

File tree

‎backends/aoti/slim/core/SlimTensor.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,13 @@ class SlimTensor {
227227
return device().is_cpu();
228228
}
229229

230+
/**
231+
* Check if the tensor is on CUDA.
232+
*/
233+
bool is_cuda() const {
234+
return device().is_cuda();
235+
}
236+
230237
/**
231238
* Check if the tensor is defined (has valid storage).
232239
*/

‎backends/aoti/slim/core/targets.bzl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def define_common_targets():
3737
"//executorch/backends/aoti/slim/c10/core:sizes_and_strides",
3838
"//executorch/backends/aoti/slim/util:array_ref_util",
3939
"//executorch/backends/aoti/slim/util:size_util",
40+
"//executorch/backends/aoti/slim/c10/cuda:exception",
4041
"//executorch/runtime/platform:platform",
4142
],
4243
)

‎backends/aoti/slim/core/test/targets.bzl‎

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,37 @@ def define_common_targets():
3232
**backend_kwargs
3333
)
3434

35-
runtime.cxx_test(
36-
name = "test_slimtensor_basic",
37-
srcs = [
38-
"test_slimtensor_basic.cpp",
39-
],
40-
deps = [
41-
"//executorch/backends/aoti/slim/core:slimtensor",
42-
"//executorch/backends/aoti/slim/core:storage",
43-
],
44-
)
35+
backend_kwargs = {
36+
"external_deps": [("cuda", None, "cuda-lazy")],
37+
"preprocessor_flags": ["-DCUDA_AVAILABLE=1"],
38+
"keep_gpu_sections": True,
39+
"remote_execution": re_test_utils.remote_execution(
40+
platform = "gpu-remote-execution",
41+
),
42+
} if backend_mode == "cuda" else {}
43+
44+
runtime.cxx_test(
45+
name = "test_storage" + backend_suffix,
46+
srcs = [
47+
"test_storage.cpp",
48+
],
49+
deps = [
50+
"//executorch/backends/aoti/slim/core:storage",
51+
],
52+
**backend_kwargs
53+
)
54+
55+
runtime.cxx_test(
56+
name = "test_slimtensor_basic" + backend_suffix,
57+
srcs = [
58+
"test_slimtensor_basic.cpp",
59+
],
60+
deps = [
61+
"//executorch/backends/aoti/slim/core:slimtensor",
62+
"//executorch/backends/aoti/slim/core:storage",
63+
],
64+
**backend_kwargs
65+
)
4566

4667
runtime.cxx_test(
4768
name = "test_slimtensor_copy",

‎backends/aoti/slim/core/test/test_slimtensor_basic.cpp‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ namespace executorch::backends::aoti::slim {
2121
inline std::vector<c10::Device> get_test_devices() {
2222
std::vector<c10::Device> devices;
2323
devices.push_back(CPU_DEVICE);
24+
#ifdef CUDA_AVAILABLE
25+
devices.push_back(DEFAULT_CUDA_DEVICE);
26+
#endif
2427
return devices;
2528
}
2629

@@ -52,7 +55,9 @@ INSTANTIATE_TEST_SUITE_P(
5255
DeviceTests,
5356
SlimTensorBasicDeviceTest,
5457
::testing::ValuesIn(get_test_devices()),
55-
[](const ::testing::TestParamInfo<c10::Device>& info) { return "CPU"; });
58+
[](const ::testing::TestParamInfo<c10::Device>& info) {
59+
return info.param.is_cuda() ? "CUDA" : "CPU";
60+
});
5661

5762
// =============================================================================
5863
// Constructor Tests (Device-Parameterized)
@@ -144,11 +149,11 @@ TEST_P(SlimTensorBasicDeviceTest, Dtype) {
144149
TEST_P(SlimTensorBasicDeviceTest, Device) {
145150
SlimTensor tensor = make_2x3_tensor();
146151

147-
// We only support CPU for now
148-
EXPECT_TRUE(tensor.is_cpu());
149-
EXPECT_EQ(tensor.device_type(), c10::DeviceType::CPU);
150-
152+
// Check device type and index
153+
EXPECT_EQ(tensor.device_type(), device().type());
151154
EXPECT_EQ(tensor.device_index(), device().index());
155+
EXPECT_EQ(tensor.is_cpu(), device().is_cpu());
156+
EXPECT_EQ(tensor.is_cuda(), device().is_cuda());
152157
}
153158

154159
TEST_P(SlimTensorBasicDeviceTest, Numel) {

‎backends/aoti/slim/factory/Empty.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace executorch::backends::aoti::slim {
2323
/// @param sizes The sizes of each dimension.
2424
/// @param strides The strides of each dimension.
2525
/// @param dtype The scalar type of tensor elements.
26-
/// @param device The target device (must be CPU).
26+
/// @param device The target device.
2727
/// @return A new SlimTensor with allocated but uninitialized storage.
2828
inline SlimTensor empty_strided(
2929
IntArrayRef sizes,
@@ -41,7 +41,7 @@ inline SlimTensor empty_strided(
4141
///
4242
/// @param sizes The sizes of each dimension.
4343
/// @param dtype The scalar type of tensor elements.
44-
/// @param device The target device (must be CPU).
44+
/// @param device The target device.
4545
/// @return A new SlimTensor with contiguous strides and uninitialized storage.
4646
inline SlimTensor empty(
4747
IntArrayRef sizes,
@@ -59,7 +59,7 @@ inline SlimTensor empty(
5959
///
6060
/// @param sizes The sizes of each dimension as an initializer list.
6161
/// @param dtype The scalar type of tensor elements.
62-
/// @param device The target device (must be CPU).
62+
/// @param device The target device.
6363
/// @return A new SlimTensor with contiguous strides and uninitialized storage.
6464
inline SlimTensor empty(
6565
std::initializer_list<int64_t> sizes,
Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
1+
load("@fbcode_macros//build_defs/lib:re_test_utils.bzl", "re_test_utils")
12
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
23

4+
def get_backend_mode():
5+
"""Get the supported backend mode of slimtensor."""
6+
return ["cuda", "cpu"]
7+
38
def define_common_targets():
49
"""Define test targets for SlimTensor factory module."""
510

6-
runtime.cxx_test(
7-
name = "test_empty",
8-
srcs = [
9-
"test_empty.cpp",
10-
],
11-
deps = [
12-
"//executorch/backends/aoti/slim/factory:empty",
13-
],
14-
)
11+
# GPU empty test with CUDA support
12+
for backend_mode in get_backend_mode():
13+
backend_suffix = "_" + backend_mode if backend_mode == "cuda" else ""
14+
15+
backend_kwargs = {
16+
"external_deps": [("cuda", None, "cuda-lazy")],
17+
"preprocessor_flags": ["-DCUDA_AVAILABLE=1"],
18+
"keep_gpu_sections": True,
19+
"remote_execution": re_test_utils.remote_execution(
20+
platform = "gpu-remote-execution",
21+
),
22+
} if backend_mode == "cuda" else {}
23+
24+
runtime.cxx_test(
25+
name = "test_empty" + backend_suffix,
26+
srcs = [
27+
"test_empty.cpp",
28+
],
29+
deps = [
30+
"//executorch/backends/aoti/slim/factory:empty",
31+
],
32+
**backend_kwargs
33+
)

0 commit comments

Comments
 (0)