Skip to content

Commit 414fc32

Browse files
authored
Use temp allocator for kernel registry (pytorch#13012)
Summary: When indexing to the registry to get the op, memory is allocated from the method allocator to instantiate some TensorMeta and included objects. This memory is only used for that purpose and is not needed for the entire lifetime of the Method. Thus, we can instead use temp allocator which can later be reset and free up memory as needed. Differential Revision: D79285675 cc @larryliu0820 @JacobSzwejbka @lucylq
1 parent 089d218 commit 414fc32

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

runtime/executor/method.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ Error Method::resolve_operator(
670670
size_t kernel_index,
671671
InstructionArgs args,
672672
size_t n_args) {
673-
// TODO(T153505381, T153506819) Investigate optimizing this function for both
673+
// TODO(T153506819) Investigate optimizing this function for both
674674
// space and time.
675675

676676
// resolve name
@@ -691,8 +691,16 @@ Error Method::resolve_operator(
691691
}
692692

693693
// resolve tensor meta
694-
auto method_allocator = memory_manager_->method_allocator();
695-
TensorMeta* meta = method_allocator->allocateList<TensorMeta>(n_args);
694+
// Since temp allocator can be freed, we optimistically
695+
// try to use that allocator first.
696+
auto allocator = memory_manager_->temp_allocator();
697+
// However, it does not have to be provided, so if it
698+
// is not provided (or an empty one is provided), we
699+
// fall back to the method allocator.
700+
if (allocator == nullptr || allocator->size() == 0) {
701+
allocator = memory_manager_->method_allocator();
702+
}
703+
TensorMeta* meta = allocator->allocateList<TensorMeta>(n_args);
696704
if (meta == nullptr) {
697705
return Error::MemoryAllocationFailed;
698706
}
@@ -705,8 +713,7 @@ Error Method::resolve_operator(
705713
auto tensor = eval->toTensor();
706714
meta[count].dtype_ = tensor.scalar_type();
707715
executorch::aten::DimOrderType* dim_order_ptr =
708-
method_allocator->allocateList<executorch::aten::DimOrderType>(
709-
tensor.dim());
716+
allocator->allocateList<executorch::aten::DimOrderType>(tensor.dim());
710717
if (dim_order_ptr == nullptr) {
711718
return Error::MemoryAllocationFailed;
712719
}

0 commit comments

Comments
 (0)