xref: /aosp_15_r20/external/executorch/extension/runner_util/inputs.h (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
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/runtime/core/span.h>
12 #include <executorch/runtime/executor/method.h>
13 #include <executorch/runtime/executor/method_meta.h>
14 
15 namespace executorch {
16 namespace extension {
17 
18 /**
19  * RAII helper that frees a set of buffers when destroyed. Movable.
20  */
21 class BufferCleanup final {
22  public:
23   /**
24    * Takes ownership of `buffers.data()` and the elements of `buffers`, which
25    * each will be passed to `free()` when the object is destroyed.
26    */
BufferCleanup(executorch::runtime::Span<void * > buffers)27   explicit BufferCleanup(executorch::runtime::Span<void*> buffers)
28       : buffers_(buffers) {}
29 
30   /**
31    * Move ctor. Takes ownership of the data previously owned by `rhs`, leaving
32    * `rhs` with an empty list of buffers.
33    */
BufferCleanup(BufferCleanup && rhs)34   BufferCleanup(BufferCleanup&& rhs) noexcept : buffers_(rhs.buffers_) {
35     rhs.buffers_ = executorch::runtime::Span<void*>();
36   }
37 
~BufferCleanup()38   ~BufferCleanup() {
39     for (auto buffer : buffers_) {
40       free(buffer);
41     }
42     free(buffers_.data());
43   }
44 
45  private:
46   // Delete other rule-of-five methods.
47   BufferCleanup(const BufferCleanup&) = delete;
48   BufferCleanup& operator=(const BufferCleanup&) = delete;
49   BufferCleanup& operator=(BufferCleanup&&) noexcept = delete;
50 
51   executorch::runtime::Span<void*> buffers_;
52 };
53 
54 /**
55  * Allocates input tensors for the provided Method, filling them with ones. Does
56  * not modify inputs that are not Tensors.
57  *
58  * @param[in] method The Method that owns the inputs to prepare.
59  *
60  * @returns On success, an object that owns any allocated tensor memory. It must
61  *     remain alive when calling `method->execute()`.
62  * @returns An error on failure.
63  */
64 executorch::runtime::Result<BufferCleanup> prepare_input_tensors(
65     executorch::runtime::Method& method);
66 
67 namespace internal {
68 /**
69  * INTERNAL-ONLY: Creates a Tensor using the provided shape and buffer,
70  * fills it with ones, and sets the input at `input_index`.
71  */
72 executorch::runtime::Error fill_and_set_input(
73     executorch::runtime::Method& method,
74     executorch::runtime::TensorInfo& tensor_meta,
75     size_t input_index,
76     void* data_ptr);
77 } // namespace internal
78 
79 } // namespace extension
80 } // namespace executorch
81 
82 namespace torch {
83 namespace executor {
84 namespace util {
85 // TODO(T197294990): Remove these deprecated aliases once all users have moved
86 // to the new `::executorch` namespaces.
87 using ::executorch::extension::BufferCleanup;
88 using ::executorch::extension::prepare_input_tensors;
89 } // namespace util
90 } // namespace executor
91 } // namespace torch
92