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 custom call op uniform random * Reformat code * Use direct function call instead of CustomInstr
- Loading branch information
Showing
17 changed files
with
502 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) 2021 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" | ||
#include "cinn/frontend/var_type_utils.h" | ||
|
||
namespace cinn { | ||
namespace frontend { | ||
namespace paddle_mappers { | ||
|
||
void UniformRandomOpMapper(const paddle::cpp::OpDesc& op_desc, const OpMapperContext& ctx) { | ||
CHECK_EQ(op_desc.Output("Out").size(), 1UL); | ||
auto out_name = op_desc.Output("Out").front(); | ||
|
||
auto shape_origin = utils::GetAttrOrDefault<std::vector<int64_t>>(op_desc, "shape"); | ||
auto shape = utils::ToShapeType(shape_origin); | ||
|
||
auto min = utils::GetAttrOrDefault<float>(op_desc, "min", -1.0f); | ||
auto max = utils::GetAttrOrDefault<float>(op_desc, "max", 1.0f); | ||
auto seed = utils::GetAttrOrDefault<int>(op_desc, "seed", 0); | ||
|
||
auto dtype_id = utils::GetAttrOrDefault<int>(op_desc, "dtype", static_cast<int>(paddle::cpp::VarDescAPI::Type::FP32)); | ||
auto dtype_pd = static_cast<paddle::cpp::VarDescAPI::Type>(dtype_id); | ||
auto dtype_cinn = utils::CppVarType2CommonType(dtype_pd); | ||
auto dtype = common::Type2Str(dtype_cinn); | ||
|
||
auto out = ctx.Builder()->UniformRandom(shape, min, max, seed, dtype); | ||
ctx.AddVar(out_name, out); | ||
ctx.AddVarModelToProgram(out_name, out->id); | ||
} | ||
|
||
} // namespace paddle_mappers | ||
} // namespace frontend | ||
} // namespace cinn | ||
|
||
CINN_REGISTER_HELPER(paddle_uniform_random) { | ||
CINN_REGISTER_OP_MAPPER(uniform_random, cinn::frontend::paddle_mappers::UniformRandomOpMapper) | ||
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
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,109 @@ | ||
// 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 <gflags/gflags.h> | ||
|
||
#include <memory> | ||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "absl/types/variant.h" | ||
#include "cinn/common/cas.h" | ||
#include "cinn/common/cinn_value.h" | ||
#include "cinn/common/common.h" | ||
#include "cinn/common/context.h" | ||
#include "cinn/common/ir_util.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/op_util.h" | ||
#include "cinn/hlir/pe/elementwise.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 "cinn/lang/packed_func.h" | ||
#include "cinn/poly/stage.h" | ||
#include "glog/logging.h" | ||
|
||
namespace cinn { | ||
namespace hlir { | ||
namespace op { | ||
|
||
using common::CINNValue; | ||
using common::CINNValuePack; | ||
|
||
std::shared_ptr<framework::OpStrategy> StrategyForUniformRandom(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) { | ||
framework::CINNCompute uniform_random_compute([=](lang::Args args, lang::RetValue *ret) { | ||
CHECK(attrs.attr_store.count("shape")); | ||
ir::Tensor shape_tensor; | ||
std::string tensor_name = "uniform_random_out"; | ||
auto out = pe::Identity(shape_tensor, tensor_name).front(); | ||
auto stages = CreateStages({out}); | ||
std::vector<CINNValue> res{CINNValue(out), CINNValue(stages)}; | ||
*ret = CINNValuePack{res}; | ||
}); | ||
auto strategy = std::make_shared<framework::OpStrategy>(); | ||
strategy->AddImpl( | ||
uniform_random_compute, GetInjectiveScheduleFunc(output_shapes, target), "strategy.uniform_random.x86", 1); | ||
return strategy; | ||
} | ||
|
||
std::vector<framework::shape_t> InferShapeForUniformRandom(const std::vector<framework::shape_t> &inputs_shape, | ||
const framework::AttrMapType &attrs) { | ||
CHECK(attrs.count("shape")); | ||
auto shape = absl::get<std::vector<int>>(attrs.at("shape")); | ||
CHECK(!shape.empty()) << "shape attr is empty!"; | ||
return {shape}; | ||
} | ||
|
||
std::vector<Type> InferDtypeForUniformRandom(const std::vector<Type> &inputs_type, | ||
const framework::AttrMapType &attrs) { | ||
std::string dtype = "float32"; | ||
if (attrs.find("dtype") != attrs.end()) { | ||
dtype = absl::get<std::string>(attrs.at("dtype")); | ||
} | ||
std::vector<Type> res{common::Str2Type(dtype)}; | ||
return res; | ||
} | ||
|
||
} // namespace op | ||
} // namespace hlir | ||
} // namespace cinn | ||
|
||
CINN_REGISTER_HELPER(uniform_random_ops) { | ||
CINN_REGISTER_OP(uniform_random) | ||
.describe("UniformRandom") | ||
.set_num_inputs(0) | ||
.set_num_outputs(1) | ||
.set_attr<cinn::hlir::framework::StrategyFunction>("CINNStrategy", cinn::hlir::op::StrategyForUniformRandom) | ||
.set_attr("infershape", MakeOpFunction(cinn::hlir::op::InferShapeForUniformRandom)) | ||
.set_attr("inferdtype", MakeOpFunction(cinn::hlir::op::InferDtypeForUniformRandom)) | ||
.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
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
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.