Line data Source code
1 : // SPDX-License-Identifier: Apache-2.0
2 : /**
3 : * Copyright (C) 2022 Jijoong.Moon <jijoong.moon@samsung.com>
4 : *
5 : * @file optimzied_v2_planner.h
6 : * @date 29 December 2022
7 : * @see https://github.com/nnstreamer/nntrainer
8 : * @author Parichay Kapoor <pk.kapoor@samsung.com>
9 : * @author Jijoong Moon <jijoong.moon@samsung.com>
10 : * @bug No known bugs except for NYI items
11 : * @brief This is Optimized V2 Memory Planner
12 : *
13 : * @note This planner has been design to give reduced memory usage for training
14 : * and might not perform very well for inference.
15 : *
16 : * @details The principle for this planner is to give memory to the requests in
17 : * the order of the start of their validity.
18 : * This takes advantage of the pattern that the outputs of the layer nodes
19 : * allocated during forwarding are also used during backwarding as well.
20 : *
21 : * If two memory requests have the same start time, then the memory request with
22 : * higher end is allocated first. This is to minimize the fragmentation once the
23 : * memory is being freed.
24 : *
25 : * The assigned memories are cached, and once their validity is finished, they
26 : * are freed and reused for the next allocations.
27 : */
28 :
29 : #ifndef __OPTIMIZED_V2_PLANNER_H_
30 : #define __OPTIMIZED_V2_PLANNER_H_
31 :
32 : #include <vector>
33 :
34 : #include <memory_planner.h>
35 :
36 : namespace nntrainer {
37 :
38 : /**
39 : * @class OptimizedV2Planner
40 : * @brief Optimized V2 Memory Planner provides the optimized plan for memory
41 : * layout
42 : * @details optimized planner performs sharing of overlapping memory sharing
43 : * upto certain extent
44 : */
45 : class OptimizedV2Planner : public MemoryPlanner {
46 : public:
47 : /**
48 : * @brief OptimizedV1Planner destructor
49 : *
50 : */
51 : OptimizedV2Planner() = default;
52 :
53 : /**
54 : * @copydoc MemoryPlanner::planLayout(
55 : * const std::vector<size_t> &memory_size,
56 : * const std::vector<std::pair<unsigned int, unsigned int>> &memory_validity,
57 : * std::vector<size_t> &memory_offset,
58 : * std::vector<bool> &memory_is_wgrad);
59 : *
60 : */
61 : size_t planLayout(
62 : const std::vector<size_t> &memory_size,
63 : const std::vector<std::pair<unsigned int, unsigned int>> &memory_validity,
64 : std::vector<size_t> &memory_offset, std::vector<bool> &memory_is_wgrad,
65 : size_t n_wgrad = 0) const;
66 :
67 : /**
68 : * @copydoc MemoryPlanner::getType() const
69 : *
70 : */
71 0 : const std::string getType() const { return type; }
72 :
73 : static constexpr const char *type = "optimized_v2_planner";
74 : };
75 :
76 : } // namespace nntrainer
77 :
78 : #endif /** __OPTIMIZED_V2_PLANNER_H_ */
|