Skip to content

Commit f179ce1

Browse files
authored
[BC] keep_temps update (#411)
* Rename ```base_path``` to ```persistent_objects_path``` and ```keep_temps``` to ```explicit_temps_dir```. Makes sure that if ```persistent_objects_path``` is set then ```explicit_temps_dir``` is also set or neither of them is set. Further, passes ```explicit_temps_dir``` to ```compilation_runner.get_workdir_context``` and ensures that the flag and argument are not set at the same time. * Addressing @mtrofin and @boomanaiden154 comments. * Set explicit_temps_dir to persistent_objects_path+'/temp_dirs' whenever explicit_temps_dir is not set and persistent_objects_path is set.
1 parent 7e94d9e commit f179ce1

File tree

5 files changed

+77
-26
lines changed

5 files changed

+77
-26
lines changed

compiler_opt/rl/compilation_runner.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
'Max duration (in seconds) after which we cancel any compilation job.')
4040
_QUIET = flags.DEFINE_bool(
4141
'quiet', True, 'Whether or not to compile quietly (hiding info logging)')
42-
_KEEP_TEMPS = flags.DEFINE_string(
43-
'keep_temps', None,
42+
_EXPLICIT_TEMPS_DIR = flags.DEFINE_string(
43+
'explicit_temps_dir', None,
4444
'Put temporary files into given directory and keep them past exit.')
4545

4646

@@ -80,14 +80,25 @@ def __exit__(self, exc, value, tb):
8080
pass
8181

8282

83-
def get_workdir_context():
83+
def get_workdir_context(explicit_temps_dir: Optional[str] = None):
8484
"""Return a context which manages how the temperory directories are handled.
8585
86-
When the flag keep_temps is specified temporary directories are stored in
87-
keep_temps.
86+
When the flag explicit_temps_dir is specified temporary directories are
87+
stored in explicit_temps_dir.
88+
89+
Args:
90+
explicit_temps_dir: Put temporary files into given directory and keep them
91+
past exit when compilining
8892
"""
89-
if _KEEP_TEMPS.value is not None:
90-
tempdir_context = NonTemporaryDirectory(dir=_KEEP_TEMPS.value)
93+
if explicit_temps_dir and _EXPLICIT_TEMPS_DIR.value:
94+
raise ValueError('Only one of flag'
95+
'explicit_temps_dir={_EXPLICIT_TEMPS_DIR.value}'
96+
'and arg explicit_temps_dir={explicit_temps_dir}'
97+
'should be specified.')
98+
if _EXPLICIT_TEMPS_DIR.value is not None:
99+
tempdir_context = NonTemporaryDirectory(dir=_EXPLICIT_TEMPS_DIR.value)
100+
elif explicit_temps_dir:
101+
tempdir_context = NonTemporaryDirectory(dir=explicit_temps_dir)
91102
else:
92103
tempdir_context = tempfile.TemporaryDirectory() # pylint: disable=consider-using-with
93104
return tempdir_context

compiler_opt/rl/env.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ def clang_session(
225225
module: corpus.LoadedModuleSpec,
226226
task_type: Type[MLGOTask],
227227
*,
228+
explicit_temps_dir: Optional[str] = None,
228229
interactive: bool,
229230
):
230231
"""Context manager for clang session.
@@ -236,12 +237,15 @@ def clang_session(
236237
clang_path: The clang binary to use for the InteractiveClang session.
237238
module: The module to compile with clang.
238239
task_type: Type of the MLGOTask to use.
240+
explicit_temps_dir: Put temporary files into given directory and keep them
241+
past exit when compilining
239242
interactive: Whether to use an interactive or default clang instance
240243
241244
Yields:
242245
Either the constructed InteractiveClang or DefaultClang object.
243246
"""
244-
tempdir_context = compilation_runner.get_workdir_context()
247+
tempdir_context = compilation_runner.get_workdir_context(
248+
explicit_temps_dir=explicit_temps_dir)
245249
with tempdir_context as td:
246250
task_working_dir = os.path.join(td, '__task_working_dir__')
247251
os.mkdir(task_working_dir)
@@ -290,6 +294,7 @@ def _get_scores() -> dict[str, float]:
290294
def _get_clang_generator(
291295
clang_path: str,
292296
task_type: Type[MLGOTask],
297+
explicit_temps_dir: Optional[str] = None,
293298
interactive_only: bool = False,
294299
) -> Generator[Optional[Tuple[ClangProcess, InteractiveClang]],
295300
Optional[corpus.LoadedModuleSpec], None]:
@@ -298,6 +303,8 @@ def _get_clang_generator(
298303
Args:
299304
clang_path: Path to the clang binary to use within InteractiveClang.
300305
task_type: Type of the MLGO task to use.
306+
explicit_temps_dir: Put temporary files into given directory and keep them
307+
past exit when compilining
301308
interactive_only: If set to true the returned tuple of generators is
302309
iclang, iclang instead of iclang, clang
303310
@@ -315,12 +322,20 @@ def _get_clang_generator(
315322
# https://github.com/google/yapf/issues/1092
316323
module = yield
317324
with clang_session(
318-
clang_path, module, task_type, interactive=True) as iclang:
325+
clang_path,
326+
module,
327+
task_type,
328+
explicit_temps_dir=explicit_temps_dir,
329+
interactive=True) as iclang:
319330
if interactive_only:
320331
yield iclang, iclang
321332
else:
322333
with clang_session(
323-
clang_path, module, task_type, interactive=False) as clang:
334+
clang_path,
335+
module,
336+
task_type,
337+
explicit_temps_dir=explicit_temps_dir,
338+
interactive=False) as clang:
324339
yield iclang, clang
325340

326341

@@ -340,10 +355,14 @@ def __init__(
340355
task_type: Type[MLGOTask],
341356
obs_spec,
342357
action_spec,
358+
explicit_temps_dir: Optional[str] = None,
343359
interactive_only: bool = False,
344360
):
345361
self._clang_generator = _get_clang_generator(
346-
clang_path, task_type, interactive_only=interactive_only)
362+
clang_path,
363+
task_type,
364+
explicit_temps_dir=explicit_temps_dir,
365+
interactive_only=interactive_only)
347366
self._obs_spec = obs_spec
348367
self._action_spec = action_spec
349368

