#!/usr/bin/env python3
# Copyright 2018 Google LLC
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""Tiny wrapper for t2t-trainer that automatically loads genie.

Usage:
genie-trainer
  --data_dir ./workdir
  --problem semparse_thingtalk_noquote
  --model genie_copy_transformer
  --hparams_set transformer_tiny_genie
  --output_dir ./workdir-small/model
"""

# Part of this code is derived from Tensor2Tensor, which is:
# Copyright 2018 The Tensor2Tensor Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys
import tensorflow as tf
from tensor2tensor.bin import t2t_trainer
from genieparser.scripts import avg_checkpoints

flags = tf.flags
FLAGS = tf.app.flags.FLAGS

import genieparser

LUI_prob = False
LUI_model = False

flags.DEFINE_bool("perturb_training", False, "apply training trick")
flags.DEFINE_integer("num_loops", 2, "number of training loops")

# modify loss according to the class (Synthetic, Paraphrase, Augmented) each sentence belongs to
flags.DEFINE_bool("curriculum", False, "use curriculum learning")


if 'semparse_thingtalk' in FLAGS.problem or 'semparse_posthingtalk' in FLAGS.problem:
    LUI_prob = True

if "genie" in FLAGS.model:
    LUI_model = True
if LUI_prob != LUI_model:
    tf.logging.error('** Be advised that LUINetProblems should use LUINetModels while regular t2t.Problems should use t2t.Models **')
    sys.exit(1)

def main(argv):
    # strip off commas to avoid hparams parsing issues
    FLAGS.hparams = FLAGS.hparams.strip(',')
    output_dir = FLAGS.output_dir
    t2t_trainer.main(argv)

    if FLAGS.perturb_training:
        tf.logging.info('Continue training...')

        for i in range(FLAGS.num_loops):
            FLAGS.num_last_checkpoints = FLAGS.num_last_checkpoints if FLAGS.num_last_checkpoints > 0 else 3
            FLAGS.prefix = os.path.join(FLAGS.output_dir, 'model.ckpt')
            FLAGS.output_path = os.path.join(output_dir, 'averaged_{}/model.ckpt'.format(i))
            avg_checkpoints.main(argv)
            FLAGS.output_dir = os.path.join(output_dir, 'averaged_{}'.format(i))
            FLAGS.hparams = FLAGS.hparams.strip(',')
            t2t_trainer.main(argv)

        tf.logging.info("Done training!")

if __name__ == "__main__":
    tf.app.run()
