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

Fix computation with splited blocks #1434

Merged
merged 5 commits into from
Apr 29, 2022
Merged
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
9 changes: 8 additions & 1 deletion nevergrad/benchmark/experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,13 @@ def yawidebbob(seed: tp.Optional[int] = None) -> tp.Iterator[Experiment]:

functions += [
ArtificialFunction(
name, block_dimension=d, rotation=rotation, noise_level=nl, split=split, translation_factor=tf
name,
block_dimension=d,
rotation=rotation,
noise_level=nl,
split=split,
translation_factor=tf,
num_blocks=num_blocks,
)
for name in names # period 5
for rotation in [True, False] # period 2
Expand Down Expand Up @@ -686,6 +692,7 @@ def yabbob(
rotation=rotation,
noise_level=noise_level,
split=split,
num_blocks=num_blocks,
bounded=bounded or box,
)
for name in names
Expand Down
2 changes: 1 addition & 1 deletion nevergrad/functions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def copy(self: EF) -> EF:
return output

def compute_pseudotime( # pylint: disable=unused-argument
self, input_parameter: tp.Any, loss: tp.Loss
self, input_parameter: tp.ArgsKwargs, loss: tp.Loss
) -> float:
"""Computes a pseudotime used during benchmarks for mocking parallelization in a reproducible way.
By default, each call takes 1 unit of pseudotime, but this can be modified by overriding this
Expand Down
26 changes: 14 additions & 12 deletions nevergrad/functions/functionlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def _initialize(self) -> None:
self._dimension, self.block_dimension * self.num_blocks, replace=False
).tolist()
indices.sort() # keep the indices sorted sorted so that blocks do not overlap
# Caution this is also important for split, so that splitted arrays end un in the same block
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Caution this is also important for split, so that splitted arrays end un in the same block
# Caution this is also important for split, so that splitted arrays end up in the same block

for transform_inds in tools.grouper(indices, n=self.block_dimension):
self._transforms.append(
utils.Transform(
Expand Down Expand Up @@ -176,14 +177,14 @@ def __init__( # pylint: disable=too-many-arguments
info = corefuncs.registry.get_info(self._parameters["name"])
only_index_transform = info.get("no_transform", False)

assert not (split and hashing)
assert not (split and useless_variables > 0)
array_bounds = dict(upper=5, lower=-5) if bounded else {}
if not split:
parametrization: ng.p.Parameter = ng.p.Array(
shape=(1,) if hashing else (self._dimension,), **array_bounds # type: ignore
).set_name("")
else:
assert not hashing
assert not useless_variables
arrays = [
ng.p.Array(shape=(block_dimension,), **array_bounds) for _ in range(num_blocks) # type: ignore
]
Expand Down Expand Up @@ -218,9 +219,8 @@ def __init__( # pylint: disable=too-many-arguments

@property
def dimension(self) -> int:
return (
self._dimension
) # bypass the parametrization one (because of the "hashing" case) # TODO: remove
# bypass the parametrization one (because of the "hashing" case) # TODO: remove
return self._dimension

@staticmethod
def list_sorted_function_names() -> tp.List[str]:
Expand Down Expand Up @@ -248,27 +248,29 @@ def evaluation_function(self, *recommendations: ng.p.Parameter) -> float:
Under the hood, __call__ delegates to oracle_call + add some noise if noise_level > 0.
"""
assert len(recommendations) == 1, "Should not be a pareto set for a singleobjective function"
assert len(recommendations[0].args) == 1 and not recommendations[0].kwargs
data = self._transform(recommendations[0].args[0])
assert not recommendations[0].kwargs
# we can concatenate since blocks are necessarily sorted
data = np.concatenate(recommendations[0].args, axis=0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aaaaaaah c'etait ca. Ok My bad I was testing "flatten" and desperately failing.

data = self._transform(data)
return self.function_from_transform(data)

def noisy_function(self, x: tp.ArrayLike) -> float:
def noisy_function(self, *x: tp.ArrayLike) -> float:
data = np.concatenate(x, axis=0) # we can concatenate since blocks are necessarily sorted
return _noisy_call(
x=np.array(x, copy=False),
x=data,
transf=self._transform,
func=self.function_from_transform,
noise_level=self._parameters["noise_level"],
noise_dissymmetry=self._parameters["noise_dissymmetry"],
random_state=self._parametrization.random_state,
)

def compute_pseudotime(self, input_parameter: tp.Any, loss: tp.Loss) -> float:
def compute_pseudotime(self, input_parameter: tp.ArgsKwargs, loss: tp.Loss) -> float:
"""Delay before returning results in steady state mode benchmarks (fake execution time)"""
args, kwargs = input_parameter
assert not kwargs
assert len(args) == 1
if hasattr(self._func, "compute_pseudotime"):
data = self._transform(args[0])
data = self._transform(np.concatenate(args, axis=0))
total = 0.0
for block in data:
total += self._func.compute_pseudotime(((block,), {}), loss) # type: ignore
Expand Down
12 changes: 12 additions & 0 deletions nevergrad/functions/test_functionlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ def test_oracle() -> None:
np.testing.assert_array_almost_equal(y3, y4) # should be equal


@pytest.mark.parametrize("split", [True, False]) # type: ignore
def test_blocks(split: bool) -> None:
func = functionlib.ArtificialFunction("sphere", block_dimension=5, split=split, num_blocks=2)
assert func.dimension == 10
param = func.parametrization
y = func(*param.args, **param.kwargs)
y2 = func.evaluation_function(param) # returns a float
t = func.compute_pseudotime((param.args, param.kwargs), y2)
assert t > 0
np.testing.assert_array_almost_equal(y, y2) # should be equal


def test_function_transform() -> None:
func = functionlib.ArtificialFunction("sphere", 2, num_blocks=1, noise_level=0.1)
output = func._transform(np.array([0.0, 0]))
Expand Down