compiler_opt/rl/env_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ def test_interactive_clang_temp_dir(self, mock_popen):
178178
self.assertEqual(os.path.exists(working_dir), False)
179179

180180
with tempfile.TemporaryDirectory() as td:
181-
with flagsaver.flagsaver((env.compilation_runner._KEEP_TEMPS, td)): # pylint: disable=protected-access
181+
with flagsaver.flagsaver(
182+
(env.compilation_runner._EXPLICIT_TEMPS_DIR, td)): # pylint: disable=protected-access
182183
with env.clang_session(
183184
_CLANG_PATH, _MOCK_MODULE, MockTask,
184185
interactive=True) as clang_session:

compiler_opt/rl/imitation_learning/generate_bc_trajectories_lib.py

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Generator, Union
2121
import json
2222

23-
# from absl import flags
23+
from absl import flags
2424
from absl import logging
2525
import bisect
2626
import dataclasses
@@ -46,6 +46,14 @@
4646
from compiler_opt.distributed import buffered_scheduler
4747
from compiler_opt.distributed.local import local_worker_manager
4848

49+
_PERSISTENT_OBJECTS_PATH = flags.DEFINE_string(
50+
'persistent_objects_path', None,
51+
('If specified, the temp compiled binaries throughout'
52+
'the trajectory generation will be saved in persistent_objects_path'
53+
'for linking the final binary.'))
54+
55+
FLAGS = flags.FLAGS
56+
4957
ProfilingDictValueType = Dict[str, Union[str, float, int]]
5058

5159

@@ -318,6 +326,7 @@ def __init__(
318326
tensor_spec.BoundedTensorSpec,
319327
]] = None,
320328
reward_key: str = '',
329+
explicit_temps_dir: Optional[str] = None,
321330
**kwargs,
322331
):
323332
self._loaded_module_spec = loaded_module_spec
@@ -343,6 +352,7 @@ def __init__(
343352
task_type=mlgo_task_type,
344353
obs_spec=obs_spec,
345354
action_spec=action_spec,
355+
explicit_temps_dir=explicit_temps_dir,
346356
interactive_only=True,
347357
)
348358
if self._env.action_spec:
@@ -603,8 +613,8 @@ def _process_obs(self, curr_obs, sequence_example):
603613
class ModuleWorkerResultProcessor:
604614
"""Utility class to process ModuleExplorer results for ModuleWorker."""
605615

606-
def __init__(self, base_path: Optional[str] = None):
607-
self._base_path = base_path
616+
def __init__(self, persistent_objects_path: Optional[str] = None):
617+
self._persistent_objects_path = persistent_objects_path
608618

