forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputs.h
More file actions
118 lines (102 loc) · 3.53 KB
/
inputs.h
File metadata and controls
118 lines (102 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
* Copyright 2025 Arm Limited and/or its affiliates.
*
* 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 <vector>
#include <executorch/runtime/core/span.h>
#include <executorch/runtime/executor/method.h>
#include <executorch/runtime/executor/method_meta.h>
namespace executorch {
namespace extension {
using ::executorch::ET_RUNTIME_NAMESPACE::Method;
using ::executorch::ET_RUNTIME_NAMESPACE::TensorInfo;
/**
* RAII helper that frees a set of buffers when destroyed. Movable.
*/
class BufferCleanup final {
public:
/**
* Takes ownership of `buffers.data()` and the elements of `buffers`, which
* each will be passed to `free()` when the object is destroyed.
*/
explicit BufferCleanup(executorch::runtime::Span<void*> buffers)
: buffers_(buffers) {}
/**
* Move ctor. Takes ownership of the data previously owned by `rhs`, leaving
* `rhs` with an empty list of buffers.
*/
BufferCleanup(BufferCleanup&& rhs) noexcept : buffers_(rhs.buffers_) {
rhs.buffers_ = executorch::runtime::Span<void*>();
}
~BufferCleanup() {
for (auto buffer : buffers_) {
free(buffer);
}
free(buffers_.data());
}
private:
// Delete other rule-of-five methods.
BufferCleanup(const BufferCleanup&) = delete;
BufferCleanup& operator=(const BufferCleanup&) = delete;
BufferCleanup& operator=(BufferCleanup&&) noexcept = delete;
executorch::runtime::Span<void*> buffers_;
};
/// Defines options for `prepare_input_tensors()`.
struct PrepareInputTensorsOptions {
/**
* The maximum total size in bytes of all input tensors. If the total size of
* all inputs exceeds this, an error is returned. This prevents allocating too
* much memory if the PTE file is malformed.
*/
size_t max_total_allocation_size = 1024 * 1024 * 1024;
/**
* The maximum number of inputs to allocate. If the number of inputs exceeds
* this, an error is returned. This prevents allocating too much memory if the
* PTE file is malformed.
*/
size_t max_inputs = 1024;
};
/**
* Allocates input tensors for the provided Method, filling them with ones. Does
* not modify inputs that are not Tensors.
*
* @param[in] method The Method that owns the inputs to prepare.
* @param[in] options Extra options for preparing the inputs.
*
* @returns On success, an object that owns any allocated tensor memory. It must
* remain alive when calling `method->execute()`.
* @returns An error on failure.
*/
executorch::runtime::Result<BufferCleanup> prepare_input_tensors(
Method& method,
PrepareInputTensorsOptions options = {},
const std::vector<std::pair<char*, size_t>>& input_buffers = {});
namespace internal {
/**
* INTERNAL-ONLY: Creates a Tensor using the provided shape and buffer,
* fills it with ones by default, and sets the input at `input_index`.
*/
executorch::runtime::Error fill_and_set_input(
Method& method,
TensorInfo& tensor_meta,
size_t input_index,
void* data_ptr,
bool fill_tensor = true);
} // namespace internal
} // namespace extension
} // namespace executorch
namespace torch {
namespace executor {
namespace util {
// TODO(T197294990): Remove these deprecated aliases once all users have moved
// to the new `::executorch` namespaces.
using ::executorch::extension::BufferCleanup;
using ::executorch::extension::prepare_input_tensors;
} // namespace util
} // namespace executor
} // namespace torch