|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2020 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +"""Tests for compiler_opt.rl.best_trajectory.""" |
| 16 | + |
| 17 | +from absl.testing import absltest |
| 18 | +from absl.testing import parameterized |
| 19 | + |
| 20 | +import tensorflow as tf |
| 21 | + |
| 22 | +from compiler_opt.rl import best_trajectory |
| 23 | + |
| 24 | +_ACTION_NAME = 'mock' |
| 25 | + |
| 26 | + |
| 27 | +def _get_test_repo_1(): |
| 28 | + repo = best_trajectory.BestTrajectoryRepo(action_name=_ACTION_NAME) |
| 29 | + # pylint: disable=protected-access |
| 30 | + repo._best_trajectories['module_1'] = { |
| 31 | + 'function_1': |
| 32 | + best_trajectory.BestTrajectory(reward=3.4, action_list=[1, 3, 5]), |
| 33 | + 'function_2': |
| 34 | + best_trajectory.BestTrajectory(reward=1.2, action_list=[9, 7, 5]) |
| 35 | + } |
| 36 | + # pylint: enable=protected-access |
| 37 | + return repo |
| 38 | + |
| 39 | + |
| 40 | +def _get_test_repo_2(): |
| 41 | + repo = best_trajectory.BestTrajectoryRepo(action_name=_ACTION_NAME) |
| 42 | + # pylint: disable=protected-access |
| 43 | + repo._best_trajectories['module_1'] = { |
| 44 | + 'function_1': |
| 45 | + best_trajectory.BestTrajectory(reward=2.3, action_list=[1, 3]), |
| 46 | + 'function_2': |
| 47 | + best_trajectory.BestTrajectory(reward=3.4, action_list=[9, 7]) |
| 48 | + } |
| 49 | + repo._best_trajectories['module_2'] = { |
| 50 | + 'function_1': |
| 51 | + best_trajectory.BestTrajectory(reward=7.8, action_list=[2, 4, 6]), |
| 52 | + } |
| 53 | + # pylint: enable=protected-access |
| 54 | + return repo |
| 55 | + |
| 56 | + |
| 57 | +def _get_combined_repo(): |
| 58 | + repo = best_trajectory.BestTrajectoryRepo(action_name=_ACTION_NAME) |
| 59 | + # pylint: disable=protected-access |
| 60 | + repo._best_trajectories['module_1'] = { |
| 61 | + 'function_1': |
| 62 | + best_trajectory.BestTrajectory(reward=2.3, action_list=[1, 3]), |
| 63 | + 'function_2': |
| 64 | + best_trajectory.BestTrajectory(reward=1.2, action_list=[9, 7, 5]) |
| 65 | + } |
| 66 | + repo._best_trajectories['module_2'] = { |
| 67 | + 'function_1': |
| 68 | + best_trajectory.BestTrajectory(reward=7.8, action_list=[2, 4, 6]), |
| 69 | + } |
| 70 | + # pylint: enable=protected-access |
| 71 | + return repo |
| 72 | + |
| 73 | + |
| 74 | +def _create_sequence_example(action_list): |
| 75 | + example = tf.train.SequenceExample() |
| 76 | + for action in action_list: |
| 77 | + example.feature_lists.feature_list[_ACTION_NAME].feature.add( |
| 78 | + ).int64_list.value.append(action) |
| 79 | + return example.SerializeToString() |
| 80 | + |
| 81 | + |
| 82 | +class BestTrajectoryTest(parameterized.TestCase): |
| 83 | + |
| 84 | + @parameterized.named_parameters(('repo_1', _get_test_repo_1()), |
| 85 | + ('repo_2', _get_test_repo_2())) |
| 86 | + def test_sink_load_json_file(self, repo): |
| 87 | + path = self.create_tempfile().full_path |
| 88 | + repo.sink_to_json_file(path) |
| 89 | + loaded_repo = best_trajectory.BestTrajectoryRepo(action_name=_ACTION_NAME) |
| 90 | + loaded_repo.load_from_json_file(path) |
| 91 | + self.assertDictEqual(repo.best_trajectories, loaded_repo.best_trajectories) |
| 92 | + |
| 93 | + def test_sink_to_csv_file(self): |
| 94 | + path = self.create_tempfile().full_path |
| 95 | + repo = _get_test_repo_1() |
| 96 | + repo.sink_to_csv_file(path) |
| 97 | + with open(path, 'r', encoding='utf-8') as f: |
| 98 | + text = f.read() |
| 99 | + |
| 100 | + self.assertEqual(text, |
| 101 | + 'module_1,function_1,1,3,5\nmodule_1,function_2,9,7,5\n') |
| 102 | + |
| 103 | + @parameterized.named_parameters( |
| 104 | + { |
| 105 | + 'testcase_name': 'repo_1_combine_2', |
| 106 | + 'base_repo': _get_test_repo_1(), |
| 107 | + 'second_repo': _get_test_repo_2() |
| 108 | + }, { |
| 109 | + 'testcase_name': 'repo_2_combine_1', |
| 110 | + 'base_repo': _get_test_repo_2(), |
| 111 | + 'second_repo': _get_test_repo_1() |
| 112 | + }) |
| 113 | + def test_combine_with_other_repo(self, base_repo, second_repo): |
| 114 | + base_repo.combine_with_other_repo(second_repo) |
| 115 | + self.assertDictEqual(base_repo.best_trajectories, |
| 116 | + _get_combined_repo().best_trajectories) |
| 117 | + |
| 118 | + def test_update_if_better_trajectory(self): |
| 119 | + repo = _get_test_repo_1() |
| 120 | + repo.update_if_better_trajectory( |
| 121 | + 'module_1', 'function_1', 2.3, |
| 122 | + _create_sequence_example(action_list=[1, 3])) |
| 123 | + repo.update_if_better_trajectory( |
| 124 | + 'module_1', 'function_2', 3.4, |
| 125 | + _create_sequence_example(action_list=[9, 7])) |
| 126 | + repo.update_if_better_trajectory( |
| 127 | + 'module_2', 'function_1', 7.8, |
| 128 | + _create_sequence_example(action_list=[2, 4, 6])) |
| 129 | + self.assertDictEqual(repo.best_trajectories, |
| 130 | + _get_combined_repo().best_trajectories) |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == '__main__': |
| 134 | + absltest.main() |
0 commit comments