# coding=utf-8
# Copyright 2022 The Google Research 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.

# python3
"""EBM definition and training logic."""
import math
import os
import time

from absl import app
from absl import flags
import numpy as np
import tensorflow.compat.v2 as tf
import tensorflow_probability as tfp

# pylint: disable=g-import-not-at-top
USE_LOCAL_FUN_MC = True

if USE_LOCAL_FUN_MC:
  from fun_mc import using_tensorflow as fun_mc  # pylint: disable=reimported

from neutra.ebm import ebm_util
# pylint: enable=g-import-not-at-top


FLAGS = flags.FLAGS
tfd = tfp.distributions
tfb = tfp.bijectors

flags.DEFINE_string('logdir', None, 'Directory to place all the artifacts in.')

flags.DEFINE_integer('seed', 1, 'PRNG seed.')

flags.DEFINE_enum('dataset', 'celeba', ['celeba', 'mnist'],
                  'Dataset to train on.')

flags.DEFINE_integer('batch_size', 64, 'Training minibatch size.')
flags.DEFINE_integer('train_steps', int(1e5),
                     'Total number of training iterations')
flags.DEFINE_integer('save_steps', 1000, 'How often to save checkpoints.')

flags.DEFINE_boolean('use_mcmc', False,
                     'Whether to use MCMC when training EBM.')
flags.DEFINE_integer('mcmc_num_steps', 10, 'How many MCMC steps to take.')
flags.DEFINE_float('mcmc_step_size', 0.1, 'MCMC initial step size.')
flags.DEFINE_integer('mcmc_leapfrog_steps', 3, 'MCMC number of leapfrog steps.')