diff --git a/runtime/executor/method.cpp b/runtime/executor/method.cpp index 1c9a8a5463b..7d35ebe5054 100644 --- a/runtime/executor/method.cpp +++ b/runtime/executor/method.cpp @@ -670,7 +670,7 @@ Error Method::resolve_operator( size_t kernel_index, InstructionArgs args, size_t n_args) { - // TODO(T153505381, T153506819) Investigate optimizing this function for both + // TODO(T153506819) Investigate optimizing this function for both // space and time. // resolve name @@ -691,8 +691,16 @@ Error Method::resolve_operator( } // resolve tensor meta - auto method_allocator = memory_manager_->method_allocator(); - TensorMeta* meta = method_allocator->allocateList(n_args); + // Since temp allocator can be freed, we optimistically + // try to use that allocator first. + auto allocator = memory_manager_->temp_allocator(); + // However, it does not have to be provided, so if it + // is not provided (or an empty one is provided), we + // fall back to the method allocator. + if (allocator == nullptr || allocator->size() == 0) { + allocator = memory_manager_->method_allocator(); + } + TensorMeta* meta = allocator->allocateList(n_args); if (meta == nullptr) { return Error::MemoryAllocationFailed; } @@ -705,8 +713,7 @@ Error Method::resolve_operator( auto tensor = eval->toTensor(); meta[count].dtype_ = tensor.scalar_type(); executorch::aten::DimOrderType* dim_order_ptr = - method_allocator->allocateList( - tensor.dim()); + allocator->allocateList(tensor.dim()); if (dim_order_ptr == nullptr) { return Error::MemoryAllocationFailed; }