def test_register_bracket_multi_fidelity(self, space, b_config): """Check that a point is registered inside the same bracket for diff fidelity.""" asha = ASHA(space, num_brackets=3) value = 50 fidelity = 1 trial = create_trial_for_hb((fidelity, value), 0.0) trial_id = asha.get_id(trial, ignore_fidelity=True) force_observe(asha, trial) bracket = asha.brackets[0] assert len(bracket.rungs[0]) assert trial_id in bracket.rungs[0]["results"] assert bracket.rungs[0]["results"][trial_id][0] == 0.0 assert bracket.rungs[0]["results"][trial_id][1].params == trial.params fidelity = 3 trial = create_trial_for_hb((fidelity, value), 0.0) trial_id = asha.get_id(trial, ignore_fidelity=True) force_observe(asha, trial) assert len(bracket.rungs[1]) assert trial_id in bracket.rungs[1]["results"] assert bracket.rungs[0]["results"][trial_id][1].params != trial.params assert bracket.rungs[1]["results"][trial_id][0] == 0.0 assert bracket.rungs[1]["results"][trial_id][1].params == trial.params
def test_get_id(self, space, b_config): """Test valid id of points""" asha = ASHA(space, max_resources=b_config['R'], grace_period=b_config['r'], reduction_factor=b_config['eta'], num_brackets=3) assert asha.get_id([1, 'whatever']) == asha.get_id([1, 'is here']) assert asha.get_id([1, 'whatever']) != asha.get_id([2, 'is here'])
def test_get_id_multidim(self, b_config): """Test valid id for points with dim of shape > 1""" space = Space() space.register(Fidelity('epoch', 1, 9, 3)) space.register(Real('lr', 'uniform', 0, 1, shape=2)) asha = ASHA(space, num_brackets=3) assert asha.get_id(['whatever', [1, 1]]) == asha.get_id(['is here', [1, 1]]) assert asha.get_id(['whatever', [1, 1]]) != asha.get_id( ['is here', [2, 2]])
def test_get_id_multidim(self, b_config): """Test valid id for points with dim of shape > 1""" space = Space() space.register(Fidelity("epoch", 1, 9, 3)) space.register(Real("lr", "uniform", 0, 1, shape=2)) asha = ASHA(space, num_brackets=3) assert asha.get_id(["whatever", [1, 1]]) == asha.get_id(["is here", [1, 1]]) assert asha.get_id(["whatever", [1, 1]]) != asha.get_id( ["is here", [2, 2]])
def test_get_id_multidim(self, b_config): """Test valid id for points with dim of shape > 1""" space = Space() space.register(Fidelity('epoch')) space.register(Real('lr', 'uniform', 0, 1, shape=2)) asha = ASHA(space, max_resources=b_config['R'], grace_period=b_config['r'], reduction_factor=b_config['eta'], num_brackets=3) assert asha.get_id(['whatever', [1, 1]]) == asha.get_id(['is here', [1, 1]]) assert asha.get_id(['whatever', [1, 1]]) != asha.get_id( ['is here', [2, 2]])
def test_update_rungs_return_candidate(self, asha: ASHA, bracket: ASHABracket, rung_1: RungDict): """Check if a valid modified candidate is returned by update_rungs.""" assert bracket.owner is asha bracket.rungs[1] = rung_1 trial = create_trial_for_hb((3, 0.0), 0.0) candidate = bracket.promote(1)[0] trial_id = asha.get_id(trial, ignore_fidelity=True) assert trial_id in bracket.rungs[1]["results"] assert bracket.rungs[1]["results"][trial_id][1].params == trial.params assert candidate.params["epoch"] == 9
def test_register_next_bracket(self, space, b_config): """Check that a point is registered inside the good bracket when higher fidelity.""" asha = ASHA(space, num_brackets=3) value = 50 fidelity = 3 trial = create_trial_for_hb((fidelity, value), 0.0) trial_id = asha.get_id(trial, ignore_fidelity=True) force_observe(asha, trial) assert sum(len(rung["results"]) for rung in asha.brackets[0].rungs) == 0 assert sum(len(rung["results"]) for rung in asha.brackets[1].rungs) == 1 assert sum(len(rung["results"]) for rung in asha.brackets[2].rungs) == 0 assert trial_id in asha.brackets[1].rungs[0]["results"] compare_registered_trial( asha.brackets[1].rungs[0]["results"][trial_id], trial) value = 51 fidelity = 9 trial = create_trial_for_hb((fidelity, value), 0.0) trial_id = asha.get_id(trial, ignore_fidelity=True) force_observe(asha, trial) assert sum(len(rung["results"]) for rung in asha.brackets[0].rungs) == 0 assert sum(len(rung["results"]) for rung in asha.brackets[1].rungs) == 1 assert sum(len(rung["results"]) for rung in asha.brackets[2].rungs) == 1 assert trial_id in asha.brackets[2].rungs[0]["results"] compare_registered_trial( asha.brackets[2].rungs[0]["results"][trial_id], trial)
def test_register(self, asha: ASHA, bracket: ASHABracket, rung_0: RungDict, rung_1: RungDict): """Check that a point is registered inside the bracket.""" asha.brackets = [bracket] assert bracket.owner is asha bracket.rungs = [rung_0, rung_1] trial = create_trial_for_hb((1, 0.0), 0.0) trial_id = asha.get_id(trial, ignore_fidelity=True) asha.observe([trial]) assert len(bracket.rungs[0]) assert trial_id in bracket.rungs[0]["results"] assert bracket.rungs[0]["results"][trial_id][0] == 0.0 assert bracket.rungs[0]["results"][trial_id][1].params == trial.params
def test_promotion_with_rung_1_hit(self, asha: ASHA, bracket: ASHABracket, rung_0: RungDict): """Test that get_candidate gives us the next best thing if point is already in rung 1.""" trial = create_trial_for_hb((1, 0.0), None) assert bracket.owner is asha bracket.rungs[0] = rung_0 assert trial.objective is not None bracket.rungs[1]["results"][asha.get_id(trial, ignore_fidelity=True)] = ( trial.objective.value, trial, ) trial = bracket.get_candidates(0)[0] assert trial.params == create_trial_for_hb((1, 1.0), 0.0).params
def test_suggest_inf_duplicates( self, monkeypatch, asha: ASHA, bracket: ASHABracket, rung_0: RungDict, rung_1: RungDict, rung_2: RungDict, ): """Test that sampling inf collisions returns None.""" asha.brackets = [bracket] assert bracket.owner is asha fidelity = 1 zhe_trial = create_trial_for_hb((fidelity, 0.0)) asha.trial_to_brackets[asha.get_id(zhe_trial, ignore_fidelity=True)] = 0 def sample(num=1, seed=None): return [zhe_trial] monkeypatch.setattr(asha.space, "sample", sample) assert asha.suggest(1) == []
def test_suggest_duplicates( self, monkeypatch, asha: ASHA, bracket: ASHABracket, rung_0: RungDict, rung_1: RungDict, rung_2: RungDict, ): """Test that sampling collisions are handled.""" asha.brackets = [bracket] assert bracket.owner is asha fidelity = 1 duplicate_trial = create_trial_for_hb((fidelity, 0.0)) new_trial = create_trial_for_hb((fidelity, 0.5)) duplicate_id_wo_fidelity = asha.get_id(duplicate_trial, ignore_fidelity=True) bracket.rungs[0] = dict( n_trials=2, resources=1, results={duplicate_id_wo_fidelity: (0.0, duplicate_trial)}, ) asha.trial_to_brackets[duplicate_id_wo_fidelity] = 0 asha.register(duplicate_trial) trials = [duplicate_trial, new_trial] def sample(num=1, seed=None): return trials monkeypatch.setattr(asha.space, "sample", sample) assert asha.suggest(1)[0].params == new_trial.params
def test_get_id(self, space, b_config): """Test valid id of points""" asha = ASHA(space, num_brackets=3) assert asha.get_id(['whatever', 1]) == asha.get_id(['is here', 1]) assert asha.get_id(['whatever', 1]) != asha.get_id(['is here', 2])