-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Host] Add empty op and ceil op (#10092)
- Loading branch information
1 parent
1f8b83e
commit 44ae965
Showing
11 changed files
with
340 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) 2023 PaddlePaddle 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 "lite/kernels/host/empty_compute.h" | ||
|
||
namespace paddle { | ||
namespace lite { | ||
namespace kernels { | ||
namespace host { | ||
|
||
void EmptyCompute::Run() { | ||
auto& param = *param_.get_mutable<param_t>(); | ||
auto output = param.Out; | ||
auto output_dims = output->dims(); | ||
if (param.dtype == static_cast<int32_t>(lite::core::FluidType::BOOL)) { | ||
output->set_precision(PRECISION(kBool)); | ||
output->template mutable_data<bool>(); | ||
} else if (param.dtype == static_cast<int32_t>(lite::core::FluidType::FP32)) { | ||
output->set_precision(PRECISION(kFloat)); | ||
output->template mutable_data<float>(); | ||
} else if (param.dtype == | ||
static_cast<int32_t>(lite::core::FluidType::INT32)) { | ||
output->set_precision(PRECISION(kInt32)); | ||
output->template mutable_data<int32_t>(); | ||
} else if (param.dtype == | ||
static_cast<int32_t>(lite::core::FluidType::INT64)) { | ||
output->set_precision(PRECISION(kInt64)); | ||
output->template mutable_data<int64_t>(); | ||
} else { | ||
output->set_precision(PRECISION(kInt32)); | ||
output->template mutable_data<int32_t>(); | ||
} | ||
|
||
return; | ||
} | ||
|
||
} // namespace host | ||
} // namespace kernels | ||
} // namespace lite | ||
} // namespace paddle | ||
|
||
REGISTER_LITE_KERNEL( | ||
empty, kHost, kAny, kNCHW, paddle::lite::kernels::host::EmptyCompute, def) | ||
.BindInput("ShapeTensor", | ||
{LiteType::GetTensorTy(TARGET(kHost), PRECISION(kAny))}) | ||
.BindInput("ShapeTensorList", | ||
{LiteType::GetTensorTy(TARGET(kHost), PRECISION(kAny))}) | ||
.BindOutput("Out", {LiteType::GetTensorTy(TARGET(kHost), PRECISION(kAny))}) | ||
.Finalize(); |
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,36 @@ | ||
// Copyright (c) 2023 PaddlePaddle 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 "lite/core/kernel.h" | ||
#include "lite/core/op_registry.h" | ||
|
||
namespace paddle { | ||
namespace lite { | ||
namespace kernels { | ||
namespace host { | ||
|
||
class EmptyCompute : public KernelLite<TARGET(kHost), PRECISION(kAny)> { | ||
public: | ||
using param_t = operators::EmptyParam; | ||
|
||
void Run() override; | ||
|
||
virtual ~EmptyCompute() = default; | ||
}; | ||
|
||
} // namespace host | ||
} // namespace kernels | ||
} // namespace lite | ||
} // namespace paddle |
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,89 @@ | ||
// Copyright (c) 2023 PaddlePaddle 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 "lite/operators/empty_op.h" | ||
#include "lite/core/op_registry.h" | ||
|
||
namespace paddle { | ||
namespace lite { | ||
namespace operators { | ||
|
||
bool EmptyOp::CheckShape() const { | ||
CHECK_OR_FALSE(param_.Out); | ||
return true; | ||
} | ||
|
||
bool EmptyOp::InferShapeImpl() const { | ||
std::vector<int64_t> OutShape; | ||
auto ShapeTensor = param_.ShapeTensor; | ||
auto ShapeTensorList = param_.ShapeTensorList; | ||
if (ShapeTensor != nullptr) { | ||
auto ShapeTensorData = ShapeTensor->data<int>(); | ||
for (int i = 0; i < ShapeTensor->numel(); i++) { | ||
OutShape.push_back(ShapeTensorData[i]); | ||
} | ||
} else if (!ShapeTensorList.empty()) { | ||
for (size_t i = 0; i < ShapeTensorList.size(); i++) { | ||
OutShape.push_back(ShapeTensorList[i]->data<int>()[0]); | ||
} | ||
} else if (!param_.shape.empty()) { | ||
OutShape = param_.shape; | ||
} else { | ||
LOG(FATAL) << "no valid out_shape. Must set one of shape_tensor, or " | ||
"shape_tensor_list, or shape."; | ||
} | ||
|
||
param_.Out->Resize(OutShape); | ||
return true; | ||
} | ||
|
||
bool EmptyOp::AttachImpl(const cpp::OpDesc& opdesc, lite::Scope* scope) { | ||
if (opdesc.HasInput("ShapeTensor") && !opdesc.Input("ShapeTensor").empty()) { | ||
param_.ShapeTensor = | ||
scope->FindMutableTensor(opdesc.Input("ShapeTensor").front()); | ||
} | ||
param_.ShapeTensorList.clear(); | ||
if (opdesc.HasInput("ShapeTensorList") && | ||
!opdesc.Input("ShapeTensorList").empty()) { | ||
for (auto name : opdesc.Input("ShapeTensorList")) { | ||
param_.ShapeTensorList.push_back( | ||
GetMutableVar<lite::Tensor>(scope, name)); | ||
} | ||
} | ||
if (opdesc.HasAttr("shape")) { | ||
auto type = opdesc.GetAttrType("shape"); | ||
if (type == OpAttrType::INTS) { // paddle1.0 shape type is ints | ||
auto shape = opdesc.GetAttr<std::vector<int32_t>>("shape"); | ||
param_.shape.resize(shape.size()); | ||
for (int i = 0; i < shape.size(); i++) { | ||
param_.shape[i] = shape[i]; | ||
} | ||
} else { | ||
param_.shape = opdesc.GetAttr<std::vector<int64_t>>("shape"); | ||
} | ||
} | ||
param_.Out = scope->FindMutableTensor(opdesc.Output("Out").front()); | ||
CHECK(param_.Out) << "Output(Out) of EmptyOp should not be null."; | ||
if (opdesc.HasAttr("dtype")) { | ||
param_.dtype = opdesc.GetAttr<int>("dtype"); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
} // namespace operators | ||
} // namespace lite | ||
} // namespace paddle | ||
|
||
REGISTER_LITE_OP(empty, paddle::lite::operators::EmptyOp); |
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,44 @@ | ||
// Copyright (c) 2023 PaddlePaddle 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 "lite/core/op_lite.h" | ||
|
||
namespace paddle { | ||
namespace lite { | ||
namespace operators { | ||
|
||
class EmptyOp : public OpLite { | ||
public: | ||
EmptyOp() {} | ||
explicit EmptyOp(const std::string &op_type) : OpLite(op_type) {} | ||
|
||
bool CheckShape() const override; | ||
|
||
bool InferShapeImpl() const override; | ||
|
||
bool AttachImpl(const cpp::OpDesc &opdesc, lite::Scope *scope) override; | ||
|
||
void AttachKernel(KernelBase *kernel) override { kernel->SetParam(param_); } | ||
std::string DebugString() const override { return "empty"; } | ||
|
||
protected: | ||
mutable EmptyParam param_; | ||
}; | ||
|
||
} // namespace operators | ||
} // namespace lite | ||
} // namespace paddle |
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.