-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Add order to functions in C Codegen #10590
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 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
24f793d
Add function ordering to C Codegen
mehrdadh 795237a
trigger
mehrdadh 3d11372
Merge branch 'main' into aot/fix_function_order
mehrdadh d0e33a2
fix comment
mehrdadh 60de0b7
address comments
mehrdadh f97c405
add test
mehrdadh 25ae939
add unorder check
mehrdadh fea6594
fix test
mehrdadh a95e147
Merge branch 'main' into aot/fix_function_order
mehrdadh ddbe737
address comments
mehrdadh 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
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
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
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
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
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
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
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
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,108 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| #include <gmock/gmock.h> | ||
| #include <gtest/gtest.h> | ||
| #include <tvm/driver/driver_api.h> | ||
| #include <tvm/ir/type.h> | ||
| #include <tvm/node/reflection.h> | ||
| #include <tvm/runtime/metadata.h> | ||
| #include <tvm/runtime/module.h> | ||
| #include <tvm/target/target.h> | ||
| #include <tvm/te/operation.h> | ||
|
|
||
| TEST(CCodegen, MainFunctionOrder) { | ||
| using namespace tvm; | ||
| using namespace tvm::te; | ||
|
|
||
| std::string tvm_module_main = std::string(runtime::symbol::tvm_module_main); | ||
|
|
||
| tvm::Target target_c = tvm::Target("c -keys=cpu -link-params=0"); | ||
|
|
||
| const int n = 4; | ||
| Array<PrimExpr> shape{n}; | ||
|
|
||
| auto A = placeholder(shape, DataType::Float(32), "A"); | ||
| auto B = placeholder(shape, DataType::Float(32), "B"); | ||
|
|
||
| auto elemwise_add = compute( | ||
| A->shape, [&A, &B](PrimExpr i) { return A[i] + B[i]; }, "elemwise_add"); | ||
|
|
||
| auto fcreate = [=]() { | ||
| With<Target> llvm_scope(target_c); | ||
| return create_schedule({elemwise_add->op}); | ||
| }; | ||
|
|
||
| auto args = Array<Tensor>({A, B, elemwise_add}); | ||
|
|
||
| std::unordered_map<Tensor, Buffer> binds; | ||
| auto lowered = LowerSchedule(fcreate(), args, "elemwise_add", binds); | ||
| Map<tvm::Target, IRModule> inputs = {{target_c, lowered}}; | ||
| runtime::Module module = build(inputs, Target()); | ||
| Array<String> functions = module->GetFunction("get_func_names", false)(); | ||
|
|
||
| ICHECK(functions.back().compare(tvm_module_main) == 0); | ||
| } | ||
|
|
||
| TEST(CCodegen, FunctionOrder) { | ||
| using testing::_; | ||
| using testing::ElementsAre; | ||
| using testing::StrEq; | ||
| using namespace tvm; | ||
| using namespace tvm::te; | ||
|
|
||
| auto target = Target("c -keys=cpu -link-params=0"); | ||
|
|
||
| // The shape of input tensors. | ||
| const int n = 4; | ||
| Array<PrimExpr> shape{n}; | ||
|
|
||
| auto A = placeholder(shape, DataType::Float(32), "A"); | ||
| auto B = placeholder(shape, DataType::Float(32), "B"); | ||
|
|
||
| auto op_1 = compute( | ||
| A->shape, [&A, &B](PrimExpr i) { return A[i] + B[i]; }, "op_1"); | ||
|
|
||
| auto op_2 = compute( | ||
| A->shape, [&A, &B](PrimExpr i) { return A[i] - B[i]; }, "op_2"); | ||
|
|
||
| auto fcreate_s1 = [=]() { | ||
| With<Target> llvm_scope(target); | ||
| return create_schedule({op_1->op}); | ||
| }; | ||
|
|
||
| auto fcreate_s2 = [=]() { | ||
| With<Target> llvm_scope(target); | ||
| return create_schedule({op_2->op}); | ||
| }; | ||
|
|
||
| auto args1 = Array<Tensor>({A, B, op_1}); | ||
| auto args2 = Array<Tensor>({A, B, op_2}); | ||
|
|
||
| std::unordered_map<Tensor, Buffer> binds; | ||
| auto lowered_s1 = LowerSchedule(fcreate_s1(), args1, "op_1", binds); | ||
| auto lowered_s2 = LowerSchedule(fcreate_s2(), args2, "op_2", binds); | ||
|
|
||
| // add schedules in reverse order | ||
| Map<tvm::Target, IRModule> inputs = {{target, lowered_s2}, {target, lowered_s1}}; | ||
mehrdadh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| auto module = build(inputs, Target()); | ||
| Array<String> func_array = module->GetFunction("get_func_names", false)(); | ||
| std::vector<std::string> functions{func_array.begin(), func_array.end()}; | ||
| EXPECT_THAT(functions, ElementsAre(StrEq("op_1"), _, StrEq("op_2"), _)); | ||
| } | ||
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.
i think this change is equivalent
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,
PackedFunc()just matches with the rest of the codebase.