#!/usr/bin/env python
# Copyright 2017 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.

r"""Trainer for T2T models.

This binary perform training, evaluation, and inference using
the Estimator API with tf.learn Experiment objects.

To train your model, for example:
  t2t-trainer \
      --data_dir ~/data \
      --problems=algorithmic_identity_binary40 \
      --model=transformer
      --hparams_set=transformer_base
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import importlib
import os
import sys

# Dependency imports

from tensor2tensor.utils import eval_utils as utils

import tensorflow as tf

flags = tf.flags
FLAGS = flags.FLAGS

flags.DEFINE_string("t2t_usr_dir", "",
                    "Path to a Python module that will be imported. The "
                    "__init__.py file should include the necessary imports. "
                    "The imported files should contain registrations, "
                    "e.g. @registry.register_model calls, that will then be "
                    "available to the t2t-trainer.")


def import_usr_dir():
  """Import module at FLAGS.t2t_usr_dir, if provided."""
  if not FLAGS.t2t_usr_dir:
    return
  dir_path = os.path.expanduser(FLAGS.t2t_usr_dir)
  if dir_path[-1] == "/":
    dir_path = dir_path[:-1]
  containing_dir, module_name = os.path.split(dir_path)
  tf.logging.info("Importing user module %s from path %s", module_name,
                  containing_dir)
  sys.path.insert(0, containing_dir)
  importlib.import_module(module_name)
  sys.path.pop(0)


def main(_):
  tf.logging.set_verbosity(tf.logging.INFO)
  #tf.set_random_seed(FLAGS.random_seed)
  import_usr_dir()
  #printparameters()
  utils.log_registry()
  utils.validate_flags()
  utils.run(
      data_dir=FLAGS.data_dir,
      model=FLAGS.model,
      output_dir=FLAGS.output_dir,
      train_steps=FLAGS.train_steps,
      eval_steps=FLAGS.eval_steps,
      schedule=FLAGS.schedule)

def printparameters():
  if not FLAGS.decode_from_file:
    tf.logging.info("=======================================================")
    tf.logging.info("training parameters are as follows:")
    tf.logging.info("data_dir= %s", FLAGS.data_dir)
    tf.logging.info("problem= %s",FLAGS.problems)
    tf.logging.info("model= %s",FLAGS.model)
    tf.logging.info("hparams_set= %s",FLAGS.hparams_set)
    tf.logging.info("output_dir= %s",FLAGS.output_dir)
    tf.logging.info("train_steps= %s",FLAGS.train_steps)
    tf.logging.info("worker_gpu= %s",FLAGS.worker_gpu)
    tf.logging.info("local_eval_frequency= %s",FLAGS.local_eval_frequency)
    tf.logging.info("hparams= %s",FLAGS.hparams)
    tf.logging.info("=======================================================")
  else:
    tf.logging.info("=======================================================")
    tf.logging.info("decode parameters are as follows:")
    tf.logging.info("data_dir= %s", FLAGS.data_dir)
    tf.logging.info("problem= %s",FLAGS.problems)
    tf.logging.info("model= %s",FLAGS.model)
    tf.logging.info("hparams_set= %s",FLAGS.hparams_set)
    tf.logging.info("output_dir= %s",FLAGS.output_dir)
    tf.logging.info("decode_beam_size= %s",FLAGS.decode_beam_size)
    tf.logging.info("decode_alpha= %s",FLAGS.decode_alpha)
    tf.logging.info("decode_from_file= %s",FLAGS.decode_from_file)
    tf.logging.info("=======================================================")
if __name__ == "__main__":
  tf.app.run()
