1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/compiler/xla/service/memory_space_assignment_tuning_utils.h"
17 
18 #include "tensorflow/compiler/xla/service/memory_space_assignment_utils.h"
19 namespace xla {
20 
21 namespace memory_space_assignment {
22 
CustomizeSortedBufferInterval(std::optional<std::vector<uint64_t>> memory_space_assignment_config,std::vector<BufferInterval> & sorted_buffer_intervals)23 void CustomizeSortedBufferInterval(
24     std::optional<std::vector<uint64_t>> memory_space_assignment_config,
25     std::vector<BufferInterval>& sorted_buffer_intervals) {
26   // A copy of the sorted buffer intervals to assist the creating of the
27   // customized buffer intervals vector respecting the config.
28   std::vector<BufferInterval> sorted_buffer_intervals_copy(
29       sorted_buffer_intervals);
30 
31   std::vector<uint64_t> config;
32   if (!memory_space_assignment_config.has_value()) {
33     config.resize(sorted_buffer_intervals_copy.size());
34     absl::c_iota(config, 0);
35   } else {
36     config = *memory_space_assignment_config;
37   }
38 
39   CHECK_EQ(config.size(), sorted_buffer_intervals_copy.size());
40   sorted_buffer_intervals.clear();
41   for (int i = 0; i < config.size(); ++i) {
42     sorted_buffer_intervals.push_back(sorted_buffer_intervals_copy[config[i]]);
43   }
44 
45   if (!sorted_buffer_intervals.empty()) {
46     HloModule* module =
47         sorted_buffer_intervals[0].buffer->instruction()->GetModule();
48 
49     // Update the memory space assignment auto-tuning config of a module with a
50     // given config.
51     module->config().mutable_memory_space_assignment_config()->assign(
52         config.begin(), config.end());
53   }
54 }
55 
56 }  // namespace memory_space_assignment
57 }  // namespace xla
58