Skip to content

Commit 9939f2b

Browse files
authored
Avoid zero-filling XNNPACK cache allocations (pytorch#22617) (pytorch#22617)
Summary: Back heap-backed XNNPACK cache entries with uninitialized owned byte arrays. XNNPACK initializes the allocation before packing, avoiding an implicit full-buffer clear from `std::string::resize()`. Reviewed By: digantdesai, doggeral Differential Revision: D119213262
1 parent f3c08fc commit 9939f2b

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

‎backends/xnnpack/runtime/XNNWeightsCache.cpp‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,11 +570,10 @@ void* XNNWeightsCache::reserve_space(XNNWeightsCache* context, size_t n) {
570570

571571
void* XNNWeightsCache::reserve_space_heap(size_t n) {
572572
try {
573-
std::string data_container;
574573
size_t raw_allocation_size = n + kPackedAllocationAlignment - 1;
575-
data_container.resize(raw_allocation_size);
574+
std::unique_ptr<char[]> data_container(new char[raw_allocation_size]);
576575

577-
void* maybe_aligned_space = data_container.data();
576+
void* maybe_aligned_space = data_container.get();
578577
void* aligned_space = std::align(
579578
kPackedAllocationAlignment,
580579
n,

‎backends/xnnpack/runtime/XNNWeightsCache.h‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <executorch/runtime/executor/pte_data_map.h>
1818
#include <array>
1919
#include <atomic>
20+
#include <memory>
2021
#include <mutex>
2122
#include <string>
2223
#include <unordered_map>
@@ -238,8 +239,11 @@ class XNNWeightsCache {
238239
std::unordered_map<std::string, PackedDataMeta> name_to_packed_data_metadata_;
239240
// Vector holding list of pointers to the packed data
240241
std::vector<void*> packed_data_ptrs_;
241-
// vector holding list of strings which are containers for packed_data_ptrs
242-
std::unordered_map<void*, std::string> packed_pointer_to_container_;
242+
// Owns heap allocations backing packed_data_ptrs_. XNNPACK initializes the
243+
// allocation before packing, so these buffers do not need value
244+
// initialization.
245+
std::unordered_map<void*, std::unique_ptr<char[]>>
246+
packed_pointer_to_container_;
243247
// Vector hodling list of unpacked freeable buffers
244248
std::vector<FreeableBuffer> unpacked_data_;
245249
// xnnpack's weight cache provider

0 commit comments

Comments
 (0)