# Path: compiler_opt/rl/compilation_runner.py
# class RewardStat:
# class DataClassJSONEncoder(json.JSONEncoder):
# class ProcessKilledError(Exception):
# class ProcessCancellationToken:
# class WorkerCancellationManager:
# class CompilationRunner:
#   def default(self, o):
# def _postprocessing_sequence_example(
#     sequence_example: tf.train.SequenceExample, moving_average_reward: float,
#     policy_reward: float) -> tf.train.SequenceExample:
# def get_command_line_for_bundle(cmd_file: str,
#                                 ir_file: str,
#                                 thinlto: Optional[str] = None) -> List[str]:
#   def __init__(self):
#   def __init__(self):
#   def signal(self):
#   def wait(self):
# def kill_process_ignore_exceptions(p: subprocess.Popen):
#   def __init__(self):
#   def register_process(self, p: subprocess.Popen):
#   def signal(self):
#   def unregister_process(self, p: subprocess.Popen):
# def start_cancellable_process(
#     cmdline: List[str],
#     timeout: float,
#     cancellation_manager: Optional[WorkerCancellationManager],
#     want_output: bool = False) -> Optional[bytes]:
#   def init_pool():
#   def _get_pool():
#   def __init__(self,
#                clang_path: str,
#                llvm_size_path: str,
#                launcher_path: Optional[str] = None,
#                moving_average_decay_rate: float = 1):
#   def _get_cancellation_manager(
#       self, cancellation_token: Optional[ProcessCancellationToken]
#   ) -> Optional[WorkerCancellationManager]:
#     def signaler():
#   def collect_data(
#       self,
#       file_paths: Tuple[str, ...],
#       tf_policy_path: str,
#       reward_stat: Optional[Dict[str, RewardStat]],
#       cancellation_token: Optional[ProcessCancellationToken] = None
#   ) -> Tuple[List[str], Dict[str, RewardStat], List[float]]:
#   def _compile_fn(
#       self, file_paths: Tuple[str, ...], tf_policy_path: str, reward_only: bool,
#       cancellation_manager: Optional[WorkerCancellationManager]
#   ) -> Dict[str, Tuple[tf.train.SequenceExample, float]]:
#   _POOL: concurrent.futures.ThreadPoolExecutor = None
# 
# Path: compiler_opt/rl/constant.py
# BASE_DIR = 'compiler_opt/rl'
# BASE_MODULE_DIR = 'compiler_opt.rl'
# DELTA = 0.01
#   BEHAVIORAL_CLONE = 0
#   DQN = 1
#   PPO = 2
# class AgentName(enum.Enum):


import os
import string
import subprocess
import time
import tensorflow as tf
from unittest import mock
from absl import flags
from google.protobuf import text_format
from compiler_opt.rl import compilation_runner
from compiler_opt.rl import constant
# 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.

"""Tests for compiler_opt.rl.compilation_runner."""




_DEFAULT_FEATURE_VALUE = 12
_POLICY_FEATURE_VALUE = 34

_DEFAULT_REWARD = 10
_POLICY_REWARD = 8

_MOVING_AVERAGE_DECAY_RATE = 0.8


def _get_sequence_example_with_reward(feature_value, reward):
  sequence_example_text = string.Template("""
    feature_lists {
      feature_list {
        key: "feature_0"
        value {
          feature { int64_list { value: $feature_value } }
          feature { int64_list { value: $feature_value } }
        }
      }
      feature_list {
        key: "reward"

# Path: compiler_opt/rl/compilation_runner.py
# class RewardStat:
# class DataClassJSONEncoder(json.JSONEncoder):
# class ProcessKilledError(Exception):
# class ProcessCancellationToken:
# class WorkerCancellationManager:
# class CompilationRunner:
#   def default(self, o):
# def _postprocessing_sequence_example(
#     sequence_example: tf.train.SequenceExample, moving_average_reward: float,
#     policy_reward: float) -> tf.train.SequenceExample:
# def get_command_line_for_bundle(cmd_file: str,
#                                 ir_file: str,
#                                 thinlto: Optional[str] = None) -> List[str]:
#   def __init__(self):
#   def __init__(self):
#   def signal(self):
#   def wait(self):
# def kill_process_ignore_exceptions(p: subprocess.Popen):
#   def __init__(self):
#   def register_process(self, p: subprocess.Popen):
#   def signal(self):
#   def unregister_process(self, p: subprocess.Popen):
# def start_cancellable_process(
#     cmdline: List[str],
#     timeout: float,
#     cancellation_manager: Optional[WorkerCancellationManager],
#     want_output: bool = False) -> Optional[bytes]:
#   def init_pool():
#   def _get_pool():
#   def __init__(self,
#                clang_path: str,
#                llvm_size_path: str,
#                launcher_path: Optional[str] = None,
#                moving_average_decay_rate: float = 1):
#   def _get_cancellation_manager(
#       self, cancellation_token: Optional[ProcessCancellationToken]
#   ) -> Optional[WorkerCancellationManager]:
#     def signaler():
#   def collect_data(
#       self,
#       file_paths: Tuple[str, ...],
#       tf_policy_path: str,
#       reward_stat: Optional[Dict[str, RewardStat]],
#       cancellation_token: Optional[ProcessCancellationToken] = None
#   ) -> Tuple[List[str], Dict[str, RewardStat], List[float]]:
#   def _compile_fn(
#       self, file_paths: Tuple[str, ...], tf_policy_path: str, reward_only: bool,
#       cancellation_manager: Optional[WorkerCancellationManager]
#   ) -> Dict[str, Tuple[tf.train.SequenceExample, float]]:
#   _POOL: concurrent.futures.ThreadPoolExecutor = None
# 
# Path: compiler_opt/rl/constant.py
# BASE_DIR = 'compiler_opt/rl'
# BASE_MODULE_DIR = 'compiler_opt.rl'
# DELTA = 0.01
#   BEHAVIORAL_CLONE = 0
#   DQN = 1
#   PPO = 2
# class AgentName(enum.Enum):


# Path: compiler_opt/rl/compilation_runner_test.py
import os
import string
import subprocess
import time
import tensorflow as tf
from unittest import mock
from absl import flags
from google.protobuf import text_format
from compiler_opt.rl import compilation_runner
from compiler_opt.rl import constant

# 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.

"""Tests for compiler_opt.rl.compilation_runner."""




_DEFAULT_FEATURE_VALUE = 12
_POLICY_FEATURE_VALUE = 34

_DEFAULT_REWARD = 10
_POLICY_REWARD = 8

_MOVING_AVERAGE_DECAY_RATE = 0.8


def _get_sequence_example_with_reward(feature_value, reward):
  sequence_example_text = string.Template("""
    feature_lists {
      feature_list {
        key: "feature_0"
        value {
          feature { int64_list { value: $feature_value } }
          feature { int64_list { value: $feature_value } }
        }
      }
      feature_list {
        key: "reward"

        value {