|
| 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 generate_default_trace.""" |
| 16 | + |
| 17 | +import os |
| 18 | +from unittest import mock |
| 19 | + |
| 20 | +from absl import flags |
| 21 | +from absl.testing import absltest |
| 22 | +from absl.testing import flagsaver |
| 23 | +import gin |
| 24 | +import tensorflow as tf |
| 25 | + |
| 26 | +# This is https://github.com/google/pytype/issues/764 |
| 27 | +from google.protobuf import text_format # pytype: disable=pyi-error |
| 28 | +from compiler_opt.rl import compilation_runner |
| 29 | +from compiler_opt.tools import generate_default_trace |
| 30 | + |
| 31 | +flags.FLAGS['num_workers'].allow_override = True |
| 32 | + |
| 33 | + |
| 34 | +class MockCompilationRunner(compilation_runner.CompilationRunner): |
| 35 | + """A compilation runner just for test.""" |
| 36 | + |
| 37 | + def collect_data(self, file_paths, tf_policy_path, reward_stat): |
| 38 | + sequence_example_text = """ |
| 39 | + feature_lists { |
| 40 | + feature_list { |
| 41 | + key: "feature_0" |
| 42 | + value { |
| 43 | + feature { int64_list { value: 1 } } |
| 44 | + feature { int64_list { value: 1 } } |
| 45 | + } |
| 46 | + } |
| 47 | + }""" |
| 48 | + sequence_example = text_format.Parse(sequence_example_text, |
| 49 | + tf.train.SequenceExample()) |
| 50 | + |
| 51 | + return compilation_runner.CompilationResult( |
| 52 | + sequence_examples=[sequence_example], |
| 53 | + reward_stats={ |
| 54 | + 'default': |
| 55 | + compilation_runner.RewardStat( |
| 56 | + default_reward=1, moving_average_reward=2) |
| 57 | + }, |
| 58 | + rewards=[1.2], |
| 59 | + keys=['default']) |
| 60 | + |
| 61 | + |
| 62 | +class GenerateDefaultTraceTest(absltest.TestCase): |
| 63 | + |
| 64 | + @mock.patch('compiler_opt.tools.generate_default_trace.get_runner') |
| 65 | + def test_api(self, mock_get_runner): |
| 66 | + tmp_dir = self.create_tempdir() |
| 67 | + module_names = ['a', 'b', 'c', 'd'] |
| 68 | + |
| 69 | + with tf.io.gfile.GFile( |
| 70 | + os.path.join(tmp_dir.full_path, 'module_paths'), 'w') as f: |
| 71 | + f.write('\n'.join(module_names)) |
| 72 | + |
| 73 | + for module_name in module_names: |
| 74 | + with tf.io.gfile.GFile( |
| 75 | + os.path.join(tmp_dir.full_path, module_name + '.bc'), 'w') as f: |
| 76 | + f.write(module_name) |
| 77 | + |
| 78 | + mock_compilation_runner = MockCompilationRunner() |
| 79 | + mock_get_runner.return_value = mock_compilation_runner |
| 80 | + |
| 81 | + with flagsaver.flagsaver( |
| 82 | + data_path=tmp_dir.full_path, |
| 83 | + num_workers=2, |
| 84 | + output_path=os.path.join(tmp_dir.full_path, 'output'), |
| 85 | + output_performance_path=os.path.join(tmp_dir.full_path, |
| 86 | + 'output_performance'), |
| 87 | + ): |
| 88 | + generate_default_trace.main(None) |
| 89 | + |
| 90 | + def test_get_runner(self): |
| 91 | + with gin.unlock_config(): |
| 92 | + gin.parse_config_files_and_bindings( |
| 93 | + config_files=['compiler_opt/rl/inlining/gin_configs/common.gin'], |
| 94 | + bindings=None) |
| 95 | + runner = generate_default_trace.get_runner() |
| 96 | + self.assertIsInstance(runner, compilation_runner.CompilationRunner) |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == '__main__': |
| 100 | + absltest.main() |
0 commit comments