-
Notifications
You must be signed in to change notification settings - Fork 685
Arm backend: Allocate the scratch buffer runtime rather than in the pte #10714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zingo
merged 1 commit into
pytorch:main
from
gggekov:Allocate_scratch_buffer_outside_pte
May 16, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,8 @@ using executorch::runtime::FreeableBuffer; | |
using executorch::runtime::MemoryAllocator; | ||
using executorch::runtime::Result; | ||
|
||
#define ETHOSU_NUM_BASE_ADDRS 3 | ||
|
||
namespace executorch { | ||
namespace backends { | ||
namespace arm { | ||
|
@@ -181,23 +183,33 @@ class EthosUBackend final : public ::executorch::runtime::BackendInterface { | |
} | ||
EXECUTORCH_PROF_END(event_tracer, event_tracer_local_scope); | ||
|
||
MemoryAllocator* temp_allocator = context.get_temp_allocator(); | ||
// Use a temporary allocator for the intermediate tensors of the | ||
// computation. The allocator is released in runtime/executor/method.cpp at | ||
// the end of the execution of the Ethos-U custom delegate | ||
char* ethosu_scratch = | ||
static_cast<char*>(temp_allocator->allocate(handles.scratch_data_size)); | ||
extern size_t ethosu_fast_scratch_size; | ||
extern unsigned char* ethosu_fast_scratch; | ||
Comment on lines
+192
to
+193
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok this is not the cleanest. Let me think of a better way to do this. |
||
ET_LOG( | ||
Debug, | ||
"EthosUBackend::execute: Running program data:\n cmd %p %zu\n weight %p %zu\n scratch %p %zu\n", | ||
"EthosUBackend::execute: Running program data:\n cmd %p %zu\n weight %p %zu\n scratch %p %zu\n fast scratch %p %zu\n", | ||
handles.cmd_data, | ||
handles.cmd_data_size, | ||
handles.weight_data, | ||
handles.weight_data_size, | ||
handles.scratch_data, | ||
handles.scratch_data_size); | ||
ethosu_scratch, | ||
handles.scratch_data_size, | ||
ethosu_fast_scratch, | ||
ethosu_fast_scratch_size); | ||
|
||
// Write argument values (from EValue tensor) into Ethos-U scratch | ||
// TODO(MLETORCH-123): Optimise into direct write from Vela into the SRAM | ||
// or DRAM output for compatible data layouts. | ||
for (int i = 0; i < handles.inputs->count; i++) { | ||
auto tensor_count = 1, io_count = 1; | ||
auto tensor_in = args[i]->toTensor(); | ||
char* scratch_addr = handles.scratch_data + handles.inputs->io[i].offset; | ||
char* scratch_addr = ethosu_scratch + handles.inputs->io[i].offset; | ||
|
||
// We accept: | ||
bool supported = 0; | ||
|
@@ -294,13 +306,17 @@ class EthosUBackend final : public ::executorch::runtime::BackendInterface { | |
// Ethos-U low level driver expected order for Ethos U-55, we have | ||
// constant weight data, then scratch (which contains input and output) | ||
// scratch is written above in this function. | ||
uint64_t bases[2] = { | ||
|
||
uint64_t bases[ETHOSU_NUM_BASE_ADDRS] = { | ||
static_cast<uint64_t>( | ||
reinterpret_cast<uintptr_t>((handles.weight_data))), | ||
static_cast<uint64_t>(reinterpret_cast<uintptr_t>(ethosu_scratch)), | ||
static_cast<uint64_t>( | ||
reinterpret_cast<uintptr_t>((handles.scratch_data)))}; | ||
size_t bases_size[2] = { | ||
handles.weight_data_size, handles.scratch_data_size}; | ||
reinterpret_cast<uintptr_t>(ethosu_fast_scratch))}; | ||
size_t bases_size[ETHOSU_NUM_BASE_ADDRS] = { | ||
handles.weight_data_size, | ||
handles.scratch_data_size, | ||
ethosu_fast_scratch_size}; | ||
int result = 0; | ||
EXECUTORCH_PROF_START( | ||
event_tracer, event_tracer_local_scope, "+EthosUBackend::execute()NPU"); | ||
|
@@ -310,7 +326,7 @@ class EthosUBackend final : public ::executorch::runtime::BackendInterface { | |
handles.cmd_data_size, | ||
bases, | ||
bases_size, | ||
2, /* fixed array of pointers to binary interface*/ | ||
3, /* fixed array of pointers to binary interface*/ | ||
nullptr); | ||
EXECUTORCH_PROF_END(event_tracer, event_tracer_local_scope); | ||
|
||
|
@@ -325,8 +341,7 @@ class EthosUBackend final : public ::executorch::runtime::BackendInterface { | |
// Write outputs from scratch into EValue pointers | ||
for (int i = 0; i < handles.outputs->count; i++) { | ||
int tensor_count = 1, io_count = 1; | ||
const char* output_addr = | ||
handles.scratch_data + handles.outputs->io[i].offset; | ||
const char* output_addr = ethosu_scratch + handles.outputs->io[i].offset; | ||
// Process input EValue into scratch | ||
// Outputs are in the index immediately after inputs | ||
auto tensor_out = args[handles.inputs->count + i]->toTensor(); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice