|
| 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 |
0 commit comments