-
Notifications
You must be signed in to change notification settings - Fork 0
[Meta Schedule] Add XGBoost Model & Random Model #519
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
Merged
junrushao
merged 4 commits into
tlc-pack:meta-schedule-refactor
from
zxybazh:tensorir-infra/upstream/2021-11-12/xgboost-model
Dec 1, 2021
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| """Cost model metrics for meta schedule""" | ||
| import numpy as np | ||
|
|
||
|
|
||
| def max_curve(trial_scores: np.ndarray): | ||
| """f(n) = max([s[i] fo i < n]) | ||
|
|
||
| Parameters | ||
| ---------- | ||
| trial_scores: Array of float | ||
| the score of i th trial | ||
|
|
||
| Returns | ||
| ------- | ||
| curve: Array of float | ||
| function values | ||
| """ | ||
| ret = np.empty(len(trial_scores)) | ||
| keep = -1e9 | ||
| for i, score in enumerate(trial_scores): | ||
| keep = max(keep, score) | ||
| ret[i] = keep | ||
| return ret | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| """ | ||
| Random cost model | ||
| """ | ||
| from typing import List, Union, Tuple, Optional | ||
|
|
||
| import numpy as np | ||
|
|
||
| from ..runner import RunnerResult | ||
| from ..tune_context import TuneContext | ||
| from ..search_strategy import MeasureCandidate | ||
| from ..cost_model import PyCostModel | ||
|
|
||
|
|
||
| class RandomModel(PyCostModel): | ||
| """Random cost model | ||
|
|
||
| Parameters | ||
| ---------- | ||
| random_state : Union[Tuple[str, np.ndarray, int, int, float], dict] | ||
| The random state of the random number generator. | ||
| path : Optional[str] | ||
| The path of the random cost model. | ||
| max_range : Optional[int] | ||
| The maximum range of random results, [0, max_range]. | ||
|
|
||
| Reference | ||
| --------- | ||
| https://numpy.org/doc/stable/reference/random/generated/numpy.random.get_state.html | ||
| """ | ||
|
|
||
| random_state: Union[Tuple[str, np.ndarray, int, int, float], dict] | ||
| path: Optional[str] | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| seed: Optional[int] = None, | ||
| path: Optional[str] = None, | ||
| max_range: Optional[int] = 100, | ||
| ): | ||
| super().__init__() | ||
| if path is not None: | ||
| self.load(path) | ||
| else: | ||
| np.random.seed(seed) | ||
| self.random_state = np.random.get_state() | ||
zxybazh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.max_range = max_range | ||
|
|
||
| def load(self, path: str) -> None: | ||
| """Load the cost model from given file location. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| path : str | ||
| The file path. | ||
| """ | ||
| self.random_state = tuple(np.load(path, allow_pickle=True)) | ||
|
|
||
| def save(self, path: str) -> None: | ||
| """Save the cost model to given file location. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| path : str | ||
| The file path. | ||
| """ | ||
| np.save(path, np.array(self.random_state, dtype=object), allow_pickle=True) | ||
|
|
||
| def update( | ||
| self, | ||
| tune_context: TuneContext, | ||
| candidates: List[MeasureCandidate], | ||
| results: List[RunnerResult], | ||
| ) -> None: | ||
| """Update the cost model given running results. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| tune_context : TuneContext, | ||
| The tuning context. | ||
| candidates : List[MeasureCandidate] | ||
| The measure candidates. | ||
| results : List[RunnerResult] | ||
| The running results of the measure candidates. | ||
| """ | ||
|
|
||
| def predict(self, tune_context: TuneContext, candidates: List[MeasureCandidate]) -> np.ndarray: | ||
| """Update the cost model given running results. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| tune_context : TuneContext, | ||
| The tuning context. | ||
| candidates : List[MeasureCandidate] | ||
| The measure candidates. | ||
|
|
||
| Return | ||
| ------ | ||
| result : np.ndarray | ||
| The predicted running results. | ||
| """ | ||
| np.random.set_state(self.random_state) | ||
| result = np.random.rand(len(candidates)) * self.max_range | ||
| self.random_state = np.random.get_state() | ||
| return result | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.