Skip to content

Commit 1336645

Browse files
[slimtensor] Add from_blob() factory function for wrapping external memory (#16837)
This PR was created by the merge bot to help merge the original PR into the main branch. ghstack PR number: #16442 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/84/base ghstack PR head: https://github.com/pytorch/executorch/tree/gh/gasoonjia/84/head Merge bot PR base: https://github.com/pytorch/executorch/tree/main Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/gasoonjia/84/orig Differential Revision: [D89950482](https://our.internmc.facebook.com/intern/diff/D89950482/) @diff-train-skip-merge Co-authored-by: gasoonjia <gasoonjia@icloud.com>
1 parent c8c7312 commit 1336645

4 files changed

Lines changed: 914 additions & 0 deletions

File tree

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
#pragma once
10+
11+
#include <executorch/backends/aoti/slim/core/SlimTensor.h>
12+
#include <executorch/backends/aoti/slim/util/ArrayRefUtil.h>
13+
#include <executorch/backends/aoti/slim/util/SizeUtil.h>
14+
15+
namespace executorch::backends::aoti::slim {
16+
17+
/// Creates a SlimTensor that wraps external memory without taking ownership.
18+
/// The returned tensor does NOT own the underlying storage; the caller is
19+
/// responsible for keeping the data alive for the lifetime of the tensor.
20+
///
21+
/// @param data Pointer to external memory (must not be null).
22+
/// @param sizes The sizes of each dimension.
23+
/// @param strides The strides of each dimension.
24+
/// @param dtype The scalar type of tensor elements.
25+
/// @param device The device where the data resides.
26+
/// @param storage_offset Offset into storage in number of elements.
27+
/// @return A new SlimTensor with non-owning storage.
28+
inline SlimTensor from_blob(
29+
void* data,
30+
IntArrayRef sizes,
31+
IntArrayRef strides,
32+
c10::ScalarType dtype,
33+
const c10::Device& device = CPU_DEVICE,
34+
int64_t storage_offset = 0) {
35+
ET_CHECK_MSG(data != nullptr, "from_blob: data pointer cannot be nullptr");
36+
37+
size_t nbytes = compute_storage_nbytes(
38+
sizes, strides, c10::elementSize(dtype), storage_offset);
39+
40+
Storage storage(new MaybeOwningStorage(device, data, nbytes));
41+
return SlimTensor(std::move(storage), sizes, strides, dtype, storage_offset);
42+
}
43+
44+
/// Creates a contiguous SlimTensor that wraps external memory.
45+
/// Computes contiguous strides automatically.
46+
///
47+
/// @param data Pointer to external memory (must not be null).
48+
/// @param sizes The sizes of each dimension.
49+
/// @param dtype The scalar type of tensor elements.
50+
/// @param device The device where the data resides.
51+
/// @param storage_offset Offset into storage in number of elements.
52+
/// @return A new SlimTensor with non-owning storage and contiguous strides.
53+
inline SlimTensor from_blob(
54+
void* data,
55+
IntArrayRef sizes,
56+
c10::ScalarType dtype,
57+
const c10::Device& device = CPU_DEVICE,
58+
int64_t storage_offset = 0) {
59+
std::vector<int64_t> contig_strides = compute_contiguous_strides(sizes);
60+
return from_blob(
61+
data, sizes, makeArrayRef(contig_strides), dtype, device, storage_offset);
62+
}
63+
64+
/// Creates a contiguous SlimTensor from an initializer list of sizes.
65+
///
66+
/// @param data Pointer to external memory (must not be null).
67+
/// @param sizes The sizes as an initializer list.
68+
/// @param dtype The scalar type of tensor elements.
69+
/// @param device The device where the data resides.
70+
/// @param storage_offset Offset into storage in number of elements.
71+
/// @return A new SlimTensor with non-owning storage and contiguous strides.
72+
inline SlimTensor from_blob(
73+
void* data,
74+
std::initializer_list<int64_t> sizes,
75+
c10::ScalarType dtype,
76+
const c10::Device& device = CPU_DEVICE,
77+
int64_t storage_offset = 0) {
78+
return from_blob(data, makeArrayRef(sizes), dtype, device, storage_offset);
79+
}
80+
81+
/// Creates a SlimTensor from initializer lists for both sizes and strides.
82+
///
83+
/// @param data Pointer to external memory (must not be null).
84+
/// @param sizes The sizes as an initializer list.
85+
/// @param strides The strides as an initializer list.
86+
/// @param dtype The scalar type of tensor elements.
87+
/// @param device The device where the data resides.
88+
/// @param storage_offset Offset into storage in number of elements.
89+
/// @return A new SlimTensor with non-owning storage.
90+
inline SlimTensor from_blob(
91+
void* data,
92+
std::initializer_list<int64_t> sizes,
93+
std::initializer_list<int64_t> strides,
94+
c10::ScalarType dtype,
95+
const c10::Device& device = CPU_DEVICE,
96+
int64_t storage_offset = 0) {
97+
return from_blob(
98+
data,
99+
makeArrayRef(sizes),
100+
makeArrayRef(strides),
101+
dtype,
102+
device,
103+
storage_offset);
104+
}
105+
106+
} // namespace executorch::backends::aoti::slim

backends/aoti/slim/factory/targets.bzl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,16 @@ def define_common_targets():
1616
"//executorch/backends/aoti/slim/util:size_util",
1717
],
1818
)
19+
20+
runtime.cxx_library(
21+
name = "from_blob",
22+
headers = [
23+
"FromBlob.h",
24+
],
25+
visibility = ["@EXECUTORCH_CLIENTS"],
26+
exported_deps = [
27+
"//executorch/backends/aoti/slim/core:slimtensor",
28+
"//executorch/backends/aoti/slim/util:array_ref_util",
29+
"//executorch/backends/aoti/slim/util:size_util",
30+
],
31+
)

backends/aoti/slim/factory/test/targets.bzl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,16 @@ def define_common_targets():
3131
],
3232
**backend_kwargs
3333
)
34+
35+
runtime.cxx_test(
36+
name = "test_from_blob" + backend_suffix,
37+
srcs = [
38+
"test_from_blob.cpp",
39+
],
40+
deps = [
41+
"//executorch/backends/aoti/slim/core:storage",
42+
"//executorch/backends/aoti/slim/factory:from_blob",
43+
"//executorch/backends/aoti/slim/factory:empty",
44+
],
45+
**backend_kwargs
46+
)

0 commit comments

Comments
 (0)