-
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 tril_triu; fix expand_as (#5507)
- Loading branch information
1 parent
fd8bac9
commit 5f38270
Showing
12 changed files
with
424 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) 2021 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/tril_triu_compute.h" | ||
|
||
namespace paddle { | ||
namespace lite { | ||
namespace kernels { | ||
namespace host { | ||
|
||
template <class T> | ||
void TrilTriu(const T* in, | ||
const int64_t diagonal, | ||
const bool lower, | ||
const int64_t h, | ||
const int64_t w, | ||
T* out) { | ||
int64_t size = h * w; | ||
for (int64_t idx = 0; idx < size; idx++) { | ||
const int64_t row = idx / w; | ||
const int64_t col = idx % w; | ||
const bool mask = lower ? (col - row > diagonal) : (col - row < diagonal); | ||
out[idx] = mask ? 0 : in[idx]; | ||
} | ||
return; | ||
} | ||
|
||
template <class T> | ||
void TrilTriuCompute<T>::Run() { | ||
auto& param = this->template Param<param_t>(); | ||
const lite::Tensor* x = param.x; | ||
lite::Tensor* out = param.out; | ||
int64_t diagonal = param.diagonal; | ||
bool lower = param.lower; | ||
|
||
const T* x_data = x->template data<T>(); | ||
T* out_data = out->template mutable_data<T>(); | ||
auto x_dims = x->dims(); | ||
int64_t h = x_dims[x_dims.size() - 2]; | ||
int64_t w = x_dims[x_dims.size() - 1]; | ||
int64_t n = x_dims.production() / h / w; | ||
|
||
for (int64_t i = 0; i < n; i++) { | ||
TrilTriu(x_data, diagonal, lower, h, w, out_data); | ||
x_data += h * w; | ||
out_data += h * w; | ||
} | ||
return; | ||
} | ||
|
||
} // namespace host | ||
} // namespace kernels | ||
} // namespace lite | ||
} // namespace paddle | ||
|
||
using TrilTriuFloat32 = paddle::lite::kernels::host::TrilTriuCompute<float>; | ||
REGISTER_LITE_KERNEL(tril_triu, kHost, kAny, kNCHW, TrilTriuFloat32, float32) | ||
.BindInput("X", {LiteType::GetTensorTy(TARGET(kHost), PRECISION(kFloat))}) | ||
.BindOutput("Out", | ||
{LiteType::GetTensorTy(TARGET(kHost), PRECISION(kFloat))}) | ||
.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,37 @@ | ||
// Copyright (c) 2021 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 { | ||
|
||
template <class T> | ||
class TrilTriuCompute : public KernelLite<TARGET(kHost), PRECISION(kAny)> { | ||
public: | ||
using param_t = operators::TrilTriuParam; | ||
|
||
void Run() override; | ||
|
||
virtual ~TrilTriuCompute() = 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
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) 2021 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/tril_triu_op.h" | ||
#include "lite/core/op_registry.h" | ||
|
||
namespace paddle { | ||
namespace lite { | ||
namespace operators { | ||
|
||
bool TrilTriuOp::CheckShape() const { | ||
CHECK(param_.x); | ||
CHECK(param_.out); | ||
return true; | ||
} | ||
|
||
bool TrilTriuOp::InferShapeImpl() const { | ||
CHECK_GE(param_.x->dims().size(), 2UL); | ||
param_.out->Resize(param_.x->dims()); | ||
param_.out->set_lod(param_.x->lod()); | ||
return true; | ||
} | ||
|
||
bool TrilTriuOp::AttachImpl(const cpp::OpDesc &op_desc, lite::Scope *scope) { | ||
param_.x = scope->FindTensor(op_desc.Input("X").front()); | ||
param_.out = scope->FindMutableTensor(op_desc.Output("Out").front()); | ||
|
||
param_.diagonal = op_desc.GetAttr<int>("diagonal"); | ||
param_.lower = op_desc.GetAttr<bool>("lower"); | ||
return true; | ||
} | ||
|
||
} // namespace operators | ||
} // namespace lite | ||
} // namespace paddle | ||
|
||
REGISTER_LITE_OP(tril_triu, paddle::lite::operators::TrilTriuOp); |
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,45 @@ | ||
// Copyright (c) 2021 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 "lite/core/op_lite.h" | ||
#include "lite/core/scope.h" | ||
#include "lite/utils/all.h" | ||
|
||
namespace paddle { | ||
namespace lite { | ||
namespace operators { | ||
|
||
class TrilTriuOp : public OpLite { | ||
public: | ||
TrilTriuOp() {} | ||
explicit TrilTriuOp(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 "tril_triu"; } | ||
|
||
private: | ||
mutable TrilTriuParam 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.