-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[Metaschedule] Add test case for multi-anchor subgraph #10856
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
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dd3b3de
add test mod
masahi cec5da1
task extraction works
masahi 9b4ea12
trying relay.build
masahi 5f8dffd
test runs but result not correct
masahi c27a481
test worked
masahi b3a3a7c
update te_compiler_cache
masahi cbea61e
use temp dir for database json
masahi a8ac4b0
comment out schedule dump
masahi b9b5e7c
Update src/relay/backend/te_compiler_cache.cc
masahi 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
131 changes: 131 additions & 0 deletions
131
tests/python/unittest/test_meta_schedule_multi_anchor.py
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,131 @@ | ||
| # 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. | ||
| import os | ||
| import tempfile | ||
|
|
||
| import numpy as np | ||
|
|
||
| import tvm | ||
| import tvm.testing | ||
| from tvm import relay | ||
| from tvm.meta_schedule.tune import Parse, extract_task_from_relay | ||
| from tvm.meta_schedule.database import TuningRecord, JSONDatabase | ||
| from tvm.meta_schedule.integration import ApplyHistoryBest | ||
|
|
||
|
|
||
| def get_dense_dense(data_shape, weight_shape): | ||
| def multi_dense(): | ||
| p_data = relay.var("p_data", shape=data_shape, dtype="float32") | ||
| p_weight1 = relay.var("p_weight1", shape=weight_shape, dtype="float32") | ||
| p_weight2 = relay.var("p_weight2", shape=weight_shape, dtype="float32") | ||
|
|
||
| dense1 = relay.nn.dense(p_data, p_weight1) | ||
| dense2 = relay.nn.dense(dense1, p_weight2) | ||
|
|
||
| f = relay.Function([p_data, p_weight1, p_weight2], dense2) | ||
| f = f.with_attr("Primitive", tvm.tir.IntImm("int32", 1)) | ||
| return f | ||
|
|
||
| data = relay.var("data", shape=data_shape, dtype="float32") | ||
| weight1 = relay.var("weight1", shape=weight_shape, dtype="float32") | ||
| weight2 = relay.var("weight2", shape=weight_shape, dtype="float32") | ||
|
|
||
| out = relay.Call(multi_dense(), [data, weight1, weight2]) | ||
| return relay.Function([data, weight1, weight2], out) | ||
|
|
||
|
|
||
| def get_ref(data_np, weight1_np, weight2_np): | ||
| dense1 = np.dot(data_np, np.transpose(weight1_np)) | ||
| return np.dot(dense1, np.transpose(weight2_np)) | ||
|
|
||
|
|
||
| def schedule_dense_dense(sch): | ||
| dense1 = sch.get_block("T_matmul_NT") | ||
| dense2 = sch.get_block("T_matmul_NT_1") | ||
|
|
||
| y1, x1, k1 = sch.get_loops(dense1) | ||
| y2, x2, k2 = sch.get_loops(dense2) | ||
|
|
||
| # ... | ||
|
|
||
|
|
||
| def test_dense_dense(): | ||
| M, N, K = 128, 128, 128 | ||
| data_shape = (M, K) | ||
| weight_shape = (N, K) | ||
|
|
||
| relay_mod = tvm.IRModule.from_expr(get_dense_dense(data_shape, weight_shape)) | ||
|
|
||
| # print(relay.transform.InferType()(relay_mod)) | ||
|
|
||
| target = "llvm" | ||
|
|
||
| data_np = np.random.randn(*data_shape).astype("float32") | ||
| weight1_np = np.random.randn(*weight_shape).astype("float32") | ||
| weight2_np = np.random.randn(*weight_shape).astype("float32") | ||
|
|
||
| params = {"weight1": weight1_np, "weight2": weight2_np} | ||
|
|
||
| extracted_tasks = extract_task_from_relay(relay_mod, target, params) | ||
|
|
||
| assert len(extracted_tasks) == 1 | ||
|
|
||
| task = extracted_tasks[0] | ||
|
|
||
| mod = Parse._mod(task.dispatched[0]) | ||
|
|
||
| with tempfile.TemporaryDirectory() as work_dir: | ||
| database = JSONDatabase( | ||
| path_workload=os.path.join(work_dir, "database_workload.json"), | ||
| path_tuning_record=os.path.join(work_dir, "database_tuning_record.json"), | ||
| ) | ||
|
|
||
| workload = database.commit_workload(mod) | ||
|
|
||
| sch = tvm.tir.Schedule(mod) | ||
|
|
||
| schedule_dense_dense(sch) | ||
|
|
||
| # print(sch.mod.script()) | ||
|
|
||
| tune_rec = TuningRecord(sch.trace, [0.0], workload, tvm.target.Target(target), []) | ||
|
|
||
| database.commit_tuning_record(tune_rec) | ||
|
|
||
| with ApplyHistoryBest(database): | ||
| with tvm.transform.PassContext( | ||
| opt_level=3, | ||
| config={"relay.backend.use_meta_schedule": True}, | ||
| ): | ||
| lib = relay.build(relay_mod, target=target, params=params) | ||
|
|
||
| dev = tvm.device(target, 0) | ||
|
|
||
| runtime = tvm.contrib.graph_executor.GraphModule(lib["default"](dev)) | ||
|
|
||
| runtime.set_input("data", data_np) | ||
| runtime.run() | ||
|
|
||
| out = runtime.get_output(0).numpy() | ||
|
|
||
| ref = get_ref(data_np, weight1_np, weight2_np) | ||
|
|
||
| tvm.testing.assert_allclose(out, ref, atol=1e-4, rtol=1e-4) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| test_dense_dense() | ||
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.
@junrushao1994 @zxybazh
I keep writing this database boilerplate for manual scheduling. I'm thinking about a clean API so that users don't have to go through the explicit task extraction -> database creation steps. Right now it looks like
If this looks ok, I can PR it after this one.
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.
There is a DummyDatabase class in
python/tvm/meta_schedule/testing/utils.pywhere we don't need to create json files for intermediate results, and I was wondering if we could further reduce boilerplate by enhancing that class. What do you think?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.
Yeah using the
Dummyclasses can be very helpful in tuning and it's acutally a good idea to provide new use interface as you mentioned, a schedule function to work on on the relay level. Let me know when you got the PR ready : )