Skip to content

Commit 6ac99a1

Browse files
David Linfacebook-github-bot
authored andcommitted
Add allocate_temp method to KernelRuntimeContext
Summary: This adds an `allocate_temp` method to KernelRuntimeContext, and passes the temporary memory allocator from `execute_instruction`. The method returns a result that errors if the temporary `MemoryAllocator` was not provided or the memory could not be allocated. Differential Revision: D56421957
1 parent 8dc54d5 commit 6ac99a1

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

runtime/executor/method.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,8 @@ Error Method::execute_instruction() {
10151015
internal::EventTracerProfileScope(event_tracer_, "OPERATOR_CALL");
10161016
// TODO(T147221312): Also expose the temp allocator and tensor resizer
10171017
// via the context.
1018-
KernelRuntimeContext context(event_tracer_);
1018+
KernelRuntimeContext context(
1019+
event_tracer_, memory_manager_->temp_allocator());
10191020
auto args = chain.argument_lists_[step_state_.instr_idx];
10201021
chain.kernels_[step_state_.instr_idx](context, args.data());
10211022
err = context.failure_state();

runtime/kernel/kernel_runtime_context.h

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include <executorch/runtime/core/error.h>
1212
#include <executorch/runtime/core/event_tracer_hooks.h>
13+
#include <executorch/runtime/core/memory_allocator.h>
14+
#include <executorch/runtime/core/result.h>
1315
#include <executorch/runtime/platform/compiler.h>
1416

1517
namespace torch {
@@ -24,10 +26,13 @@ namespace executor {
2426
class KernelRuntimeContext {
2527
public:
2628
/**
27-
* Construct a new kernel runtime context along with an optional event tracer.
29+
* Construct a new kernel runtime context along with an optional event tracer
30+
* and an optional memory allocator.
2831
*/
29-
KernelRuntimeContext(EventTracer* event_tracer = nullptr)
30-
: event_tracer_(event_tracer) {}
32+
KernelRuntimeContext(
33+
EventTracer* event_tracer = nullptr,
34+
MemoryAllocator* temp_allocator = nullptr)
35+
: event_tracer_(event_tracer), temp_allocator_(temp_allocator) {}
3136
/**
3237
* Tells the runtime that the kernel call has failed. Prefer this over
3338
* ET_CHECK_*(), which fatally panics the process/system.
@@ -59,13 +64,29 @@ class KernelRuntimeContext {
5964
EventTracer* internal_event_tracer() {
6065
return event_tracer_;
6166
}
62-
63-
// TODO(T147221312): Add a way to allocate temporary memory.
67+
/**
68+
* Allocates temporary memory that will be freed when the kernel returns. This
69+
* returns a pointer to the allocated memory, or an error if there is no
70+
* MemoryAllocator provided or the allocation fails.
71+
*/
72+
Result<void*> allocate_temp(
73+
size_t size,
74+
size_t alignment = MemoryAllocator::kDefaultAlignment) {
75+
ET_CHECK_OR_RETURN_ERROR(
76+
temp_allocator_ != nullptr, NotFound, "No temp allocator provided");
77+
void* temp_memory = temp_allocator_->allocate(size, alignment);
78+
ET_CHECK_OR_RETURN_ERROR(
79+
temp_memory != nullptr,
80+
MemoryAllocationFailed,
81+
"Failed to allocate temp memory");
82+
return temp_memory;
83+
}
6484

6585
// TODO(T147221312): Add a way to resize a tensor.
6686

6787
private:
6888
EventTracer* event_tracer_ = nullptr;
89+
MemoryAllocator* temp_allocator_ = nullptr;
6990
Error failure_state_ = Error::Ok;
7091
};
7192

runtime/kernel/targets.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def define_common_targets():
5555
exported_deps = [
5656
"//executorch/runtime/core:core",
5757
"//executorch/runtime/platform:platform",
58+
"//executorch/runtime/core:memory_allocator",
5859
"//executorch/runtime/core:event_tracer" + aten_suffix,
5960
# TODO(T147221312): This will eventually depend on exec_aten
6061
# once KernelRuntimeContext support tensor resizing, which is

0 commit comments

Comments
 (0)