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

support string alg in tune #1093

Merged
merged 9 commits into from
Jul 1, 2023
Merged

Conversation

skzhang1
Copy link
Collaborator

@skzhang1 skzhang1 commented Jun 27, 2023

Why are these changes needed?

Support specifying search algorithm by passing the corresponding string in tune.

Related issue number

N/A

Checks

@skzhang1 skzhang1 requested review from sonichi and qingyun-wu and removed request for sonichi June 27, 2023 16:13
Copy link
Contributor

@sonichi sonichi left a comment

Choose a reason for hiding this comment

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

Please add a test.

search_alg: An instance of BlendSearch as the search algorithm
to be used. The same instance can be used for iterative tuning.
search_alg: An instance/string of the search algorithm
to be used. The same instance/string can be used for iterative tuning.
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
to be used. The same instance/string can be used for iterative tuning.
to be used. The same instance can be used for iterative tuning.

My understanding is that this sentence is about using the same instance for iterative tuning.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

resolved

@@ -504,15 +503,19 @@ def easy_objective(config):
try:
import optuna as _

SearchAlgorithm = BlendSearch
SearchAlgorithm = BlendSearch if not search_alg else eval(search_alg)
Copy link
Contributor

Choose a reason for hiding this comment

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

The use of eval is not safe. Could you use an alternative way?

Copy link
Collaborator Author

@skzhang1 skzhang1 Jun 28, 2023

Choose a reason for hiding this comment

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

I use assert to make it feasible.

Copy link
Contributor

Choose a reason for hiding this comment

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

First, I don't like the use of eval. Unless you get an approval from an experience Python engineer, I'm against it. Even locals is better.
Second, the code doesn't look correct in the try...except block. The try part tries to import optuna before using BlendSearch. If the choice is not BlendSearch, we shouldn't enter the try...except block.

"BlendSearch",
"CFO",
], "If lexico_objectives is not None, search_alg must be CFO or BlendSearch."
search_alg.lexico_objectives = lexico_objectives
Copy link
Contributor

Choose a reason for hiding this comment

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

Shall we implement this in set_search_properties? Otherwise we need to document this way of setting lexico_objectives somewhere. Conceptually I think it should be part of set_search_properties.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes. I agree. I support it in CFO for now. Will make it work in BlendSearch in future PR.

@@ -481,12 +481,11 @@ def easy_objective(config):
else:
logger.setLevel(logging.CRITICAL)

from .searcher.blendsearch import BlendSearch, CFO
from .searcher.blendsearch import BlendSearch, CFO, RandomSearch
Copy link
Contributor

Choose a reason for hiding this comment

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

RandomSearch is not used.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

we may use it in eval(search_alg)

@skzhang1 skzhang1 requested a review from sonichi June 28, 2023 16:12
"CFO",
"CFOCat",
"RandomSearch",
], "The string of the search_alg must be feasible."
Copy link
Contributor

Choose a reason for hiding this comment

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

"feasible" is not clear.

@@ -504,15 +503,19 @@ def easy_objective(config):
try:
import optuna as _

SearchAlgorithm = BlendSearch
SearchAlgorithm = BlendSearch if not search_alg else eval(search_alg)
Copy link
Contributor

Choose a reason for hiding this comment

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

First, I don't like the use of eval. Unless you get an approval from an experience Python engineer, I'm against it. Even locals is better.
Second, the code doesn't look correct in the try...except block. The try part tries to import optuna before using BlendSearch. If the choice is not BlendSearch, we shouldn't enter the try...except block.

@@ -317,6 +317,7 @@ def set_search_properties(
metric: Optional[str] = None,
mode: Optional[str] = None,
config: Optional[Dict] = None,
lexico_objectives: Optional[Dict] = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this argument added?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

removed

# Iterative training function - can be any arbitrary training procedure
_evaluation_fn(step, width, height)
# Feed the score back back to Tune.
print("Trial stopped", step)
Copy link
Contributor

Choose a reason for hiding this comment

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

remove?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

agree

test/tune/test_tune.py Show resolved Hide resolved
@skzhang1 skzhang1 requested a review from sonichi June 29, 2023 08:08
)
metric = lexico_objectives["metrics"][0] or DEFAULT_METRIC
else:
if not search_alg or search_alg == "BlendSearch":
Copy link
Contributor

Choose a reason for hiding this comment

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

when search_alg=="BlendSearch", we shouldn't automatically switch to CFO. We should raise an error if the import fails.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes. resolved.

Comment on lines +583 to +585
assert search_alg.__class__.__name__ in [
"CFO",
], "If lexico_objectives is not None, the search_alg must be CFO for now."
Copy link
Contributor

Choose a reason for hiding this comment

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

Such assertions and logging happen in different places: here, 519, 487. Could you add TODO notes to modify them later in case they are forgotten?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Resolved

# lexico tune
tune.run(
_BraninCurrin, search_alg="CFO", num_samples=10, config=mo_search_space, lexico_objectives=lexico_objectives
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Add another test with lexico_objectives andsearch_alg= "BlendSearch"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added

logger.warning("If lexico_objectives is not None, search_alg is forced to be CFO")
search_alg = None
if search_alg is None:
logger.warning("If lexico_objectives is not None, search_alg is forced to be CFO for now.")
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not put this warning in the place where SearchAlgorithm is forced to be CFO? i.e., immediately after line 518.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

removed

@skzhang1 skzhang1 self-assigned this Jun 29, 2023
@sonichi sonichi enabled auto-merge June 30, 2023 16:14
@sonichi sonichi added this pull request to the merge queue Jun 30, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jun 30, 2023
@skzhang1 skzhang1 added this pull request to the merge queue Jun 30, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jun 30, 2023
@skzhang1 skzhang1 added this pull request to the merge queue Jun 30, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jul 1, 2023
@skzhang1 skzhang1 enabled auto-merge July 1, 2023 01:01
@skzhang1 skzhang1 added this pull request to the merge queue Jul 1, 2023
Merged via the queue into microsoft:main with commit 7a64148 Jul 1, 2023
@skzhang1 skzhang1 deleted the string_algorithm branch July 1, 2023 05:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants