From 9ab0e5495d89354f93f35226e76a70278fa7e658 Mon Sep 17 00:00:00 2001 From: Jin Xin Ng Date: Fri, 19 Aug 2022 15:25:27 -0700 Subject: [PATCH 1/4] Add method to get modules in a Corpus --- compiler_opt/rl/corpus.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/compiler_opt/rl/corpus.py b/compiler_opt/rl/corpus.py index e29abf31..81fdffe8 100644 --- a/compiler_opt/rl/corpus.py +++ b/compiler_opt/rl/corpus.py @@ -122,6 +122,10 @@ def filter(self, p: re.Pattern): """Filters module specs, keeping those which match the provided pattern.""" self._module_specs = [ms for ms in self._module_specs if p.match(ms.name)] + @property + def modules(self): + return list(self._module_specs) + def __len__(self): return len(self._module_specs) From 99a47d4a51693961f253d3e861ed08b00987f9d4 Mon Sep 17 00:00:00 2001 From: Jin Xin Ng Date: Fri, 19 Aug 2022 16:06:17 -0700 Subject: [PATCH 2/4] change to get_modules_copy --- compiler_opt/rl/corpus.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/compiler_opt/rl/corpus.py b/compiler_opt/rl/corpus.py index 81fdffe8..3b821ba7 100644 --- a/compiler_opt/rl/corpus.py +++ b/compiler_opt/rl/corpus.py @@ -122,8 +122,7 @@ def filter(self, p: re.Pattern): """Filters module specs, keeping those which match the provided pattern.""" self._module_specs = [ms for ms in self._module_specs if p.match(ms.name)] - @property - def modules(self): + def get_modules_copy(self): return list(self._module_specs) def __len__(self): From 41000a4d5cc9ee4bb31d5bc84a4bd4d4b8f8e445 Mon Sep 17 00:00:00 2001 From: Jin Xin Date: Mon, 5 Sep 2022 23:06:06 -0400 Subject: [PATCH 3/4] Switch to tuples --- compiler_opt/rl/corpus.py | 35 +++++++++++++++++----------------- compiler_opt/rl/corpus_test.py | 6 ++++-- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/compiler_opt/rl/corpus.py b/compiler_opt/rl/corpus.py index 3b821ba7..ce65d804 100644 --- a/compiler_opt/rl/corpus.py +++ b/compiler_opt/rl/corpus.py @@ -19,7 +19,7 @@ from absl import logging from dataclasses import dataclass -from typing import List, Dict, Tuple, Any +from typing import Iterable, List, Dict, Tuple, Any import json import os @@ -45,7 +45,7 @@ def __init__(self): self._ranges = {} def __call__(self, - module_specs: List[ModuleSpec], + module_specs: Tuple[ModuleSpec], k: int, n: int = 20) -> List[ModuleSpec]: """ @@ -86,20 +86,23 @@ def __init__(self, data_path: str, additional_flags: Tuple[str, ...] = (), delete_flags: Tuple[str, ...] = ()): - self._module_specs = _build_modulespecs_from_datapath( - data_path=data_path, - additional_flags=additional_flags, - delete_flags=delete_flags) + self.module_specs = tuple( + sorted( + _build_modulespecs_from_datapath( + data_path=data_path, + additional_flags=additional_flags, + delete_flags=delete_flags), + key=lambda m: m.size, + reverse=True)) self._root_dir = data_path - self._module_specs.sort(key=lambda m: m.size, reverse=True) @classmethod - def from_module_specs(cls, module_specs: List[ModuleSpec]): + def from_module_specs(cls, module_specs: Iterable[ModuleSpec]): """Construct a Corpus from module specs. Mostly for testing purposes.""" cps = cls.__new__(cls) # Avoid calling __init__ super(cls, cps).__init__() - cps._module_specs = list(module_specs) # Don't mutate the original list. - cps._module_specs.sort(key=lambda m: m.size, reverse=True) + cps.module_specs = tuple( + sorted(module_specs, key=lambda m: m.size, reverse=True)) cps.root_dir = None return cps @@ -110,23 +113,21 @@ def sample(self, """Samples `k` module_specs, optionally sorting by size descending.""" # Note: sampler is intentionally defaulted to a mutable object, as the # only mutable attribute of SamplerBucketRoundRobin is its range cache. - k = min(len(self._module_specs), k) + k = min(len(self.module_specs), k) if k < 1: raise ValueError('Attempting to sample <1 module specs from corpus.') - sampled_specs = sampler(self._module_specs, k=k) + sampled_specs = sampler(self.module_specs, k=k) if sort: sampled_specs.sort(key=lambda m: m.size, reverse=True) return sampled_specs def filter(self, p: re.Pattern): """Filters module specs, keeping those which match the provided pattern.""" - self._module_specs = [ms for ms in self._module_specs if p.match(ms.name)] - - def get_modules_copy(self): - return list(self._module_specs) + self.module_specs = tuple( + ms for ms in self.module_specs if p.match(ms.name)) def __len__(self): - return len(self._module_specs) + return len(self.module_specs) def _build_modulespecs_from_datapath( diff --git a/compiler_opt/rl/corpus_test.py b/compiler_opt/rl/corpus_test.py index c8202206..5e6d153e 100644 --- a/compiler_opt/rl/corpus_test.py +++ b/compiler_opt/rl/corpus_test.py @@ -246,8 +246,10 @@ def test_constructor(self): cps = corpus.Corpus(tempdir.full_path, additional_flags=('-add',)) self.assertEqual( - corpus._build_modulespecs_from_datapath( - tempdir.full_path, additional_flags=('-add',)), cps._module_specs) + tuple( + corpus._build_modulespecs_from_datapath( + tempdir.full_path, additional_flags=('-add',))), + cps.module_specs) self.assertEqual(len(cps), 1) def test_sample(self): From cec329db3ccf4fec703e633b43caffe1dcd0368b Mon Sep 17 00:00:00 2001 From: Jin Xin Date: Tue, 6 Sep 2022 16:06:55 -0400 Subject: [PATCH 4/4] make module_specs private --- compiler_opt/rl/corpus.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/compiler_opt/rl/corpus.py b/compiler_opt/rl/corpus.py index ce65d804..f30c1b6b 100644 --- a/compiler_opt/rl/corpus.py +++ b/compiler_opt/rl/corpus.py @@ -86,7 +86,7 @@ def __init__(self, data_path: str, additional_flags: Tuple[str, ...] = (), delete_flags: Tuple[str, ...] = ()): - self.module_specs = tuple( + self._module_specs = tuple( sorted( _build_modulespecs_from_datapath( data_path=data_path, @@ -101,7 +101,7 @@ def from_module_specs(cls, module_specs: Iterable[ModuleSpec]): """Construct a Corpus from module specs. Mostly for testing purposes.""" cps = cls.__new__(cls) # Avoid calling __init__ super(cls, cps).__init__() - cps.module_specs = tuple( + cps._module_specs = tuple( sorted(module_specs, key=lambda m: m.size, reverse=True)) cps.root_dir = None return cps @@ -113,21 +113,25 @@ def sample(self, """Samples `k` module_specs, optionally sorting by size descending.""" # Note: sampler is intentionally defaulted to a mutable object, as the # only mutable attribute of SamplerBucketRoundRobin is its range cache. - k = min(len(self.module_specs), k) + k = min(len(self._module_specs), k) if k < 1: raise ValueError('Attempting to sample <1 module specs from corpus.') - sampled_specs = sampler(self.module_specs, k=k) + sampled_specs = sampler(self._module_specs, k=k) if sort: sampled_specs.sort(key=lambda m: m.size, reverse=True) return sampled_specs def filter(self, p: re.Pattern): """Filters module specs, keeping those which match the provided pattern.""" - self.module_specs = tuple( - ms for ms in self.module_specs if p.match(ms.name)) + self._module_specs = tuple( + ms for ms in self._module_specs if p.match(ms.name)) + + @property + def module_specs(self): + return self._module_specs def __len__(self): - return len(self.module_specs) + return len(self._module_specs) def _build_modulespecs_from_datapath(