609619
def _partition_for_loss(self, seq_example: tf.train.SequenceExample,
610620
partitions: List[float], label_name: str):
@@ -654,12 +664,13 @@ def process_succeeded(
654664
logging.info('best policy idx: %s, best exploration idxs %s',
655665
best_policy_idx, best_exploration_idxs)
656666

657-
if self._base_path:
667+
if self._persistent_objects_path:
658668
# as long as we have one process handles one module this can stay here
659669
temp_working_dir_idx = working_dir_list[best_policy_idx][1]
660670
temp_working_dir_list = working_dir_list[best_policy_idx][0]
661671
temp_working_dir = temp_working_dir_list[temp_working_dir_idx]
662-
self._save_binary(self._base_path, spec_name, temp_working_dir)
672+
self._save_binary(self._persistent_objects_path, spec_name,
673+
temp_working_dir)
663674

664675
self._partition_for_loss(seq_example, partitions, label_name)
665676

@@ -689,11 +700,12 @@ def _profiling_dict(
689700
}
690701
return per_module_dict
691702

692-
def _save_binary(self, base_path: str, save_path: str, binary_path: str):
703+
def _save_binary(self, persistent_objects_path: str, save_path: str,
704+
binary_path: str):
693705
path_head_tail = os.path.split(save_path)
694706
path_head = path_head_tail[0]
695707
path_tail = path_head_tail[1]
696-
save_dir = os.path.join(base_path, path_head)
708+
save_dir = os.path.join(persistent_objects_path, path_head)
697709
if not os.path.exists(save_dir):
698710
os.makedirs(save_dir, exist_ok=True)
699711
shutil.copy(
@@ -725,7 +737,8 @@ class ModuleWorker(worker.Worker):
725737
explore_on_features: dict of feature names and functions which specify
726738
when to explore on the respective feature
727739
obs_action_specs: optional observation spec annotating TimeStep
728-
base_path: root path to save best compiled binaries for linking
740+
persistent_objects_path: root path to save best compiled binaries
741+
for linking
729742
partitions: a tuple of limits defining the buckets, see partition_for_loss
730743
env_args: additional arguments to pass to the ModuleExplorer, used in
731744
creating the environment. This has to include the reward_key
@@ -748,7 +761,7 @@ def __init__(
748761
time_step.TimeStep,
749762
tensor_spec.BoundedTensorSpec,
750763
]] = None,
751-
base_path: Optional[str] = None,
764+
persistent_objects_path: Optional[str] = None,
752765
partitions: List[float] = [
753766
0.,
754767
],
@@ -775,8 +788,8 @@ def __init__(
775788
[tf.Tensor], bool]]] = explore_on_features
776789
self._obs_action_specs: Optional[Tuple[
777790
time_step.TimeStep, tensor_spec.BoundedTensorSpec]] = obs_action_specs
778-
self._mw_utility = ModuleWorkerResultProcessor(base_path)
779-
self._base_path = base_path
791+
self._mw_utility = ModuleWorkerResultProcessor(persistent_objects_path)
792+
self._persistent_objects_path = persistent_objects_path
780793
self._partitions = partitions
781794
self._envargs = envargs
782795

@@ -858,7 +871,7 @@ def select_best_exploration(
858871
try:
859872
shutil.rmtree(temp_dir_head)
860873
except FileNotFoundError as e:
861-
if not self._base_path:
874+
if not self._persistent_objects_path:
862875
continue
863876
else:
864877
raise FileNotFoundError(
@@ -918,6 +931,13 @@ def gen_trajectories(
918931
worker_manager_class: A pool of workers hosted on the local machines, each
919932
in its own process.
920933
"""
934+
explicit_temps_dir = FLAGS.explicit_temps_dir
935+
persistent_objects_path = _PERSISTENT_OBJECTS_PATH.value
936+
if not explicit_temps_dir and persistent_objects_path:
937+
logging.warning('Setting explicit_temps_dir to persistent_objects_path=%s',
938+
persistent_objects_path)
939+
explicit_temps_dir = os.path.join(persistent_objects_path, 'temp_dirs')
940+
921941
cps = corpus.Corpus(data_path=data_path, delete_flags=delete_flags)
922942
logging.info('Done loading module specs from corpus.')
923943

@@ -944,6 +964,8 @@ def gen_trajectories(
944964
mlgo_task_type=mlgo_task_type,
945965
callable_policies=callable_policies,
946966
explore_on_features=explore_on_features,
967+
persistent_objects_path=persistent_objects_path,
968+
explicit_temps_dir=explicit_temps_dir,
947969
gin_config_str=gin.config_str(),
948970
) as lwm:
949971

compiler_opt/rl/inlining/gin_configs/imitation_learning.gin

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ generate_bc_trajectories_lib.ModuleWorker.mlgo_task_type=@env.InliningForSizeTas
1010
generate_bc_trajectories_lib.ModuleWorker.policy_paths=['']
1111
generate_bc_trajectories_lib.ModuleWorker.exploration_policy_paths=[]
1212
generate_bc_trajectories_lib.ModuleWorker.explore_on_features=None
13-
generate_bc_trajectories_lib.ModuleWorker.base_path=''
1413
generate_bc_trajectories_lib.ModuleWorker.partitions=[
1514
285.0, 376.0, 452.0, 512.0, 571.0, 627.5, 720.0, 809.5, 1304.0, 1832.0,
1615
2467.0, 3344.0, 4545.0, 6459.0, 9845.0, 17953.0, 29430.5, 85533.5,
1716
124361.0]
1817
generate_bc_trajectories_lib.ModuleWorker.reward_key='default'
19-
# generate_bc_trajectories_lib.ModuleWorker.gin_config_str=None
2018

2119
generate_bc_trajectories_lib.gen_trajectories.data_path=''
2220
generate_bc_trajectories_lib.gen_trajectories.delete_flags=('-split-dwarf-file', '-split-dwarf-output')

0 commit comments

Comments
 (0)