-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[MetaSchedule] Add Profiler Support For Tuning Efficiency Optimization #11486
Merged
junrushao
merged 6 commits into
apache:main
from
zxybazh:feature/2022-05-25/tuning-statistics
Jun 13, 2022
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b8cfc67
[MetaSchedule] Add Profiler Support For Tuning Efficiency Optimization
zxybazh 414c021
make C++ impl right
junrushao 81dad82
fix python api and unittests
junrushao a00452b
time evo search
junrushao 07a17c3
Add table
junrushao 4d9b2ee
fix lint
junrushao 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 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,103 @@ | ||
/* | ||
* 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_PROFILER_H_ | ||
#define TVM_META_SCHEDULE_PROFILER_H_ | ||
|
||
#include <tvm/ir/module.h> | ||
#include <tvm/node/reflection.h> | ||
#include <tvm/runtime/container/array.h> | ||
#include <tvm/runtime/container/optional.h> | ||
#include <tvm/runtime/container/string.h> | ||
#include <tvm/runtime/object.h> | ||
#include <tvm/runtime/packed_func.h> | ||
#include <tvm/target/target.h> | ||
|
||
#include <string> | ||
#include <unordered_map> | ||
#include <utility> | ||
#include <vector> | ||
|
||
namespace tvm { | ||
namespace meta_schedule { | ||
|
||
class ScopedTimer { | ||
public: | ||
~ScopedTimer() { | ||
if (deferred_ != nullptr) { | ||
deferred_(); | ||
} | ||
} | ||
|
||
private: | ||
friend class Profiler; | ||
|
||
explicit ScopedTimer(runtime::TypedPackedFunc<void()> deferred) : deferred_(deferred) {} | ||
runtime::TypedPackedFunc<void()> deferred_; | ||
}; | ||
|
||
/*! \brief A generic profiler */ | ||
class ProfilerNode : public runtime::Object { | ||
public: | ||
/*! \brief The segments that are already profiled */ | ||
std::unordered_map<std::string, double> stats_sec; | ||
/*! \brief Counter for the total time used */ | ||
runtime::PackedFunc total_timer; | ||
|
||
void VisitAttrs(tvm::AttrVisitor* v) { | ||
// `stats_sec` is not visited. | ||
// `total_timer` is not visited. | ||
} | ||
|
||
static constexpr const char* _type_key = "meta_schedule.Profiler"; | ||
TVM_DECLARE_FINAL_OBJECT_INFO(ProfilerNode, runtime::Object); | ||
|
||
public: | ||
/*! \brief Get the internal stats of the running time */ | ||
Map<String, FloatImm> Get() const; | ||
/*! \brief Return a summary of profiling results as table format */ | ||
String Table() const; | ||
}; | ||
|
||
/*! | ||
* \brief Managed reference to ProfilerNode | ||
* \sa ProfilerNode | ||
*/ | ||
class Profiler : public runtime::ObjectRef { | ||
public: | ||
Profiler(); | ||
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(Profiler, runtime::ObjectRef, ProfilerNode); | ||
|
||
/*! \brief Entering the scope of the context manager */ | ||
void EnterWithScope(); | ||
/*! \brief Exiting the scope of the context manager */ | ||
void ExitWithScope(); | ||
/*! \brief Returns the current profiler */ | ||
static Optional<Profiler> Current(); | ||
/*! | ||
* \brief Profile the time usage in the given scope in the given name. | ||
* \param name Name for the scope. | ||
* \return A scope timer for time profiling. | ||
*/ | ||
static ScopedTimer TimedScope(String name); | ||
}; | ||
|
||
} // namespace meta_schedule | ||
} // namespace tvm | ||
|
||
#endif // TVM_META_SCHEDULE_PROFILER_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,76 @@ | ||
# 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. | ||
"""A context manager that profiles tuning time cost for different parts.""" | ||
from __future__ import annotations | ||
|
||
import logging | ||
from contextlib import contextmanager | ||
from typing import Dict, Optional | ||
|
||
from tvm._ffi import register_object | ||
from tvm.runtime import Object | ||
|
||
from . import _ffi_api | ||
|
||
logger = logging.getLogger(__name__) # pylint: disable=invalid-name | ||
|
||
|
||
@register_object("meta_schedule.Profiler") | ||
class Profiler(Object): | ||
"""Tuning time profiler.""" | ||
|
||
def __init__(self) -> None: | ||
self.__init_handle_by_constructor__( | ||
_ffi_api.Profiler, # type: ignore # pylint: disable=no-member | ||
) | ||
|
||
def get(self) -> Dict[str, float]: | ||
"""Get the profiling results in minutes""" | ||
return _ffi_api.ProfilerGet(self) # type: ignore # pylint: disable=no-member | ||
|
||
def table(self) -> str: | ||
"""Get the profiling results in a table format""" | ||
return _ffi_api.ProfilerTable(self) # type: ignore # pylint: disable=no-member | ||
|
||
def __enter__(self) -> "Profiler": | ||
"""Entering the scope of the context manager""" | ||
_ffi_api.ProfilerEnterWithScope(self) # type: ignore # pylint: disable=no-member | ||
return self | ||
|
||
def __exit__(self, ptype, value, trace) -> None: | ||
"""Exiting the scope of the context manager""" | ||
_ffi_api.ProfilerExitWithScope(self) # type: ignore # pylint: disable=no-member | ||
|
||
@staticmethod | ||
def current() -> Optional["Profiler"]: | ||
"""Get the current profiler.""" | ||
return _ffi_api.ProfilerCurrent() # type: ignore # pylint: disable=no-member | ||
|
||
@staticmethod | ||
def timeit(name: str): | ||
"""Timeit a block of code""" | ||
|
||
@contextmanager | ||
def _timeit(): | ||
try: | ||
f = _ffi_api.ProfilerTimedScope(name) # type: ignore # pylint: disable=no-member | ||
yield | ||
finally: | ||
if f: | ||
f() | ||
|
||
return _timeit() |
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
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is only available in Python 3.7+, and fails with 3.6.
This breaks our downstream builds. @zxybazh @junrushao1994
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for reporting this issue! Will remove for compatibility.