-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[M3c][Meta Schedule] Measure Callbacks (#498)
* Add MeasureCallbacks. * ove measure callback to task scheduler level.
- Loading branch information
Showing
16 changed files
with
515 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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,125 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#ifndef TVM_META_SCHEDULE_MEASURE_CALLBACK_H_ | ||
#define TVM_META_SCHEDULE_MEASURE_CALLBACK_H_ | ||
|
||
#include <tvm/meta_schedule/builder.h> | ||
#include <tvm/meta_schedule/runner.h> | ||
#include <tvm/meta_schedule/search_strategy.h> | ||
#include <tvm/meta_schedule/tune_context.h> | ||
|
||
namespace tvm { | ||
namespace meta_schedule { | ||
|
||
class TaskScheduler; | ||
|
||
/*! \brief Rules to apply after measure results is available. */ | ||
class MeasureCallbackNode : public runtime::Object { | ||
public: | ||
/*! \brief Virtual destructor. */ | ||
virtual ~MeasureCallbackNode() = default; | ||
|
||
void VisitAttrs(tvm::AttrVisitor* v) {} | ||
|
||
/*! | ||
* \brief Apply a measure callback rule with given arguments. | ||
* \param task_scheduler The task scheduler. | ||
* \param tasks The list of tune context to process. | ||
* \param measure_candidates The measure candidates. | ||
* \param builds The builder results by building the measure candidates. | ||
* \param results The runner results by running the built measure candidates. | ||
* \return Whether the measure callback was successfully applied. | ||
*/ | ||
virtual bool Apply(const TaskScheduler& task_scheduler, // | ||
const Array<TuneContext> tasks, // | ||
const Array<MeasureCandidate>& measure_candidates, // | ||
const Array<BuilderResult>& builds, // | ||
const Array<RunnerResult>& results) = 0; | ||
|
||
static constexpr const char* _type_key = "meta_schedule.MeasureCallback"; | ||
TVM_DECLARE_BASE_OBJECT_INFO(MeasureCallbackNode, Object); | ||
}; | ||
|
||
/*! \brief The measure callback with customized methods on the python-side. */ | ||
class PyMeasureCallbackNode : public MeasureCallbackNode { | ||
public: | ||
/*! | ||
* \brief Apply a measure callback to the given schedule. | ||
* \param task_scheduler The task scheduler. | ||
* \param tasks The list of tune context to process. | ||
* \param measure_candidates The measure candidates. | ||
* \param builds The builder results by building the measure candidates. | ||
* \param results The runner results by running the built measure candidates. | ||
* \return Whether the measure callback was successfully applied. | ||
*/ | ||
using FApply = | ||
runtime::TypedPackedFunc<bool(const TaskScheduler& task_scheduler, // | ||
const Array<TuneContext> tasks, // | ||
const Array<MeasureCandidate>& measure_candidates, // | ||
const Array<BuilderResult>& builds, // | ||
const Array<RunnerResult>& results)>; | ||
/*! | ||
* \brief Get the measure callback function as string with name. | ||
* \return The string of the measure callback function. | ||
*/ | ||
using FAsString = runtime::TypedPackedFunc<String()>; | ||
|
||
/*! \brief The packed function to the `Apply` funcion. */ | ||
FApply f_apply; | ||
/*! \brief The packed function to the `AsString` funcion. */ | ||
FAsString f_as_string; | ||
|
||
void VisitAttrs(tvm::AttrVisitor* v) { | ||
// `f_apply` is not visited | ||
// `f_as_string` is not visited | ||
} | ||
|
||
bool Apply(const TaskScheduler& task_scheduler, // | ||
const Array<TuneContext> tasks, // | ||
const Array<MeasureCandidate>& measure_candidates, // | ||
const Array<BuilderResult>& builds, // | ||
const Array<RunnerResult>& results) final { | ||
return this->f_apply(task_scheduler, tasks, measure_candidates, builds, results); | ||
} | ||
|
||
static constexpr const char* _type_key = "meta_schedule.PyMeasureCallback"; | ||
TVM_DECLARE_FINAL_OBJECT_INFO(PyMeasureCallbackNode, MeasureCallbackNode); | ||
}; | ||
|
||
/*! | ||
* \brief Managed reference to MeasureCallbackNode | ||
* \sa MeasureCallbackNode | ||
*/ | ||
class MeasureCallback : public runtime::ObjectRef { | ||
public: | ||
/*! | ||
* \brief Create a measure callback with customized methods on the python-side. | ||
* \param f_apply The packed function of `Apply`. | ||
* \return The measure callback created. | ||
*/ | ||
TVM_DLL static MeasureCallback PyMeasureCallback(PyMeasureCallbackNode::FApply f_apply, // | ||
PyMeasureCallbackNode::FAsString f_as_string); | ||
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(MeasureCallback, ObjectRef, MeasureCallbackNode); | ||
}; | ||
|
||
} // namespace meta_schedule | ||
} // namespace tvm | ||
|
||
#endif // TVM_META_SCHEDULE_MEASURE_CALLBACK_H_ |
This file contains 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 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,20 @@ | ||
# 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. | ||
""" | ||
The tvm.meta_schedule.measure_callback package. | ||
""" | ||
from .measure_callback import MeasureCallback, PyMeasureCallback |
99 changes: 99 additions & 0 deletions
99
python/tvm/meta_schedule/measure_callback/measure_callback.py
This file contains 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,99 @@ | ||
# 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. | ||
"""Meta Schedule MeasureCallback.""" | ||
|
||
from typing import TYPE_CHECKING, List | ||
|
||
from tvm._ffi import register_object | ||
from tvm.runtime import Object | ||
from tvm.meta_schedule import TuneContext | ||
from tvm.meta_schedule.search_strategy import MeasureCandidate | ||
from tvm.meta_schedule.builder import BuilderResult | ||
from tvm.meta_schedule.runner import RunnerResult | ||
from tvm.meta_schedule.utils import _get_hex_address | ||
|
||
from .. import _ffi_api | ||
|
||
if TYPE_CHECKING: | ||
from ..tune_context import TuneContext | ||
from ..task_scheduler import TaskScheduler | ||
|
||
|
||
@register_object("meta_schedule.MeasureCallback") | ||
class MeasureCallback(Object): | ||
"""Rules to apply after measure results is available.""" | ||
|
||
def apply( | ||
self, | ||
task_scheduler: "TaskScheduler", | ||
tasks: List["TuneContext"], | ||
measure_candidates: List[MeasureCandidate], | ||
builds: List[BuilderResult], | ||
results: List[RunnerResult], | ||
) -> bool: | ||
"""Apply a measure callback to the given schedule. | ||
Parameters | ||
---------- | ||
task_scheduler: TaskScheduler | ||
The task scheduler. | ||
tasks: List[TuneContext] | ||
The list of tune context to process. | ||
measure_candidats: List[MeasureCandidate] | ||
The measure candidates. | ||
builds: List[BuilderResult] | ||
The builder results by building the measure candidates. | ||
results: List[RunnerResult] | ||
The runner results by running the built measure candidates. | ||
Returns | ||
------- | ||
result : bool | ||
Whether the measure callback was successfully applied. | ||
""" | ||
return _ffi_api.MeasureCallbackApply( | ||
self, task_scheduler, tasks, measure_candidates, builds, results | ||
) | ||
|
||
|
||
@register_object("meta_schedule.PyMeasureCallback") | ||
class PyMeasureCallback(MeasureCallback): | ||
"""An abstract MeasureCallback with customized methods on the python-side.""" | ||
|
||
def __init__(self): | ||
"""Constructor.""" | ||
|
||
def f_apply( | ||
task_scheduler: "TaskScheduler", | ||
tasks: List[TuneContext], | ||
measure_candidates: List[MeasureCandidate], | ||
builds: List[BuilderResult], | ||
results: List[RunnerResult], | ||
) -> bool: | ||
return self.apply(task_scheduler, tasks, measure_candidates, builds, results) | ||
|
||
def f_as_string() -> str: | ||
return str(self) | ||
|
||
self.__init_handle_by_constructor__( | ||
_ffi_api.MeasureCallbackPyMeasureCallback, # type: ignore # pylint: disable=no-member | ||
f_apply, | ||
f_as_string, | ||
) | ||
|
||
def __str__(self) -> str: | ||
return f"PyMeasureCallback({_get_hex_address(self.handle)})" |
This file contains 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 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 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 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 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
Oops, something went wrong.