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.
* add clz op * fix data type * fix datatype
- Loading branch information
Showing
14 changed files
with
425 additions
and
2 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
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,143 @@ | ||
// Copyright (c) 2022 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 <memory> | ||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "cinn/common/cas.h" | ||
#include "cinn/common/common.h" | ||
#include "cinn/common/context.h" | ||
#include "cinn/common/macros.h" | ||
#include "cinn/common/target.h" | ||
#include "cinn/hlir/framework/node.h" | ||
#include "cinn/hlir/framework/op.h" | ||
#include "cinn/hlir/framework/op_strategy.h" | ||
#include "cinn/hlir/op/contrib/clip.h" | ||
#include "cinn/hlir/pe/ir_schedule_pe.h" | ||
#include "cinn/hlir/pe/nn.h" | ||
#include "cinn/hlir/pe/schedule.h" | ||
#include "cinn/ir/ir.h" | ||
#include "cinn/ir/ir_base.h" | ||
#include "cinn/ir/ir_operators.h" | ||
#include "cinn/ir/tensor.h" | ||
#include "cinn/lang/builtin.h" | ||
#include "cinn/lang/compute.h" | ||
#include "gflags/gflags.h" | ||
|
||
DECLARE_bool(cinn_ir_schedule); | ||
|
||
namespace cinn { | ||
namespace hlir { | ||
namespace op { | ||
|
||
using common::_CINNValuePack_; | ||
using common::CINNValue; | ||
using common::CINNValuePack; | ||
using framework::OpStrategy; | ||
using framework::shape_t; | ||
using framework::StrategyFunction; | ||
|
||
ir::Tensor Clz(const ir::Tensor &input, const Target &target, const std::string &output_name) { | ||
std::string extern_func = "cinn_"; | ||
if (target == common::DefaultHostTarget()) { | ||
extern_func += "host_"; | ||
} else if (target == common::DefaultNVGPUTarget()) { | ||
extern_func += "nvgpu_"; | ||
} else { | ||
CINN_NOT_IMPLEMENTED | ||
} | ||
|
||
extern_func += "clz"; | ||
|
||
if (input->type().is_int(32) || input->type().is_uint(32)) { | ||
extern_func += "_int32"; | ||
} else if (input->type().is_int(64) || input->type().is_uint(64)) { | ||
extern_func += "_int64"; | ||
} else { | ||
CINN_NOT_IMPLEMENTED | ||
} | ||
|
||
return Compute( | ||
input->shape, | ||
[=](const std::vector<Expr> &indices) { | ||
Expr e = input(indices); | ||
return lang::CallExtern(extern_func, {e}); | ||
}, | ||
output_name); | ||
} | ||
|
||
std::shared_ptr<OpStrategy> StrategyForClz(const framework::NodeAttr &attrs, | ||
const std::vector<ir::Tensor> &inputs, | ||
const std::vector<Type> &out_type, | ||
const std::vector<std::vector<int>> &output_shapes, | ||
const Target &target) { | ||
std::string op_name("clz"); | ||
|
||
framework::CINNCompute clz_compute([=](lang::Args args, lang::RetValue *ret) { | ||
CHECK(!args.empty()) << "The input argument of " << op_name << " compute is empty! Please check.\n"; | ||
CINNValuePack pack_args = args[0]; | ||
CHECK(!pack_args.empty()) << "at least one input tensor for " << op_name << " compute\n"; | ||
|
||
std::string tensor_name = UniqName(op_name + "_Out"); | ||
if (FLAGS_cinn_ir_schedule) { | ||
CHECK_EQ(pack_args.size(), 2); | ||
CHECK(pack_args[1].is_string()); | ||
tensor_name = pack_args[1].operator std::string(); | ||
} | ||
|
||
Expr A_expr = pack_args[0]; | ||
CHECK(A_expr.as_tensor()); | ||
ir::Tensor A = A_expr.as_tensor_ref(); | ||
auto out = Clz(A, target, tensor_name); | ||
auto stages = CreateStages({out}); | ||
*ret = CINNValuePack{{CINNValue(Expr(out.get())), CINNValue(stages)}}; | ||
}); | ||
|
||
auto strategy = std::make_shared<framework::OpStrategy>(); | ||
strategy->AddImpl(clz_compute, framework::GetInjectiveScheduleFunc(output_shapes, target), "strategy.clz.x86", 1); | ||
return strategy; | ||
} | ||
|
||
std::vector<framework::shape_t> InferShapeForClz(const std::vector<framework::shape_t> &inputs_shape, | ||
const framework::AttrMapType &attrs) { | ||
CHECK(!inputs_shape.empty() && !inputs_shape[0].empty()) << "The input's shape size is 0! Please check again."; | ||
std::vector<framework::shape_t> res{inputs_shape[0]}; | ||
return res; | ||
} | ||
|
||
std::vector<Type> InferDtypeForClz(const std::vector<Type> &inputs_type, const framework::AttrMapType &attrs) { | ||
CHECK(!inputs_type.empty()) << "The input's type size is 0! Please check again."; | ||
std::vector<Type> res{inputs_type[0]}; | ||
return res; | ||
} | ||
|
||
} // namespace op | ||
} // namespace hlir | ||
} // namespace cinn | ||
|
||
CINN_REGISTER_HELPER(clz_ops) { | ||
CINN_REGISTER_OP(clz) | ||
.describe("Counting Leading Zeros.") | ||
.set_num_inputs(1) | ||
.set_num_outputs(1) | ||
.set_attr<cinn::hlir::framework::StrategyFunction>("CINNStrategy", cinn::hlir::op::StrategyForClz) | ||
.set_attr("infershape", MakeOpFunction(cinn::hlir::op::InferShapeForClz)) | ||
.set_attr("inferdtype", MakeOpFunction(cinn::hlir::op::InferDtypeForClz)) | ||
.set_attr<cinn::hlir::framework::OpPatternKind>("OpPattern", cinn::hlir::framework::OpPatternKind::kElementWise) | ||
.set_support_level(4); | ||
|
||
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,32 @@ | ||
// Copyright (c) 2022 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. | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include "cinn/ir/ir.h" | ||
#include "cinn/ir/ir_base.h" | ||
#include "cinn/ir/tensor.h" | ||
|
||
namespace cinn { | ||
namespace hlir { | ||
namespace op { | ||
|
||
ir::Tensor Clz(const ir::Tensor& input, const Target& target, const std::string& output_name = "T_Clz_out"); | ||
|
||
} // namespace op | ||
} // namespace hlir | ||
} // namespace cinn |
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,99 @@ | ||
// Copyright (c) 2022 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/hlir/op/contrib/clz.h" | ||
|
||
#include <glog/logging.h> | ||
#include <gtest/gtest.h> | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include "cinn/backends/codegen_c.h" | ||
#include "cinn/backends/codegen_c_x86.h" | ||
#include "cinn/backends/codegen_cuda_dev.h" | ||
#include "cinn/common/context.h" | ||
#include "cinn/lang/lower.h" | ||
#include "cinn/lang/placeholder.h" | ||
#include "cinn/poly/stage.h" | ||
|
||
namespace cinn { | ||
namespace hlir { | ||
namespace op { | ||
namespace { | ||
bool IsCompiledWithCUDA() { | ||
#if !defined(CINN_WITH_CUDA) | ||
return false; | ||
#else | ||
return true; | ||
#endif | ||
} | ||
} // namespace | ||
|
||
TEST(GenerateCode_Cpu, Clz) { | ||
common::Context::Global().ResetNameId(); | ||
|
||
common::Target target = common::DefaultHostTarget(); | ||
lang::Placeholder<int> in("in", std::vector<int>{10}); | ||
ir::Tensor res = Clz(in, target, "test_clz"); | ||
|
||
poly::StageMap stages = poly::CreateStages({res}); | ||
std::vector<ir::LoweredFunc> funcs = | ||
lang::LowerVec("TestGenerateCodeCpu_Clz", stages, {res}, {}, {}, nullptr, target, true); | ||
|
||
VLOG(6) << "Expr before CPU codegen:"; | ||
VLOG(6) << funcs[0]->body; | ||
|
||
ir::Module::Builder builder("Clz_Module", target); | ||
for (auto& f : funcs) { | ||
builder.AddFunction(f); | ||
} | ||
|
||
backends::CodeGenCX86 codegen(target, backends::CodeGenCX86::Feature::AVX512); | ||
codegen.SetInlineBuiltinCodes(false); | ||
std::string code = codegen.Compile(builder.Build(), backends::CodeGenC::OutputKind::CImpl); | ||
VLOG(6) << "Cpu Codegen result:"; | ||
VLOG(6) << code; | ||
} | ||
|
||
TEST(GenerateCode_Cuda, Clz) { | ||
if (!IsCompiledWithCUDA()) { | ||
return; | ||
} | ||
common::Context::Global().ResetNameId(); | ||
|
||
common::Target target = common::DefaultNVGPUTarget(); | ||
|
||
lang::Placeholder<int64_t> in("in", std::vector<int>{10}); | ||
ir::Tensor res = Clz(in, target, "test_clz"); | ||
|
||
poly::StageMap stages = poly::CreateStages({res}); | ||
stages[res]->Bind(0, "blockIdx.x"); | ||
stages[res]->SetBuffer("global"); | ||
|
||
std::vector<ir::LoweredFunc> funcs = | ||
lang::LowerVec("TestGenerateCodeCuda_Clz", stages, {res}, {}, {}, nullptr, target, true); | ||
|
||
VLOG(6) << "Expr before CUDA codegen:"; | ||
VLOG(6) << funcs[0]->body; | ||
|
||
ir::Module::Builder builder("Clz_Module", target); | ||
for (auto& f : funcs) { | ||
builder.AddFunction(f); | ||
} | ||
} | ||
|
||
} // namespace op | ||
} // namespace hlir | ||
} // namespace cinn |
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.