From b7e3fcb66625ed64c7dcd2c2ac9f2f79df23b949 Mon Sep 17 00:00:00 2001 From: Mircea Trofin Date: Thu, 12 Feb 2026 20:39:19 -0800 Subject: [PATCH 1/5] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20ch?= =?UTF-8?q?anges=20to=20main=20this=20commit=20is=20based=20on?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Created using spr 1.3.7 [skip ci] --- compiler_opt/es/blackbox_evaluator.py | 26 ++++++------ compiler_opt/es/blackbox_evaluator_test.py | 48 +++++++++++++++------- compiler_opt/es/blackbox_learner.py | 5 ++- 3 files changed, 49 insertions(+), 30 deletions(-) diff --git a/compiler_opt/es/blackbox_evaluator.py b/compiler_opt/es/blackbox_evaluator.py index f79365ee..8e0f7833 100644 --- a/compiler_opt/es/blackbox_evaluator.py +++ b/compiler_opt/es/blackbox_evaluator.py @@ -47,8 +47,10 @@ class BlackboxEvaluator(metaclass=abc.ABCMeta): """Blockbox evaluator abstraction.""" @abc.abstractmethod - def __init__(self, train_corpus: corpus.Corpus): - pass + def __init__(self, *, train_corpus: corpus.Corpus, + estimator_type: blackbox_optimizers.EstimatorType): + self._train_corpus = train_corpus + self._estimator_type = estimator_type @abc.abstractmethod def get_results( @@ -65,18 +67,17 @@ def set_baseline(self, pool: FixedWorkerPool) -> None: class SamplingBlackboxEvaluator(BlackboxEvaluator): """A blackbox evaluator that samples from a corpus to collect reward.""" - def __init__(self, train_corpus: corpus.Corpus, - estimator_type: blackbox_optimizers.EstimatorType, - total_num_perturbations: int, num_ir_repeats_within_worker: int): + def __init__(self, + *, + total_num_perturbations: int, + num_ir_repeats_within_worker: int = 1, + **kwargs): + super().__init__(**kwargs) self._samples: list[list[corpus.LoadedModuleSpec]] = [] - self._train_corpus = train_corpus self._total_num_perturbations = total_num_perturbations self._num_ir_repeats_within_worker = num_ir_repeats_within_worker - self._estimator_type = estimator_type self._baselines: list[float | None] | None = None - super().__init__(train_corpus) - def _load_samples(self) -> None: """Samples and loads modules if not already done. @@ -171,11 +172,8 @@ def get_rewards( class TraceBlackboxEvaluator(BlackboxEvaluator): """A blackbox evaluator that utilizes trace based cost modelling.""" - def __init__(self, train_corpus: corpus.Corpus, - estimator_type: blackbox_optimizers.EstimatorType, - bb_trace_path: str, function_index_path: str): - self._train_corpus = train_corpus - self._estimator_type = estimator_type + def __init__(self, *, bb_trace_path: str, function_index_path: str, **kwargs): + super().__init__(**kwargs) self._bb_trace_paths = [] if tf.io.gfile.isdir(bb_trace_path): self._bb_trace_paths.extend([ diff --git a/compiler_opt/es/blackbox_evaluator_test.py b/compiler_opt/es/blackbox_evaluator_test.py index 1844c681..1d68d2c9 100644 --- a/compiler_opt/es/blackbox_evaluator_test.py +++ b/compiler_opt/es/blackbox_evaluator_test.py @@ -33,7 +33,10 @@ def test_sampling_get_results(self): worker_kwargs={}) as pool: perturbations = [b'00', b'01', b'10'] evaluator = blackbox_evaluator.SamplingBlackboxEvaluator( - None, blackbox_optimizers.EstimatorType.FORWARD_FD, 5, None) + train_corpus=None, + estimator_type=blackbox_optimizers.EstimatorType.FORWARD_FD, + total_num_perturbations=5, + num_ir_repeats_within_worker=0) # pylint: disable=protected-access evaluator._samples = [[corpus.ModuleSpec(name='name1', size=1)], [corpus.ModuleSpec(name='name2', size=1)], @@ -55,7 +58,9 @@ def test_sampling_set_baseline(self): corpus.ModuleSpec(name='name1', size=10, command_line=('-cc1',)) ]) evaluator = blackbox_evaluator.SamplingBlackboxEvaluator( - test_corpus, blackbox_optimizers.EstimatorType.FORWARD_FD, 1, 1) + train_corpus=test_corpus, + estimator_type=blackbox_optimizers.EstimatorType.FORWARD_FD, + total_num_perturbations=1) evaluator.set_baseline(pool) # pylint: disable=protected-access @@ -63,7 +68,10 @@ def test_sampling_set_baseline(self): def test_sampling_get_rewards_without_baseline(self): evaluator = blackbox_evaluator.SamplingBlackboxEvaluator( - None, blackbox_optimizers.EstimatorType.FORWARD_FD, 5, None) + train_corpus=None, + estimator_type=blackbox_optimizers.EstimatorType.FORWARD_FD, + total_num_perturbations=5, + num_ir_repeats_within_worker=0) self.assertRaises(RuntimeError, evaluator.get_rewards, None) def test_sampling_get_rewards_with_baseline(self): @@ -78,7 +86,9 @@ def test_sampling_get_rewards_with_baseline(self): corpus.ModuleSpec(name='name1', size=1, command_line=('-cc1',)) ]) evaluator = blackbox_evaluator.SamplingBlackboxEvaluator( - test_corpus, blackbox_optimizers.EstimatorType.FORWARD_FD, 2, 1) + train_corpus=test_corpus, + estimator_type=blackbox_optimizers.EstimatorType.FORWARD_FD, + total_num_perturbations=2) evaluator.set_baseline(pool) @@ -108,8 +118,10 @@ def test_trace_get_results(self): corpus.ModuleSpec(name='name1', size=1, command_line=('-cc1',)) ]) evaluator = blackbox_evaluator.TraceBlackboxEvaluator( - test_corpus, blackbox_optimizers.EstimatorType.FORWARD_FD, - 'fake_bb_trace_path', 'fake_function_index_path') + train_corpus=test_corpus, + estimator_type=blackbox_optimizers.EstimatorType.FORWARD_FD, + bb_trace_path='fake_bb_trace_path', + function_index_path='fake_function_index_path') # pylint: disable=protected-access evaluator._baselines = [1] # pylint: enable=protected-access @@ -129,8 +141,10 @@ def test_trace_set_baseline(self): corpus.ModuleSpec(name='name1', size=1, command_line=('-cc1',)) ]) evaluator = blackbox_evaluator.TraceBlackboxEvaluator( - test_corpus, blackbox_optimizers.EstimatorType.FORWARD_FD, - 'fake_bb_trace_path', 'fake_function_index_path') + train_corpus=test_corpus, + estimator_type=blackbox_optimizers.EstimatorType.FORWARD_FD, + bb_trace_path='fake_bb_trace_path', + function_index_path='fake_function_index_path') evaluator.set_baseline(pool) # pylint: disable=protected-access self.assertLen(evaluator._baselines, 1) @@ -149,8 +163,10 @@ def test_trace_get_rewards(self): corpus.ModuleSpec(name='name1', size=1, command_line=('-cc1',)) ]) evaluator = blackbox_evaluator.TraceBlackboxEvaluator( - test_corpus, blackbox_optimizers.EstimatorType.FORWARD_FD, - 'fake_bb_trace_path', 'fake_function_index_path') + train_corpus=test_corpus, + estimator_type=blackbox_optimizers.EstimatorType.FORWARD_FD, + bb_trace_path='fake_bb_trace_path', + function_index_path='fake_function_index_path') # pylint: disable=protected-access evaluator._current_baselines = [2, 3] @@ -178,8 +194,10 @@ def test_trace_multiple_get_results(self): bb_trace_dir.create_file('bb_trace1.pb') bb_trace_dir.create_file('bb_trace2.pb') evaluator = blackbox_evaluator.TraceBlackboxEvaluator( - test_corpus, blackbox_optimizers.EstimatorType.FORWARD_FD, - bb_trace_dir.full_path, 'fake_function_index_path') + train_corpus=test_corpus, + estimator_type=blackbox_optimizers.EstimatorType.FORWARD_FD, + bb_trace_path=bb_trace_dir.full_path, + function_index_path='fake_function_index_path') # pylint: disable=protected-access evaluator._baselines = [1, 2] # pylint: enable=protected-access @@ -202,8 +220,10 @@ def test_trace_multiple_set_baseline(self): bb_trace_dir.create_file('bb_trace1.pb') bb_trace_dir.create_file('bb_trace2.pb') evaluator = blackbox_evaluator.TraceBlackboxEvaluator( - test_corpus, blackbox_optimizers.EstimatorType.FORWARD_FD, - bb_trace_dir.full_path, 'fake_function_index_path') + train_corpus=test_corpus, + estimator_type=blackbox_optimizers.EstimatorType.FORWARD_FD, + bb_trace_path=bb_trace_dir.full_path, + function_index_path='fake_function_index_path') evaluator.set_baseline(pool) # pylint: disable=protected-access self.assertLen(evaluator._baselines, 2) diff --git a/compiler_opt/es/blackbox_learner.py b/compiler_opt/es/blackbox_learner.py index ecc9b5f9..ea0750d2 100644 --- a/compiler_opt/es/blackbox_learner.py +++ b/compiler_opt/es/blackbox_learner.py @@ -160,8 +160,9 @@ def __init__(self, self._summary_writer = tf.summary.create_file_writer(output_dir) - self._evaluator = self._config.evaluator(self._train_corpus, - self._config.estimator_type) + self._evaluator = self._config.evaluator( + train_corpus=self._train_corpus, + estimator_type=self._config.estimator_type) self._thread_pool = multiprocessing.pool.ThreadPool(processes=1) self._models_to_save = [] From d95a5970da759ec459f0c32ac3d77158d9ce2f7b Mon Sep 17 00:00:00 2001 From: Mircea Trofin Date: Thu, 12 Feb 2026 21:48:09 -0800 Subject: [PATCH 2/5] fix test Created using spr 1.3.7 --- compiler_opt/baseline_cache_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler_opt/baseline_cache_test.py b/compiler_opt/baseline_cache_test.py index 88645de4..4885c8a9 100644 --- a/compiler_opt/baseline_cache_test.py +++ b/compiler_opt/baseline_cache_test.py @@ -46,7 +46,7 @@ def track_score(lst): self.assertEqual( cache.get_score(["c", "b"], get_scores_func=track_score), [3, 2]) self.assertDictEqual(cache.get_cache(), {"b": 2, "c": 3}) - self.assertListEqual(score_asked_for, ["c", "b"]) + self.assertListEqual(sorted(score_asked_for), sorted(["c", "b"])) score_asked_for.clear() self.assertEqual( @@ -103,7 +103,7 @@ def get_scores(items: list[str]): cache = baseline_cache.BaselineCache(get_key=lambda x: x) self.assertEmpty(cache.get_cache()) self.assertEqual(cache.get_score(["4", "2"], get_scores), [4, 2]) - self.assertListEqual(score_asked_for, ["4", "2"]) + self.assertListEqual(sorted(score_asked_for), sorted(["4", "2"])) self.assertDictEqual(cache.get_cache(), {"4": 4, "2": 2}) score_asked_for.clear() From cc5622df3b523eefe70247a2e78b02190a2d5d6a Mon Sep 17 00:00:00 2001 From: Mircea Trofin Date: Thu, 12 Feb 2026 22:18:56 -0800 Subject: [PATCH 3/5] test dups Created using spr 1.3.7 --- compiler_opt/baseline_cache_test.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/compiler_opt/baseline_cache_test.py b/compiler_opt/baseline_cache_test.py index 4885c8a9..1d52bfe6 100644 --- a/compiler_opt/baseline_cache_test.py +++ b/compiler_opt/baseline_cache_test.py @@ -84,6 +84,21 @@ def track_score(lst): [3, 2, 3, 2]) self.assertListEqual(sorted(score_asked_for), sorted(["c", "b"])) + def test_duplicates(self): + mock = {"a": 1, "b": 2, "c": 3} + score_asked_for = [] + + def track_score(lst): + score_asked_for.extend(lst) + return [mock[k] if k in mock else None for k in lst] + + cache = baseline_cache.BaselineCache(get_key=lambda x: x) + self.assertEmpty(cache.get_cache()) + self.assertEqual( + cache.get_score(["c", "b", "c", "b"], get_scores_func=track_score), + [3, 2, 3, 2]) + self.assertListEqual(sorted(score_asked_for), sorted(["c", "b"])) + def test_with_workers(self): with local_worker_manager.LocalWorkerPoolManager( worker_class=MockWorker, count=4) as lwm: From abf181394ba0767b4358ad3e1c82046b9d74ba65 Mon Sep 17 00:00:00 2001 From: Mircea Trofin Date: Thu, 12 Feb 2026 22:22:11 -0800 Subject: [PATCH 4/5] fix Created using spr 1.3.7 --- compiler_opt/baseline_cache_test.py | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/compiler_opt/baseline_cache_test.py b/compiler_opt/baseline_cache_test.py index 1d52bfe6..4885c8a9 100644 --- a/compiler_opt/baseline_cache_test.py +++ b/compiler_opt/baseline_cache_test.py @@ -84,21 +84,6 @@ def track_score(lst): [3, 2, 3, 2]) self.assertListEqual(sorted(score_asked_for), sorted(["c", "b"])) - def test_duplicates(self): - mock = {"a": 1, "b": 2, "c": 3} - score_asked_for = [] - - def track_score(lst): - score_asked_for.extend(lst) - return [mock[k] if k in mock else None for k in lst] - - cache = baseline_cache.BaselineCache(get_key=lambda x: x) - self.assertEmpty(cache.get_cache()) - self.assertEqual( - cache.get_score(["c", "b", "c", "b"], get_scores_func=track_score), - [3, 2, 3, 2]) - self.assertListEqual(sorted(score_asked_for), sorted(["c", "b"])) - def test_with_workers(self): with local_worker_manager.LocalWorkerPoolManager( worker_class=MockWorker, count=4) as lwm: From f5fbfe8ffdd80e6cc811cc79f75352641c435a6c Mon Sep 17 00:00:00 2001 From: Mircea Trofin Date: Tue, 17 Feb 2026 17:18:44 -0800 Subject: [PATCH 5/5] fixes Created using spr 1.3.7 --- compiler_opt/baseline_cache.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/compiler_opt/baseline_cache.py b/compiler_opt/baseline_cache.py index 1febb5f9..a4035b70 100644 --- a/compiler_opt/baseline_cache.py +++ b/compiler_opt/baseline_cache.py @@ -39,12 +39,13 @@ def get_score(self, items: list[T | None], Args: items: A list of items to get scores for. - get_scores_func: A callable that returns the scores for a batch of - items. - The callable is responsible for timely completion. It must not - raise, and it must return results in the order of the items - provided. A None value is expected for items that could not produce - a value. + get_scores_func: A callable that returns the scores for a batch of + items. + + get_scores_func: Responsible for timely completion. It must not + raise, and it must return results in the order of the items + provided. A None value is expected for items that could not + produce a value. """ todo = {i for i in items if self._get_key(i) not in self._cache} scores = get_scores_func(list(todo))