#!/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 trainer_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)

"""
train-debug:
--problems=wmt_zhen_tokens_32k
--model=transformer_relative_pos
--data_dir=../../data/zh2en/debug/train
--output_dir=../../output
--worker_gpu_memory_fraction=0.8
--hparams_set=transformer_base_debug_rp
--hparams=batch_size=1

--worker_gpu_memory_fraction=0.6
--problems=wmt_zhen_tokens_32k
--model=transformer_relative_pos
--hparams_set=transformer_base_debug_rp
--data_dir=../../data/zh2en/debug/train
--output_dir=../../output
--train_steps=0
--eval_steps=0
--decode_beam_size=3
--decode_alpha=1.3
--decode_from_file=../../data/zh2en/debug/valid/input.bpe
--decode_batch_size=1

decoding:
--worker_gpu_memory_fraction=0.6 
--problems=wmt_zhen_tokens_32k 
--model=transformer 
--hparams_set=transformer_base_debug 
--data_dir=../../data/zh2en/debug/train 
--output_dir=../../output 
--train_steps=0 
--eval_steps=0 
--decode_beam_size=3 
--decode_alpha=1.3 
--decode_from_file=../../data/zh2en/debug/valid/input.bpe 
--decode_batch_size=1


"""
def main(_):
  tf.logging.set_verbosity(tf.logging.INFO)
  import_usr_dir()
  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)


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