|
| 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 | +"""Module for training an inlining policy with imitation learning.""" |
| 16 | + |
| 17 | +from absl import app |
| 18 | +from absl import flags |
| 19 | +from absl import logging |
| 20 | + |
| 21 | +import gin |
| 22 | +import json |
| 23 | +from compiler_opt.rl import policy_saver |
| 24 | + |
| 25 | +from compiler_opt.rl.inlining import imitation_learning_config as config |
| 26 | + |
| 27 | +from compiler_opt.rl.imitation_learning.weighted_bc_trainer_lib import TrainingWeights |
| 28 | +from compiler_opt.rl.imitation_learning.weighted_bc_trainer_lib import ImitationLearningTrainer |
| 29 | +from compiler_opt.rl.imitation_learning.weighted_bc_trainer_lib import WrapKerasModel |
| 30 | + |
| 31 | +_TRAINING_DATA = flags.DEFINE_multi_string( |
| 32 | + 'training_data', None, 'Training data for one step of BC-Max') |
| 33 | +_PROFILING_DATA = flags.DEFINE_multi_string( |
| 34 | + 'profiling_data', None, |
| 35 | + ('Paths to profile files for computing the TrainingWeights' |
| 36 | + 'If specified the order for each pair of json files is' |
| 37 | + 'comparator.json followed by eval.json and the number of' |
| 38 | + 'files should always be even.')) |
| 39 | +_SAVE_MODEL_DIR = flags.DEFINE_string( |
| 40 | + 'save_model_dir', None, 'Location to save the keras and TFAgents policies.') |
| 41 | +_GIN_FILES = flags.DEFINE_multi_string( |
| 42 | + 'gin_files', [], 'List of paths to gin configuration files.') |
| 43 | +_GIN_BINDINGS = flags.DEFINE_multi_string( |
| 44 | + 'gin_bindings', [], |
| 45 | + 'Gin bindings to override the values set in the config files.') |
| 46 | + |
| 47 | + |
| 48 | +def train(): |
| 49 | + training_weights = None |
| 50 | + if _PROFILING_DATA.value: |
| 51 | + if len(_PROFILING_DATA.value) % 2 != 0: |
| 52 | + raise ValueError('Profiling file paths should always be an even number.') |
| 53 | + training_weights = TrainingWeights() |
| 54 | + for i in range(len(_PROFILING_DATA.value) // 2): |
| 55 | + with open( |
| 56 | + _PROFILING_DATA.value[2 * i], encoding='utf-8') as comp_f, open( |
| 57 | + _PROFILING_DATA.value[2 * i + 1], encoding='utf-8') as eval_f: |
| 58 | + comparator_prof = json.load(comp_f) |
| 59 | + eval_prof = json.load(eval_f) |
| 60 | + training_weights.update_weights( |
| 61 | + comparator_profile=comparator_prof, policy_profile=eval_prof) |
| 62 | + trainer = ImitationLearningTrainer( |
| 63 | + save_model_dir=_SAVE_MODEL_DIR.value, training_weights=training_weights) |
| 64 | + trainer.train(filepaths=_TRAINING_DATA.value) |
| 65 | + if _SAVE_MODEL_DIR.value: |
| 66 | + keras_policy = trainer.get_policy() |
| 67 | + expected_signature, action_spec = config.get_input_signature() |
| 68 | + wrapped_keras_model = WrapKerasModel( |
| 69 | + keras_policy=keras_policy, |
| 70 | + time_step_spec=expected_signature, |
| 71 | + action_spec=action_spec) |
| 72 | + policy_dict = {'tf_agents_policy': wrapped_keras_model} |
| 73 | + saver = policy_saver.PolicySaver(policy_dict=policy_dict) |
| 74 | + saver.save(_SAVE_MODEL_DIR.value) |
| 75 | + |
| 76 | + |
| 77 | +def main(_): |
| 78 | + gin.parse_config_files_and_bindings( |
| 79 | + _GIN_FILES.value, _GIN_BINDINGS.value, skip_unknown=False) |
| 80 | + logging.info(gin.config_str()) |
| 81 | + |
| 82 | + train() |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == '__main__': |
| 86 | + app.run(main) |
0 commit comments