This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix compute and schedule func of sort and argsoft
- Loading branch information
1 parent
a9630cc
commit b3d1a47
Showing
17 changed files
with
524 additions
and
53 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
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,51 @@ | ||
// Copyright (c) 2023 CINN Authors. All Rights Reserved. | ||
// | ||
// Licensed 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 "cinn/frontend/decomposer_registry.h" | ||
#include "cinn/frontend/syntax.h" | ||
|
||
namespace cinn { | ||
namespace frontend { | ||
namespace decomposer { | ||
|
||
void top_k(const Instruction& instr, const DecomposerContext& context) { | ||
CHECK_EQ(instr->inputs.size(), 1UL) << " 1 input tensor for " << instr->op_type; | ||
CHECK_EQ(instr->outputs.size(), 2UL) << "2 output tensors for " << instr->op_type; | ||
auto x = instr->inputs[0]; | ||
auto output = instr->outputs[0]; | ||
auto indices = instr->outputs[1]; | ||
|
||
auto* builder = context.builder(); | ||
int k = instr.GetAttrs<int>("k"); | ||
CHECK_GT(k, 0) << "The attribute k must be greater than 0."; | ||
int axis = instr.GetAttrs<int>("axis"); | ||
|
||
auto sort_tmp = builder->Sort(x, axis, false); | ||
auto sort_out = builder->Slice(sort_tmp, {axis}, {0}, {k}); | ||
auto argsort_tmp = builder->ArgSort(x, axis, false); | ||
auto argsort_out = builder->Cast(builder->Slice(argsort_tmp, {axis}, {0}, {k}), "int64"); | ||
|
||
// map the the output of decomposed operator to the original. | ||
context.MapOutToOrigin(sort_out, output); | ||
context.MapOutToOrigin(argsort_out, indices); | ||
} | ||
|
||
} // namespace decomposer | ||
} // namespace frontend | ||
} // namespace cinn | ||
|
||
CINN_REGISTER_HELPER(top_k_decomposer) { | ||
CINN_DECOMPOSER_REGISTER(top_k, cinn::frontend::decomposer::top_k); | ||
return true; | ||
} |
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,55 @@ | ||
// Copyright (c) 2023 CINN Authors. All Rights Reserved. | ||
// | ||
// Licensed 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 <gtest/gtest.h> | ||
|
||
#include "cinn/frontend/decomposer/test_helper.h" | ||
|
||
namespace cinn::frontend { | ||
|
||
TEST(Decomposer, top_k_decomposer) { | ||
NetBuilder net_builder("top_k_decomposer"); | ||
std::unordered_set<std::string> output_names; | ||
{ | ||
auto x = net_builder.CreateInput(Float(32), {10, 5}, "x"); | ||
auto y = net_builder.TopK(x, 1, -1, true); | ||
output_names.insert(y[0]->id); | ||
output_names.insert(y[1]->id); | ||
} | ||
auto program = net_builder.Build(); | ||
|
||
auto target = common::DefaultTarget(); | ||
RunDecomposer(&program, target); | ||
|
||
auto graph = std::make_shared<hlir::framework::Graph>(program, output_names, target); | ||
hlir::framework::ApplyPass(graph.get(), "OpFusionPass"); | ||
hlir::framework::ApplyPass(graph.get(), "FusionMergePass"); | ||
|
||
auto scope = BuildScope(target, graph); | ||
hlir::framework::GraphCompiler gc(target, scope, graph); | ||
auto run_program = gc.Build(); | ||
|
||
std::vector<float> x(10 * 5); | ||
InitRandomVector<float>(&x, 10 * 5, 0.0f, 1.0f, 1e-3); | ||
std::vector<std::pair<std::string, std::vector<float>>> inputs = {{"x", x}}; | ||
for (auto& input : inputs) { | ||
scope->Var<hlir::framework::Tensor>(input.first); | ||
auto tensor = scope->GetTensor(input.first); | ||
auto* data = tensor->mutable_data<float>(target); | ||
CopyFromVector(input.second, tensor, target); | ||
} | ||
run_program->Execute(); | ||
} | ||
|
||
} // namespace cinn::frontend |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) 2023 CINN Authors. All Rights Reserved. | ||
// | ||
// Licensed 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 "cinn/frontend/op_mapper_registry.h" | ||
#include "cinn/frontend/op_mappers/common_utils.h" | ||
|
||
namespace cinn { | ||
namespace frontend { | ||
namespace paddle_mappers { | ||
|
||
void TopKOpMapper(const paddle::cpp::OpDesc& op_desc, const OpMapperContext& ctx) { | ||
CHECK_EQ(op_desc.Input("X").size(), 1UL); | ||
auto x_name = op_desc.Input("X").front(); | ||
CHECK_EQ(op_desc.Output("Out").size(), 1UL); | ||
auto out_name = op_desc.Output("Out").front(); | ||
CHECK_EQ(op_desc.Output("Indices").size(), 1UL); | ||
auto indices_name = op_desc.Output("Indices").front(); | ||
auto x = ctx.GetVar(x_name); | ||
|
||
CHECK(op_desc.HasAttr("k")); | ||
auto k = utils::GetAttrOrDefault<int>(op_desc, "k"); | ||
auto outs = ctx.Builder()->TopK(x, k, -1, true); | ||
|
||
ctx.AddVar(out_name, outs[0]); | ||
ctx.AddVarModelToProgram(out_name, outs[0]->id); | ||
ctx.AddVar(indices_name, outs[1]); | ||
ctx.AddVarModelToProgram(indices_name, outs[1]->id); | ||
} | ||
|
||
} // namespace paddle_mappers | ||
} // namespace frontend | ||
} // namespace cinn | ||
|
||
CINN_REGISTER_HELPER(paddle_top_k) { | ||
CINN_REGISTER_OP_MAPPER(top_k, cinn::frontend::paddle_mappers::TopKOpMapper) | ||
return true; | ||
} |
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.