|
| 1 | +/* Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#ifdef PADDLE_WITH_XPU |
| 16 | +#include "paddle/fluid/operators/amp/check_finite_and_unscale_op.h" |
| 17 | +#include "paddle/fluid/operators/amp/fp16_type_traits.h" |
| 18 | +#include "paddle/fluid/platform/float16.h" |
| 19 | +namespace paddle { |
| 20 | +namespace operators { |
| 21 | +template <typename T> |
| 22 | +class CheckFiniteAndUnscaleXPUKernel : public framework::OpKernel<T> { |
| 23 | + using MPDType = typename details::MPTypeTrait<T>::Type; |
| 24 | + using XPUTyp = typename XPUTypeTrait<T>::Type; |
| 25 | + |
| 26 | + public: |
| 27 | + void Compute(const framework::ExecutionContext& ctx) const { |
| 28 | + auto& dev_ctx = ctx.template device_context<platform::XPUDeviceContext>(); |
| 29 | + const auto xs = ctx.MultiInput<framework::Tensor>("X"); |
| 30 | + const auto* scale = ctx.Input<framework::Tensor>("Scale"); |
| 31 | + auto outs = ctx.MultiOutput<framework::Tensor>("Out"); |
| 32 | + auto* found_inf = ctx.Output<framework::Tensor>("FoundInfinite"); |
| 33 | + |
| 34 | + const MPDType* scale_data = scale->data<MPDType>(); |
| 35 | + bool* found_inf_data = found_inf->mutable_data<bool>(dev_ctx.GetPlace()); |
| 36 | + |
| 37 | + // cpy to cpu |
| 38 | + bool cpu_found_inf_data = false; |
| 39 | + |
| 40 | + MPDType cpu_scale_data; |
| 41 | + if (platform::is_xpu_place(scale->place())) { |
| 42 | + xpu_memcpy(&cpu_scale_data, scale_data, sizeof(MPDType), |
| 43 | + XPUMemcpyKind::XPU_DEVICE_TO_HOST); |
| 44 | + } else { |
| 45 | + cpu_scale_data = (*scale_data); |
| 46 | + } |
| 47 | + MPDType inverse_scale = 1.0 / cpu_scale_data; |
| 48 | + for (size_t i = 0; i < xs.size(); ++i) { |
| 49 | + const auto* x = xs[i]; |
| 50 | + auto* out = outs[i]; |
| 51 | + out->mutable_data<T>(dev_ctx.GetPlace()); |
| 52 | + framework::Tensor is_finite = |
| 53 | + ctx.AllocateTmpTensor<bool, platform::XPUDeviceContext>(x->dims(), |
| 54 | + dev_ctx); |
| 55 | + framework::Tensor is_nan = |
| 56 | + ctx.AllocateTmpTensor<bool, platform::XPUDeviceContext>(x->dims(), |
| 57 | + dev_ctx); |
| 58 | + framework::Tensor is_finite_and_nan = |
| 59 | + ctx.AllocateTmpTensor<bool, platform::XPUDeviceContext>(x->dims(), |
| 60 | + dev_ctx); |
| 61 | + if (cpu_found_inf_data == false) { |
| 62 | + int r = xpu::isfinite(dev_ctx.x_context(), |
| 63 | + reinterpret_cast<const XPUTyp*>(x->data<T>()), |
| 64 | + is_finite.data<bool>(), x->numel()); |
| 65 | + PADDLE_ENFORCE_EQ(r, XPU_SUCCESS, platform::errors::External( |
| 66 | + "XPU API(isfinite) return wrong " |
| 67 | + "value[%d %s]", |
| 68 | + r, XPUAPIErrorMsg[r])); |
| 69 | + r = xpu::logical_not(dev_ctx.x_context(), reinterpret_cast<const bool*>( |
| 70 | + is_finite.data<bool>()), |
| 71 | + is_finite.data<bool>(), x->numel()); |
| 72 | + PADDLE_ENFORCE_EQ( |
| 73 | + r, XPU_SUCCESS, |
| 74 | + platform::errors::External("XPU API(logical_not) return wrong " |
| 75 | + "value[%d %s]", |
| 76 | + r, XPUAPIErrorMsg[r])); |
| 77 | + r = xpu::isnan(dev_ctx.x_context(), |
| 78 | + reinterpret_cast<const XPUTyp*>(x->data<T>()), |
| 79 | + is_nan.data<bool>(), x->numel()); |
| 80 | + PADDLE_ENFORCE_EQ(r, XPU_SUCCESS, platform::errors::External( |
| 81 | + "XPU API(isnan) return wrong " |
| 82 | + "value[%d %s]", |
| 83 | + r, XPUAPIErrorMsg[r])); |
| 84 | + r = xpu::logical_or(dev_ctx.x_context(), is_finite.data<bool>(), |
| 85 | + is_nan.data<bool>(), is_finite.data<bool>(), |
| 86 | + x->numel()); |
| 87 | + PADDLE_ENFORCE_EQ( |
| 88 | + r, XPU_SUCCESS, |
| 89 | + platform::errors::External("XPU API(logical_or) return wrong " |
| 90 | + "value[%d %s]", |
| 91 | + r, XPUAPIErrorMsg[r])); |
| 92 | + r = xpu::any(dev_ctx.x_context(), is_finite.data<bool>(), |
| 93 | + found_inf_data, x->numel()); |
| 94 | + PADDLE_ENFORCE_EQ(r, XPU_SUCCESS, platform::errors::External( |
| 95 | + "XPU API(any) return wrong " |
| 96 | + "value[%d %s]", |
| 97 | + r, XPUAPIErrorMsg[r])); |
| 98 | + memory::Copy(platform::CPUPlace(), &cpu_found_inf_data, |
| 99 | + BOOST_GET_CONST(platform::XPUPlace, dev_ctx.GetPlace()), |
| 100 | + found_inf_data, sizeof(bool)); |
| 101 | + } |
| 102 | + |
| 103 | + if (cpu_found_inf_data) { |
| 104 | + inverse_scale = 0.0; |
| 105 | + } |
| 106 | + auto dev_env = XPUEnv::getenv("XPUSIM_DEVICE_MODEL"); |
| 107 | + |
| 108 | + if (std::is_same<T, paddle::platform::float16>::value && |
| 109 | + (dev_env == nullptr || std::strcmp(dev_env, "KUNLUN1"))) { |
| 110 | + framework::Tensor float_x; |
| 111 | + framework::Tensor float_out; |
| 112 | + float_x.mutable_data<MPDType>(dev_ctx.GetPlace(), |
| 113 | + x->numel() * sizeof(MPDType)); |
| 114 | + float_out.mutable_data<MPDType>(dev_ctx.GetPlace(), |
| 115 | + out->numel() * sizeof(MPDType)); |
| 116 | + int r = xpu::cast_v2(dev_ctx.x_context(), |
| 117 | + reinterpret_cast<const float16*>(x->data<T>()), |
| 118 | + float_x.data<MPDType>(), x->numel()); |
| 119 | + PADDLE_ENFORCE_EQ(r, XPU_SUCCESS, platform::errors::External( |
| 120 | + "XPU API(cast_v2) return wrong " |
| 121 | + "value[%d %s]", |
| 122 | + r, XPUAPIErrorMsg[r])); |
| 123 | + |
| 124 | + r = xpu::scale(dev_ctx.x_context(), float_x.data<MPDType>(), |
| 125 | + float_out.data<MPDType>(), x->numel(), false, |
| 126 | + inverse_scale, 0.0); |
| 127 | + PADDLE_ENFORCE_EQ(r, XPU_SUCCESS, platform::errors::External( |
| 128 | + "XPU API(scale) return wrong " |
| 129 | + "value[%d %s]", |
| 130 | + r, XPUAPIErrorMsg[r])); |
| 131 | + |
| 132 | + r = xpu::cast_v2(dev_ctx.x_context(), float_out.data<MPDType>(), |
| 133 | + reinterpret_cast<float16*>(out->data<T>()), |
| 134 | + out->numel()); |
| 135 | + |
| 136 | + PADDLE_ENFORCE_EQ(r, XPU_SUCCESS, platform::errors::External( |
| 137 | + "XPU API(cast_v2) return wrong " |
| 138 | + "value[%d %s]", |
| 139 | + r, XPUAPIErrorMsg[r])); |
| 140 | + if (dev_ctx.x_context()->xpu_stream) { |
| 141 | + dev_ctx.Wait(); |
| 142 | + } |
| 143 | + |
| 144 | + } else { |
| 145 | + int r = xpu::scale(dev_ctx.x_context(), |
| 146 | + reinterpret_cast<const XPUTyp*>(x->data<T>()), |
| 147 | + reinterpret_cast<XPUTyp*>(out->data<T>()), |
| 148 | + x->numel(), false, inverse_scale, 0.0); |
| 149 | + PADDLE_ENFORCE_EQ(r, XPU_SUCCESS, platform::errors::External( |
| 150 | + "XPU API(scale) return wrong " |
| 151 | + "value[%d %s]", |
| 152 | + r, XPUAPIErrorMsg[r])); |
| 153 | + } |
| 154 | + } |
| 155 | + memory::Copy(BOOST_GET_CONST(platform::XPUPlace, dev_ctx.GetPlace()), |
| 156 | + found_inf_data, platform::CPUPlace(), &cpu_found_inf_data, |
| 157 | + sizeof(bool)); |
| 158 | + } |
| 159 | +}; |
| 160 | + |
| 161 | +} // namespace operators |
| 162 | +} // namespace paddle |
| 163 | + |
| 164 | +namespace ops = paddle::operators; |
| 165 | +namespace plat = paddle::platform; |
| 166 | +REGISTER_OP_XPU_KERNEL(check_finite_and_unscale, |
| 167 | + ops::CheckFiniteAndUnscaleXPUKernel<float>, |
| 168 | + ops::CheckFiniteAndUnscaleXPUKernel<plat::float16>); |
| 169 | + |
| 170 | +#endif |
0 commit comments