Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion backends/aoti/slim/core/Storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@

#pragma once

#include <cstdint>
#include <cstring>

#include <executorch/backends/aoti/slim/c10/core/Device.h>
#include <executorch/backends/aoti/slim/c10/core/ScalarType.h>
#include <executorch/backends/aoti/slim/util/ArrayRefUtil.h>
#include <executorch/backends/aoti/slim/util/SharedPtr.h>
#include <executorch/backends/aoti/slim/util/SizeUtil.h>
#include <executorch/runtime/platform/assert.h>

namespace executorch::backends::aoti::slim {
Expand Down Expand Up @@ -242,4 +243,20 @@ class MaybeOwningStorage {
/// Multiple tensors can share the same underlying storage.
using Storage = SharedPtr<MaybeOwningStorage>;

/// Creates a new owning storage with the given parameters.
/// @param sizes The sizes of each dimension.
/// @param strides The strides of each dimension.
/// @param dtype The scalar type of tensor elements.
/// @param device The target device (must be CPU).
/// @return A shared pointer to the newly allocated storage.
inline Storage new_storage(
IntArrayRef sizes,
IntArrayRef strides,
c10::ScalarType dtype,
const c10::Device& device = CPU_DEVICE) {
size_t nbytes =
compute_storage_nbytes(sizes, strides, c10::elementSize(dtype), 0);
return Storage(new MaybeOwningStorage(device, nbytes));
}

} // namespace executorch::backends::aoti::slim
2 changes: 2 additions & 0 deletions backends/aoti/slim/core/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ def define_common_targets():
exported_deps = [
"//executorch/backends/aoti/slim/c10/core:device",
"//executorch/backends/aoti/slim/c10/core:scalar_type",
"//executorch/backends/aoti/slim/util:array_ref_util",
"//executorch/backends/aoti/slim/util:shared_ptr",
"//executorch/backends/aoti/slim/util:size_util",
"//executorch/runtime/platform:platform",
],
)
Expand Down
82 changes: 82 additions & 0 deletions backends/aoti/slim/factory/Empty.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <cstdint>

#include <executorch/backends/aoti/slim/core/SlimTensor.h>
#include <executorch/backends/aoti/slim/util/ArrayRefUtil.h>
#include <executorch/backends/aoti/slim/util/SizeUtil.h>

namespace executorch::backends::aoti::slim {

/// Creates an empty tensor with the specified sizes and strides.
/// The tensor owns its underlying storage, which is allocated but
/// uninitialized.
///
/// @param sizes The sizes of each dimension.
/// @param strides The strides of each dimension.
/// @param dtype The scalar type of tensor elements.
/// @param device The target device (must be CPU).
/// @return A new SlimTensor with allocated but uninitialized storage.
inline SlimTensor empty_strided(
IntArrayRef sizes,
IntArrayRef strides,
c10::ScalarType dtype,
const c10::Device& device = CPU_DEVICE) {
Storage storage = new_storage(sizes, strides, dtype, device);
return SlimTensor(std::move(storage), sizes, strides, dtype, 0);
}

/// Creates an empty contiguous tensor with the specified sizes.
/// The tensor owns its underlying storage, which is allocated but
/// uninitialized. Strides are computed automatically to be contiguous
/// (row-major order).
///
/// @param sizes The sizes of each dimension.
/// @param dtype The scalar type of tensor elements.
/// @param device The target device (must be CPU).
/// @return A new SlimTensor with contiguous strides and uninitialized storage.
inline SlimTensor empty(
IntArrayRef sizes,
c10::ScalarType dtype,
const c10::Device& device = CPU_DEVICE) {
std::vector<int64_t> contig_strides = compute_contiguous_strides(sizes);
Storage storage =
new_storage(sizes, makeArrayRef(contig_strides), dtype, device);
return SlimTensor(
std::move(storage), sizes, makeArrayRef(contig_strides), dtype, 0);
}

/// Creates an empty contiguous tensor with sizes from an initializer list.
/// Convenience overload for creating tensors with inline size specification.
///
/// @param sizes The sizes of each dimension as an initializer list.
/// @param dtype The scalar type of tensor elements.
/// @param device The target device (must be CPU).
/// @return A new SlimTensor with contiguous strides and uninitialized storage.
inline SlimTensor empty(
std::initializer_list<int64_t> sizes,
c10::ScalarType dtype,
const c10::Device& device = CPU_DEVICE) {
return empty(makeArrayRef(sizes), dtype, device);
}

/// Creates an empty tensor with the same sizes, strides, dtype, and device as
/// another tensor.
///
/// @param other The tensor to copy metadata from.
/// @return A new SlimTensor with the same shape and properties, but
/// uninitialized storage.
inline SlimTensor empty_like(const SlimTensor& other) {
return empty_strided(
other.sizes(), other.strides(), other.dtype(), other.device());
}

} // namespace executorch::backends::aoti::slim
3 changes: 3 additions & 0 deletions backends/aoti/slim/factory/TARGETS
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
load("targets.bzl", "define_common_targets")

define_common_targets()
18 changes: 18 additions & 0 deletions backends/aoti/slim/factory/targets.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")

def define_common_targets():
"""Define targets for SlimTensor factory module."""

# Header-only library for empty tensor factory functions
runtime.cxx_library(
name = "empty",
headers = [
"Empty.h",
],
visibility = ["@EXECUTORCH_CLIENTS"],
exported_deps = [
"//executorch/backends/aoti/slim/core:slimtensor",
"//executorch/backends/aoti/slim/util:array_ref_util",
"//executorch/backends/aoti/slim/util:size_util",
],
)
3 changes: 3 additions & 0 deletions backends/aoti/slim/factory/test/TARGETS
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
load("targets.bzl", "define_common_targets")

define_common_targets()
14 changes: 14 additions & 0 deletions backends/aoti/slim/factory/test/targets.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")

def define_common_targets():
"""Define test targets for SlimTensor factory module."""

runtime.cxx_test(
name = "test_empty",
srcs = [
"test_empty.cpp",
],
deps = [
"//executorch/backends/aoti/slim/factory:empty",
],
)
Loading
Loading