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

[IR] Expose list of PassContext configuration names to the Python APIs #8212

Merged
merged 1 commit into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/tvm/ir/transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ class PassContext : public ObjectRef {
*/
TVM_DLL static PassContext Current();

/*!
* \brief Get all supported configuration names, registered within the PassContext.
* \return List of all configuration names.
*/
TVM_DLL static Array<String> ListConfigNames();

/*!
* \brief Call instrument implementations' callbacks when entering PassContext.
* The callbacks are called in order, and if one raises an exception, the rest will not be
Expand Down
5 changes: 5 additions & 0 deletions python/tvm/ir/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ def current():
"""Return the current pass context."""
return _ffi_transform_api.GetCurrentPassContext()

@staticmethod
def list_config_names():
"""List all registered `PassContext` configuration names"""
return list(_ffi_transform_api.ListConfigNames())


@tvm._ffi.register_object("transform.Pass")
class Pass(tvm.runtime.Object):
Expand Down
14 changes: 14 additions & 0 deletions src/ir/transform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ class PassConfigManager {
}
}

Array<String> ListConfigNames() {
Array<String> config_keys;
for (const auto& kv : key2vtype_) {
config_keys.push_back(kv.first);
}
return config_keys;
}

static PassConfigManager* Global() {
static auto* inst = new PassConfigManager();
return inst;
Expand All @@ -163,6 +171,10 @@ void PassContext::RegisterConfigOption(const char* key, uint32_t value_type_inde
PassConfigManager::Global()->Register(key, value_type_index);
}

Array<String> PassContext::ListConfigNames() {
return PassConfigManager::Global()->ListConfigNames();
}

PassContext PassContext::Create() { return PassContext(make_object<PassContextNode>()); }

void PassContext::InstrumentEnterPassContext() {
Expand Down Expand Up @@ -607,5 +619,7 @@ Pass PrintIR(String header, bool show_meta_data) {

TVM_REGISTER_GLOBAL("transform.PrintIR").set_body_typed(PrintIR);

TVM_REGISTER_GLOBAL("transform.ListConfigNames").set_body_typed(PassContext::ListConfigNames);

} // namespace transform
} // namespace tvm
7 changes: 7 additions & 0 deletions tests/cpp/relay_transform_sequential_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ TEST(Relay, Sequential) {
ICHECK(tvm::StructuralEqual()(f, expected));
}

TEST(PassContextListConfigNames, Basic) {
Array<String> configs = relay::transform::PassContext::ListConfigNames();
ICHECK_EQ(configs.empty(), false);
ICHECK_EQ(std::count(std::begin(configs), std::end(configs), "relay.backend.use_auto_scheduler"),
1);
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "threadsafe";
Expand Down
7 changes: 7 additions & 0 deletions tests/python/relay/test_pass_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ def run_after_pass(self, mod, info):
assert passes_counter.run_after_count == 0


def test_list_pass_configs():
config_names = tvm.transform.PassContext.list_config_names()

assert len(config_names) > 0
assert "relay.backend.use_auto_scheduler" in config_names


def test_enter_pass_ctx_exception():
events = []

Expand Down