Skip to content

A programming framework for agentic AI

fix UnboundLocalError in tune.run (#142) (#145)

Eduardo Büll46752083a2b3
diff --git a/flaml/tune/tune.py b/flaml/tune/tune.py
index 132e4d20f..fe4ac1cd5 100644
--- a/flaml/tune/tune.py
+++ b/flaml/tune/tune.py
@@ -334,9 +334,9 @@ def run(training_function,
result = training_function(trial_to_run.config)
if result is not None:
if isinstance(result, dict):
- tune.report(**result)
+ report(**result)
else:
- tune.report(_metric=result)
+ report(_metric=result)
_runner.stop_trial(trial_to_run)
if verbose > 0:
logger.handlers.clear()
diff --git a/test/tune/test_tune.py b/test/tune/test_tune.py
index 0252c973f..8ab7d4e06 100644
--- a/test/tune/test_tune.py
+++ b/test/tune/test_tune.py
@@ -217,6 +217,37 @@ def test_nested():
logger.info(f"BlendSearch best result: {best_trial.last_result}")
+def test_run_training_function_return_value():
+ from flaml import tune
+
+ # Test dict return value
+ def evaluate_config_dict(config):
+ metric = (round(config['x'])-85000)**2 - config['x']/config['y']
+ return {"metric": metric}
+
+ tune.run(
+ evaluate_config_dict,
+ config={
+ 'x': tune.lograndint(lower=1, upper=100000),
+ 'y': tune.randint(lower=1, upper=100000)
+ },
+ metric='metric',
+ )
+
+ # Test scalar return value
+ def evaluate_config_scalar(config):
+ metric = (round(config['x'])-85000)**2 - config['x']/config['y']
+ return metric
+
+ tune.run(
+ evaluate_config_scalar,
+ config={
+ 'x': tune.lograndint(lower=1, upper=100000),
+ 'y': tune.randint(lower=1, upper=100000)
+ },
+ )
+
+
def test_xgboost_bs():
_test_xgboost()