# Path: compiler_opt/rl/regalloc_network.py
# class RegAllocEncodingNetwork(encoding_network.EncodingNetwork):
# class RegAllocProbProjectionNetwork(
#     categorical_projection_network.CategoricalProjectionNetwork):
# class RegAllocRNDEncodingNetwork(RegAllocEncodingNetwork):
# class RegAllocNetwork(network.DistributionNetwork):
#   def __init__(self, **kwargs):
#   def __init__(self, **kwargs):
#   def __init__(self, **kwargs):
#   def __init__(
#       self,
#       input_tensor_spec: types.NestedTensorSpec,
#       output_tensor_spec: types.NestedTensorSpec,
#       preprocessing_layers: Optional[types.NestedLayer] = None,
#       preprocessing_combiner: Optional[tf.keras.layers.Layer] = None,
#       conv_layer_params: Optional[Sequence[Any]] = None,
#       fc_layer_params: Optional[Sequence[int]] = (200, 100),
#       dropout_layer_params: Optional[Sequence[float]] = None,
#       activation_fn: Callable[[types.Tensor],
#                               types.Tensor] = tf.keras.activations.relu,
#       kernel_initializer: Optional[tf.keras.initializers.Initializer] = None,
#       batch_squash: bool = True,
#       dtype: tf.DType = tf.float32,
#       name: Text = 'RegAllocNetwork'):
#   def output_tensor_spec(self):
#   def call(self,
#            observations: types.NestedTensor,
#            step_type: types.NestedTensor,
#            network_state=(),
#            training: bool = False,
#            mask=None):
# 
# Path: compiler_opt/rl/regalloc/config.py
# def get_num_registers():
# def get_regalloc_signature_spec():
# def get_observation_processing_layer_creator(quantile_file_dir=None,
#                                              with_sqrt=True,
#                                              with_z_score_normalization=True,
#                                              eps=1e-8):
#   def observation_processing_layer(obs_spec):
#       def use_def_density_processing_fn(obs):
#       def progress_processing_fn(obs):


import tensorflow as tf
import tensorflow_probability as tfp
from tf_agents.specs import tensor_spec
from compiler_opt.rl import regalloc_network
from compiler_opt.rl.regalloc import config


class RegAllocNetworkTest(tf.test.TestCase):

  def setUp(self):
    time_step_spec, action_spec = config.get_regalloc_signature_spec()
    random_observation = tensor_spec.sample_spec_nest(
        time_step_spec, outer_dims=(2, 3))
    super(RegAllocNetworkTest, self).setUp()
    self._time_step_spec = time_step_spec
    self._action_spec = action_spec
    self._random_observation = random_observation

  def testBuilds(self):
    layers = tf.nest.map_structure(_observation_processing_layer,
                                   self._time_step_spec.observation)

    net = regalloc_network.RegAllocNetwork(
        self._time_step_spec.observation,
        self._action_spec,
        preprocessing_layers=layers,
        preprocessing_combiner=tf.keras.layers.Concatenate(),
        fc_layer_params=(10,))

    action_distributions, _ = net(
        self._random_observation.observation,
        step_type=self._random_observation.step_type)
    self.assertIsInstance(action_distributions, tfp.distributions.Categorical)
    self.assertEqual([2, 3], action_distributions.mode().shape.as_list())
    self.assertAllInRange(action_distributions.mode(), 0,

# Path: compiler_opt/rl/regalloc_network.py
# class RegAllocEncodingNetwork(encoding_network.EncodingNetwork):
# class RegAllocProbProjectionNetwork(
#     categorical_projection_network.CategoricalProjectionNetwork):
# class RegAllocRNDEncodingNetwork(RegAllocEncodingNetwork):
# class RegAllocNetwork(network.DistributionNetwork):
#   def __init__(self, **kwargs):
#   def __init__(self, **kwargs):
#   def __init__(self, **kwargs):
#   def __init__(
#       self,
#       input_tensor_spec: types.NestedTensorSpec,
#       output_tensor_spec: types.NestedTensorSpec,
#       preprocessing_layers: Optional[types.NestedLayer] = None,
#       preprocessing_combiner: Optional[tf.keras.layers.Layer] = None,
#       conv_layer_params: Optional[Sequence[Any]] = None,
#       fc_layer_params: Optional[Sequence[int]] = (200, 100),
#       dropout_layer_params: Optional[Sequence[float]] = None,
#       activation_fn: Callable[[types.Tensor],
#                               types.Tensor] = tf.keras.activations.relu,
#       kernel_initializer: Optional[tf.keras.initializers.Initializer] = None,
#       batch_squash: bool = True,
#       dtype: tf.DType = tf.float32,
#       name: Text = 'RegAllocNetwork'):
#   def output_tensor_spec(self):
#   def call(self,
#            observations: types.NestedTensor,
#            step_type: types.NestedTensor,
#            network_state=(),
#            training: bool = False,
#            mask=None):
# 
# Path: compiler_opt/rl/regalloc/config.py
# def get_num_registers():
# def get_regalloc_signature_spec():
# def get_observation_processing_layer_creator(quantile_file_dir=None,
#                                              with_sqrt=True,
#                                              with_z_score_normalization=True,
#                                              eps=1e-8):
#   def observation_processing_layer(obs_spec):
#       def use_def_density_processing_fn(obs):
#       def progress_processing_fn(obs):


# Path: compiler_opt/rl/regalloc_network_test.py
import tensorflow as tf
import tensorflow_probability as tfp
from tf_agents.specs import tensor_spec
from compiler_opt.rl import regalloc_network
from compiler_opt.rl.regalloc import config



class RegAllocNetworkTest(tf.test.TestCase):

  def setUp(self):
    time_step_spec, action_spec = config.get_regalloc_signature_spec()
    random_observation = tensor_spec.sample_spec_nest(
        time_step_spec, outer_dims=(2, 3))
    super(RegAllocNetworkTest, self).setUp()
    self._time_step_spec = time_step_spec
    self._action_spec = action_spec
    self._random_observation = random_observation

  def testBuilds(self):
    layers = tf.nest.map_structure(_observation_processing_layer,
                                   self._time_step_spec.observation)

    net = regalloc_network.RegAllocNetwork(
        self._time_step_spec.observation,
        self._action_spec,
        preprocessing_layers=layers,
        preprocessing_combiner=tf.keras.layers.Concatenate(),
        fc_layer_params=(10,))

    action_distributions, _ = net(
        self._random_observation.observation,
        step_type=self._random_observation.step_type)
    self.assertIsInstance(action_distributions, tfp.distributions.Categorical)
    self.assertEqual([2, 3], action_distributions.mode().shape.as_list())
    self.assertAllInRange(action_distributions.mode(), 0,

                          config.get_num_registers() - 1)