-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cherry-pick kunlun PR: 29458, 29539 (#29583)
* support mobilenet for kunlun (#29458) * add xpu ops for training transformer in kunlun (#29539) * 1.fix matmul bug 2. add one hot * add xpu error msg Co-authored-by: procr <[email protected]> Co-authored-by: taixiurong <[email protected]>
- Loading branch information
1 parent
d82d59e
commit 03ddf69
Showing
13 changed files
with
908 additions
and
73 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,170 @@ | ||
/* Copyright (c) 2016 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 | ||
#ifdef PADDLE_WITH_XPU | ||
#include <algorithm> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "paddle/fluid/framework/op_registry.h" | ||
#include "xpu/refactor/math.h" | ||
|
||
namespace paddle { | ||
|
||
namespace operators { | ||
typedef enum { XPU_OR, XPU_AND } XpuLogicalType; | ||
|
||
std::string XpuLogicalType2Str(XpuLogicalType ty) { | ||
switch (ty) { | ||
case XpuLogicalType::XPU_OR: | ||
return std::string("logical or"); | ||
case XpuLogicalType::XPU_AND: | ||
return std::string("logical and"); | ||
default: | ||
return std::string("unknown type"); | ||
} | ||
return std::string("unknown"); | ||
} | ||
|
||
template <XpuLogicalType xpu_type, typename T> | ||
class BinaryLogicalOpXPUKernel : public framework::OpKernel<T> { | ||
public: | ||
void Compute(const framework::ExecutionContext& context) const override { | ||
auto* x = context.Input<framework::Tensor>("X"); | ||
auto* y = context.Input<framework::Tensor>("Y"); | ||
auto* out = context.Output<framework::Tensor>("Out"); | ||
T* out_ptr = out->mutable_data<T>(context.GetPlace()); | ||
const T* x_ptr = x->data<T>(); | ||
const T* y_ptr = y->data<T>(); | ||
auto& dev_ctx = | ||
context.template device_context<paddle::platform::XPUDeviceContext>(); | ||
framework::Tensor broadcast_x; | ||
framework::Tensor broadcast_y; | ||
bool need_broad_cast = false; | ||
if (x->numel() != out->numel()) { | ||
// x need broadcast | ||
T* broadcast_x_ptr = | ||
broadcast_x.mutable_data<T>(context.GetPlace(), out->numel()); | ||
auto& out_dim = out->dims(); | ||
auto& x_dim = x->dims(); | ||
int dims = out_dim.size(); | ||
std::vector<int> bcast_xdims; | ||
std::vector<int> bcast_ydims; | ||
for (int i = 0; i < dims; ++i) { | ||
if (out_dim[i] == x_dim[i]) { | ||
bcast_xdims.push_back(x_dim[i]); | ||
bcast_ydims.push_back(x_dim[i]); | ||
continue; | ||
} | ||
bcast_xdims.push_back(1); | ||
bcast_xdims.push_back(x_dim[i]); | ||
bcast_ydims.push_back(out_dim[i] / x_dim[i]); | ||
bcast_ydims.push_back(x_dim[i]); | ||
} | ||
|
||
int ret = xpu::broadcast<int8_t>( | ||
dev_ctx.x_context(), reinterpret_cast<const int8_t*> x_ptr, | ||
reinterpret_cast<int8_t*> broadcast_x_ptr, bcast_xdims, bcast_ydims); | ||
PADDLE_ENFORCE_EQ(ret, XPU_SUCCESS, | ||
platform::errors::External( | ||
"XPU broadcast kernel return wrong value[%d %s]", | ||
ret, XPUAPIErrorMsg[ret])); | ||
x_ptr = (const T*)broadcast_x_ptr; | ||
need_broad_cast = true; | ||
} | ||
if (y->numel() != out->numel()) { | ||
// y need broadcast | ||
T* broadcast_y_ptr = | ||
broadcast_y.mutable_data<T>(context.GetPlace(), out->numel()); | ||
auto& out_dim = out->dims(); | ||
auto& y_dim = y->dims(); | ||
int dims = out_dim.size(); | ||
std::vector<int> bcast_xdims; | ||
std::vector<int> bcast_ydims; | ||
for (int i = 0; i < dims; ++i) { | ||
if (out_dim[i] == y_dim[i]) { | ||
bcast_xdims.push_back(y_dim[i]); | ||
bcast_ydims.push_back(y_dim[i]); | ||
continue; | ||
} | ||
bcast_xdims.push_back(1); | ||
bcast_xdims.push_back(y_dim[i]); | ||
bcast_ydims.push_back(out_dim[i] / y_dim[i]); | ||
bcast_ydims.push_back(y_dim[i]); | ||
} | ||
|
||
int ret = xpu::broadcast<int8_t>( | ||
dev_ctx.x_context(), reinterpret_cast<const int8_t*> y_ptr, | ||
reinterpret_cast<int8_t*> broadcast_y_ptr, bcast_xdims, bcast_ydims); | ||
PADDLE_ENFORCE_EQ(ret, XPU_SUCCESS, | ||
platform::errors::External( | ||
"XPU broadcast kernel return wrong value[%d %s]", | ||
ret, XPUAPIErrorMsg[ret])); | ||
y_ptr = (const T*)broadcast_y_ptr; | ||
need_broad_cast = true; | ||
} | ||
|
||
// logical kernel | ||
int ret = XPU_SUCCESS; | ||
switch (xpu_type) { | ||
case XpuLogicalType::XPU_OR: | ||
ret = xpu::logical_or<bool>(dev_ctx.x_context(), x_ptr, y_ptr, out_ptr, | ||
out->numel()); | ||
break; | ||
case XpuLogicalType::XPU_AND: | ||
ret = xpu::logical_and<bool>(dev_ctx.x_context(), x_ptr, y_ptr, out_ptr, | ||
out->numel()); | ||
default: | ||
LOG(ERROR) << "xpu not support logical xpu type = " | ||
<< XpuLogicalType2Str(xpu_type); | ||
break; | ||
} | ||
PADDLE_ENFORCE_EQ( | ||
ret, XPU_SUCCESS, | ||
platform::errors::External("XPU API return wrong value[%d %s] in " | ||
"op_name[%s].", | ||
ret, XPUAPIErrorMsg[ret], | ||
XpuLogicalType2Str(xpu_type))); | ||
|
||
if (need_broad_cast && dev_ctx.x_context()->xpu_stream != nullptr) { | ||
xpu_wait(); | ||
} | ||
} | ||
}; | ||
|
||
template <typename T> | ||
class UnaryLogicalOpXPUKernel : public framework::OpKernel<T> { | ||
public: | ||
void Compute(const framework::ExecutionContext& context) const override { | ||
auto* x = context.Input<framework::Tensor>("X"); | ||
auto* out = context.Output<framework::Tensor>("Out"); | ||
if (x->numel() == 0) { | ||
return; | ||
} | ||
out->mutable_data<T>(context.GetPlace()); | ||
auto& dev_ctx = | ||
context.template device_context<paddle::platform::XPUDeviceContext>(); | ||
int ret = xpu::logical_not<bool>(dev_ctx.x_context(), x->data<T>(), | ||
out->data<T>(), x->numel()); | ||
PADDLE_ENFORCE_EQ( | ||
ret, XPU_SUCCESS, | ||
platform::errors::External("XPU API return wrong value[%d %s].", ret, | ||
XPUAPIErrorMsg[ret])); | ||
} | ||
}; | ||
|
||
} // namespace operators | ||
} // namespace paddle | ||
#endif |
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,21 @@ | ||
/* Copyright (c) 2016 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. */ | ||
|
||
#ifdef PADDLE_WITH_XPU | ||
#include "paddle/fluid/operators/controlflow/logical_op_xpu.h" | ||
namespace ops = paddle::operators; | ||
REGISTER_OP_XPU_KERNEL( | ||
logical_and, | ||
ops::BinaryLogicalOpXPUKernel<ops::XpuLogicalType::XPU_AND, bool>); | ||
#endif |
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,19 @@ | ||
/* Copyright (c) 2016 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. */ | ||
|
||
#ifdef PADDLE_WITH_XPU | ||
#include "paddle/fluid/operators/controlflow/logical_op_xpu.h" | ||
namespace ops = paddle::operators; | ||
REGISTER_OP_XPU_KERNEL(logicalnot, ops::UnaryLogicalOpXPUKernel<bool>); | ||
#endif |
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,22 @@ | ||
/* Copyright (c) 2016 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. */ | ||
|
||
#ifdef PADDLE_WITH_XPU | ||
#include "paddle/fluid/operators/controlflow/logical_op_xpu.h" | ||
|
||
namespace ops = paddle::operators; | ||
REGISTER_OP_XPU_KERNEL( | ||
logical_or, | ||
ops::BinaryLogicalOpXPUKernel<ops::XpuLogicalType::XPU_OR, bool>); | ||
#endif |
Oops, something went wrong.