xref: /aosp_15_r20/external/XNNPACK/src/xnnpack/memory-planner.h (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1*4bdc9457SAndroid Build Coastguard Worker // Copyright 2020 Google LLC
2*4bdc9457SAndroid Build Coastguard Worker //
3*4bdc9457SAndroid Build Coastguard Worker // This source code is licensed under the BSD-style license found in the
4*4bdc9457SAndroid Build Coastguard Worker // LICENSE file in the root directory of this source tree.
5*4bdc9457SAndroid Build Coastguard Worker 
6*4bdc9457SAndroid Build Coastguard Worker #pragma once
7*4bdc9457SAndroid Build Coastguard Worker 
8*4bdc9457SAndroid Build Coastguard Worker #include <stdbool.h>
9*4bdc9457SAndroid Build Coastguard Worker #include <stddef.h>
10*4bdc9457SAndroid Build Coastguard Worker #include <stdint.h>
11*4bdc9457SAndroid Build Coastguard Worker 
12*4bdc9457SAndroid Build Coastguard Worker #include <xnnpack.h>
13*4bdc9457SAndroid Build Coastguard Worker #include <xnnpack/common.h>
14*4bdc9457SAndroid Build Coastguard Worker #include <xnnpack/allocator.h>
15*4bdc9457SAndroid Build Coastguard Worker 
16*4bdc9457SAndroid Build Coastguard Worker #ifdef __cplusplus
17*4bdc9457SAndroid Build Coastguard Worker extern "C" {
18*4bdc9457SAndroid Build Coastguard Worker #endif
19*4bdc9457SAndroid Build Coastguard Worker 
20*4bdc9457SAndroid Build Coastguard Worker struct xnn_value_usage {
21*4bdc9457SAndroid Build Coastguard Worker   // The index (to xnn_subgraph_t->nodes) of the first xnn_node that uses this xnn_value.
22*4bdc9457SAndroid Build Coastguard Worker   uint32_t first_node;
23*4bdc9457SAndroid Build Coastguard Worker   // The index of the last xnn_node that uses this xnn_value.
24*4bdc9457SAndroid Build Coastguard Worker   uint32_t last_node;
25*4bdc9457SAndroid Build Coastguard Worker   // Note that 'tensor_size' includes the padding of XNN_EXTRA_BYTES.
26*4bdc9457SAndroid Build Coastguard Worker   size_t tensor_size;
27*4bdc9457SAndroid Build Coastguard Worker   // The memory offset of this xnn_value from the beginning of a memory buffer.
28*4bdc9457SAndroid Build Coastguard Worker   size_t alloc_offset;
29*4bdc9457SAndroid Build Coastguard Worker };
30*4bdc9457SAndroid Build Coastguard Worker 
31*4bdc9457SAndroid Build Coastguard Worker // Track the memory allocation in a memory arena for a subgraph.
32*4bdc9457SAndroid Build Coastguard Worker struct xnn_value_allocation_tracker {
33*4bdc9457SAndroid Build Coastguard Worker   xnn_subgraph_t subgraph;
34*4bdc9457SAndroid Build Coastguard Worker   size_t mem_arena_size;
35*4bdc9457SAndroid Build Coastguard Worker   // Representing the lifecycle of xnn_values in the 'subgraph', and the array size is 'subgraph->num_values'.
36*4bdc9457SAndroid Build Coastguard Worker   struct xnn_value_usage* usage;
37*4bdc9457SAndroid Build Coastguard Worker   // The range of value ids (i.e. the index to subgraph->values) whose memory might need to be allocated.
38*4bdc9457SAndroid Build Coastguard Worker   size_t min_value_id;
39*4bdc9457SAndroid Build Coastguard Worker   size_t max_value_id;
40*4bdc9457SAndroid Build Coastguard Worker };
41*4bdc9457SAndroid Build Coastguard Worker 
42*4bdc9457SAndroid Build Coastguard Worker // Initialize the memory allocation tracker for xnn_values.
43*4bdc9457SAndroid Build Coastguard Worker XNN_INTERNAL void xnn_init_value_allocation_tracker(struct xnn_value_allocation_tracker* tracker,
44*4bdc9457SAndroid Build Coastguard Worker                                                     const xnn_subgraph_t subgraph);
45*4bdc9457SAndroid Build Coastguard Worker 
xnn_release_value_allocation_tracker(struct xnn_value_allocation_tracker * tracker)46*4bdc9457SAndroid Build Coastguard Worker inline static void xnn_release_value_allocation_tracker(struct xnn_value_allocation_tracker* tracker) {
47*4bdc9457SAndroid Build Coastguard Worker   xnn_release_memory(tracker->usage);
48*4bdc9457SAndroid Build Coastguard Worker }
49*4bdc9457SAndroid Build Coastguard Worker 
50*4bdc9457SAndroid Build Coastguard Worker // Add a to-be-allocated xnn_value (referred by 'value_id') of size 'tensor_size' to the allocation tracker.
51*4bdc9457SAndroid Build Coastguard Worker // Note: this function assumes 'value_id's added in increasing order for simplicity as it's called inside a loop
52*4bdc9457SAndroid Build Coastguard Worker // iterating over 'subgraph->values'.
53*4bdc9457SAndroid Build Coastguard Worker XNN_INTERNAL void xnn_add_value_allocation_tracker(struct xnn_value_allocation_tracker* tracker,
54*4bdc9457SAndroid Build Coastguard Worker                                                    uint32_t value_id, size_t tensor_size);
55*4bdc9457SAndroid Build Coastguard Worker 
56*4bdc9457SAndroid Build Coastguard Worker // Plan the exact the memory allocation for intermediate tensors according to the xnn_value allocation tracker.
57*4bdc9457SAndroid Build Coastguard Worker XNN_INTERNAL void xnn_plan_value_allocation_tracker(struct xnn_value_allocation_tracker* tracker);
58*4bdc9457SAndroid Build Coastguard Worker 
59*4bdc9457SAndroid Build Coastguard Worker #ifdef __cplusplus
60*4bdc9457SAndroid Build Coastguard Worker }  // extern "C"
61*4bdc9457SAndroid Build Coastguard Worker #endif
62