forked from apache/tvm
-
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.
[Meta Schedule][M3a] SpaceGenerator (apache#9079)
* Add meta shedule space generator. Co-authored-by: Junru Shao <[email protected]> Co-authored-by: Bohan Hou <[email protected]> Co-authored-by: Ruihang Lai <[email protected]> Co-authored-by: Hongyi Jin <[email protected]> Co-authored-by: Wuwei Lin <[email protected]> Co-authored-by: Siyuan Feng <[email protected]> * Clean up. * Minor fix. * Move utils.h. Co-authored-by: Junru Shao <[email protected]> Co-authored-by: Bohan Hou <[email protected]> Co-authored-by: Ruihang Lai <[email protected]> Co-authored-by: Hongyi Jin <[email protected]> Co-authored-by: Wuwei Lin <[email protected]> Co-authored-by: Siyuan Feng <[email protected]>
- Loading branch information
Showing
14 changed files
with
599 additions
and
6 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,122 @@ | ||
/* | ||
* 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_SPACE_GENERATOR_H_ | ||
#define TVM_META_SCHEDULE_SPACE_GENERATOR_H_ | ||
|
||
#include <tvm/ir/module.h> | ||
#include <tvm/tir/schedule/schedule.h> | ||
|
||
namespace tvm { | ||
namespace meta_schedule { | ||
|
||
// Forward declaration | ||
class TuneContext; | ||
|
||
/*! \brief The abstract class for design space generation. */ | ||
class SpaceGeneratorNode : public Object { | ||
public: | ||
/*! \brief Default destructor */ | ||
virtual ~SpaceGeneratorNode() = default; | ||
|
||
/*! | ||
* \brief Initialize the design space generator with tuning context. | ||
* \param tune_context The tuning context for initialization. | ||
*/ | ||
virtual void InitializeWithTuneContext(const TuneContext& tune_context) = 0; | ||
|
||
/*! | ||
* \brief Generate design spaces given a module. | ||
* \param mod The module used for design space generation. | ||
* \return The generated design spaces, i.e., schedules. | ||
*/ | ||
virtual Array<tir::Schedule> GenerateDesignSpace(const IRModule& mod) = 0; | ||
|
||
static constexpr const char* _type_key = "meta_schedule.SpaceGenerator"; | ||
TVM_DECLARE_BASE_OBJECT_INFO(SpaceGeneratorNode, Object); | ||
}; | ||
|
||
/*! \brief The design space generator with customized methods on the python-side. */ | ||
class PySpaceGeneratorNode : public SpaceGeneratorNode { | ||
public: | ||
/*! | ||
* \brief The function type of `InitializeWithTuneContext` method. | ||
* \param tune_context The tuning context for initialization. | ||
*/ | ||
using FInitializeWithTuneContext = runtime::TypedPackedFunc<void(const TuneContext&)>; | ||
/*! | ||
* \brief The function type of `GenerateDesignSpace` method. | ||
* \param mod The module used for design space generation. | ||
* \return The generated design spaces, i.e., schedules. | ||
*/ | ||
using FGenerateDesignSpace = runtime::TypedPackedFunc<Array<tir::Schedule>(const IRModule&)>; | ||
|
||
/*! \brief The packed function to the `InitializeWithTuneContext` funcion. */ | ||
FInitializeWithTuneContext f_initialize_with_tune_context; | ||
/*! \brief The packed function to the `GenerateDesignSpace` function. */ | ||
FGenerateDesignSpace f_generate_design_space; | ||
|
||
void VisitAttrs(tvm::AttrVisitor* v) { | ||
// `f_initialize_with_tune_context` is not visited | ||
// `f_generate_design_space` is not visited | ||
} | ||
|
||
void InitializeWithTuneContext(const TuneContext& tune_context) final { | ||
f_initialize_with_tune_context(tune_context); | ||
} | ||
|
||
Array<tir::Schedule> GenerateDesignSpace(const IRModule& mod) final { | ||
return f_generate_design_space(mod); | ||
} | ||
|
||
static constexpr const char* _type_key = "meta_schedule.PySpaceGenerator"; | ||
TVM_DECLARE_FINAL_OBJECT_INFO(PySpaceGeneratorNode, SpaceGeneratorNode); | ||
}; | ||
|
||
/*! | ||
* \brief Managed reference to SpaceGeneratorNode. | ||
* \sa SpaceGeneratorNode | ||
*/ | ||
class SpaceGenerator : public ObjectRef { | ||
protected: | ||
SpaceGenerator() = default; | ||
|
||
public: | ||
/*! | ||
* \brief Create a design space generator with customized methods on the python-side. | ||
* \param initialize_with_tune_context_func The packed function of `InitializeWithTuneContext`. | ||
* \param generate_design_space_func The packed function of `GenerateDesignSpace`. | ||
* \return The design space generator created. | ||
*/ | ||
TVM_DLL static SpaceGenerator PySpaceGenerator( | ||
PySpaceGeneratorNode::FInitializeWithTuneContext initialize_with_tune_context_func, | ||
PySpaceGeneratorNode::FGenerateDesignSpace generate_design_space_func); | ||
|
||
/*! | ||
* \brief Create a design space generator that is union of multiple design space generators. | ||
* \param space_generators An array of design space generators to be unioned. | ||
* \return The design space generator created. | ||
*/ | ||
TVM_DLL static SpaceGenerator SpaceGeneratorUnion(Array<SpaceGenerator, void> space_generators); | ||
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(SpaceGenerator, ObjectRef, SpaceGeneratorNode); | ||
}; | ||
|
||
} // namespace meta_schedule | ||
} // namespace tvm | ||
|
||
#endif // TVM_META_SCHEDULE_SPACE_GENERATOR_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
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,25 @@ | ||
# 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.space_generator package. | ||
Meta Schedule design space generators that generates design | ||
space for generation of measure candidates. | ||
""" | ||
|
||
from .space_generator import SpaceGenerator, PySpaceGenerator | ||
from .space_generator_union import SpaceGeneratorUnion | ||
from .schedule_fn import ScheduleFn |
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,90 @@ | ||
# 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 design space generators that generates design | ||
space via a schedule function. | ||
""" | ||
from typing import TYPE_CHECKING, Callable, List, Union | ||
|
||
from tvm.ir import IRModule | ||
from tvm.ir.container import Array | ||
from tvm.tir.schedule import Schedule | ||
|
||
from .space_generator import PySpaceGenerator | ||
|
||
if TYPE_CHECKING: | ||
from ..tune_context import TuneContext | ||
|
||
|
||
class ScheduleFn(PySpaceGenerator): | ||
"""A design space generator with design spaces specified by a schedule function.""" | ||
|
||
# Multiple cases of schedule functions supported | ||
SCH_FN_TYPE = Union[ | ||
Callable[[IRModule], None], # No output | ||
Callable[[IRModule], Schedule], # Single output | ||
Callable[[IRModule], List[Schedule]], # Multiple outputs | ||
] | ||
|
||
def __init__(self, sch_fn: SCH_FN_TYPE): | ||
"""Constructor. | ||
Parameters | ||
---------- | ||
sch_fn : SCH_FN_TYPE | ||
The schedule function. | ||
""" | ||
super().__init__() | ||
self.sch_fn = sch_fn | ||
|
||
def initialize_with_tune_context(self, tune_context: "TuneContext") -> None: | ||
"""Initialize the design space generator with tuning context. | ||
Parameters | ||
---------- | ||
tune_context : TuneContext | ||
The tuning context for initializing the design space generator. | ||
""" | ||
|
||
def generate_design_space(self, mod: IRModule) -> List[Schedule]: | ||
"""Generate design spaces given a module. | ||
Parameters | ||
---------- | ||
mod : IRModule | ||
The module used for design space generation. | ||
Returns | ||
------- | ||
design_spaces : List[Schedule] | ||
The generated design spaces, i.e., schedules. | ||
""" | ||
sch = Schedule(mod) # Make sure the schedule is traced | ||
result = self.sch_fn(sch) # Call the schedule function | ||
if result is None: # Case 1. No output | ||
return [sch] | ||
if isinstance(result, Schedule): # Case 2. Single output | ||
return [result] | ||
if isinstance(result, (list, tuple, Array)): # Case 3. Multiple outputs | ||
for ret in result: # enumerate the outputs | ||
if not isinstance(ret, Schedule): | ||
raise TypeError( | ||
"Wrong type of element in the list, expected Schedule got " | ||
+ f"'{type(ret)}': {ret}" | ||
) | ||
return result | ||
raise TypeError(f"Unexpected return type {type(result)}: {result}") |
93 changes: 93 additions & 0 deletions
93
python/tvm/meta_schedule/space_generator/space_generator.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,93 @@ | ||
# 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 design space generators that generates design | ||
space for generation of measure candidates. | ||
""" | ||
|
||
from typing import TYPE_CHECKING, List | ||
|
||
from tvm._ffi import register_object | ||
from tvm.ir import IRModule | ||
from tvm.runtime import Object | ||
from tvm.tir.schedule import Schedule | ||
|
||
from .. import _ffi_api | ||
|
||
if TYPE_CHECKING: | ||
from ..tune_context import TuneContext | ||
|
||
|
||
@register_object("meta_schedule.SpaceGenerator") | ||
class SpaceGenerator(Object): | ||
"""The abstract design space generator interface.""" | ||
|
||
def initialize_with_tune_context( | ||
self, | ||
tune_context: "TuneContext", | ||
) -> None: | ||
"""Initialize the design space generator with tuning context. | ||
Parameters | ||
---------- | ||
tune_context : TuneContext | ||
The tuning context for initializing the design space generator. | ||
""" | ||
_ffi_api.SpaceGeneratorInitializeWithTuneContext( # type: ignore # pylint: disable=no-member | ||
self, tune_context | ||
) | ||
|
||
def generate_design_space(self, mod: IRModule) -> List[Schedule]: | ||
"""Generate design spaces given a module. | ||
Parameters | ||
---------- | ||
mod : IRModule | ||
The module used for design space generation. | ||
Returns | ||
------- | ||
design_spaces : List[Schedule] | ||
The generated design spaces, i.e., schedules. | ||
""" | ||
return _ffi_api.SpaceGeneratorGenerateDesignSpace(self, mod) # type: ignore # pylint: disable=no-member | ||
|
||
|
||
@register_object("meta_schedule.PySpaceGenerator") | ||
class PySpaceGenerator(SpaceGenerator): | ||
"""An abstract design space generator with customized methods on the python-side.""" | ||
|
||
def __init__(self): | ||
"""Constructor.""" | ||
|
||
def f_initialize_with_tune_context(tune_context: "TuneContext") -> None: | ||
self.initialize_with_tune_context(tune_context) | ||
|
||
def f_generate_design_space(mod: IRModule) -> List[Schedule]: | ||
return self.generate_design_space(mod) | ||
|
||
self.__init_handle_by_constructor__( | ||
_ffi_api.SpaceGeneratorPySpaceGenerator, # type: ignore # pylint: disable=no-member | ||
f_initialize_with_tune_context, | ||
f_generate_design_space, | ||
) | ||
|
||
def initialize_with_tune_context(self, tune_context: "TuneContext") -> None: | ||
raise NotImplementedError | ||
|
||
def generate_design_space(self, mod: IRModule) -> List[Schedule]: | ||
raise NotImplementedError |
Oops, something went wrong.