Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly Type CorpusSample in ES #369

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions compiler_opt/es/blackbox_learner.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ class BlackboxLearnerConfig:
step_size: float


@dataclasses.dataclass(frozen=True)
class CorpusSample:
"""A sample of a corpus."""
modules: List[corpus.ModuleSpec]


def _prune_skipped_perturbations(perturbations: List[npt.NDArray[np.float32]],
rewards: List[Optional[float]]):
"""Remove perturbations that were skipped during the training step.
Expand Down Expand Up @@ -250,8 +256,9 @@ def _get_results(
perturbations: List[bytes]) -> List[concurrent.futures.Future]:
if not self._samples:
for _ in range(self._config.total_num_perturbations):
sample = self._train_corpus.sample(
self._config.num_ir_repeats_within_worker)
sample = CorpusSample(
self._train_corpus.sample(
self._config.num_ir_repeats_within_worker))
self._samples.append(sample)
# add copy of sample for antithetic perturbation pair
if self._config.est_type == (
Expand Down
6 changes: 3 additions & 3 deletions compiler_opt/es/blackbox_learner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import concurrent.futures
import gin
import tempfile
from typing import List
import numpy as np
import numpy.typing as npt
import tensorflow as tf
Expand All @@ -45,8 +44,9 @@ def __init__(self, arg, *, kwarg):
self._kwarg = kwarg
self.function_value = 0.0

def compile(self, policy: bytes, samples: List[corpus.ModuleSpec]) -> float:
if policy and samples:
def compile(self, policy: bytes,
samples: blackbox_learner.CorpusSample) -> float:
if policy and samples.modules:
self.function_value += 1.0
return self.function_value
else:
Expand Down