|
| 1 | +// Copyright (c) 2023 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 | +#if defined(PADDLE_WITH_XPU_XFT) |
| 15 | +#include <xft/xdnn_plugin.h> |
| 16 | +#endif |
| 17 | +#include "paddle/common/enforce.h" |
| 18 | +#include "paddle/phi/backends/xpu/enforce_xpu.h" |
| 19 | +#include "paddle/phi/core/dense_tensor.h" |
| 20 | +#include "paddle/phi/core/kernel_registry.h" |
| 21 | +#include "paddle/phi/kernels/cast_kernel.h" |
| 22 | +#include "paddle/phi/kernels/transpose_kernel.h" |
| 23 | + |
| 24 | +namespace phi { |
| 25 | + |
| 26 | +template <typename T, typename Context> |
| 27 | +void WeightQuantizeKernel(const Context& dev_ctx, |
| 28 | + const DenseTensor& x, |
| 29 | + const std::string& algo, |
| 30 | + const int32_t arch, |
| 31 | + const int32_t group_size, |
| 32 | + DenseTensor* out, |
| 33 | + DenseTensor* scale) { |
| 34 | +#if defined(PADDLE_WITH_XPU_XFT) |
| 35 | + using XPUType = typename XPUTypeTrait<T>::Type; |
| 36 | + auto xpu_ctx = static_cast<const phi::XPUContext*>(&dev_ctx); |
| 37 | + int k = x.dims()[0]; |
| 38 | + int n = x.dims()[1]; |
| 39 | + scale->Resize({static_cast<int64_t>(n)}); |
| 40 | + |
| 41 | + dev_ctx.template Alloc<float>(scale); |
| 42 | + |
| 43 | + if (algo == "weight_only_int8") { |
| 44 | + out->Resize({static_cast<int64_t>(k), static_cast<int64_t>(n)}); |
| 45 | + dev_ctx.template Alloc<int8_t>(out); |
| 46 | + |
| 47 | + int ret = baidu::xpu::xftkernel::xft_quant2d_per_channel<XPUType, float>( |
| 48 | + xpu_ctx->x_context(), |
| 49 | + reinterpret_cast<const XPUType*>(x.template data<T>()), |
| 50 | + nullptr, |
| 51 | + out->data<int8_t>(), |
| 52 | + scale->data<float>(), |
| 53 | + k, |
| 54 | + n); |
| 55 | + PADDLE_ENFORCE_XDNN_SUCCESS(ret, "quant2d"); |
| 56 | + } else { |
| 57 | + PADDLE_THROW(common::errors::Unimplemented( |
| 58 | + "Weight quantize only supports weight_only_int8 on XPU now.")); |
| 59 | + } |
| 60 | +#else |
| 61 | + PADDLE_THROW(common::errors::Unimplemented( |
| 62 | + "weight_quantize is not supported since it's not " |
| 63 | + "compiled with XPU_XFT")); |
| 64 | +#endif |
| 65 | +} |
| 66 | +} // namespace phi |
| 67 | + |
| 68 | +PD_REGISTER_KERNEL(weight_quantize, |
| 69 | + XPU, |
| 70 | + ALL_LAYOUT, |
| 71 | + phi::WeightQuantizeKernel, |
| 72 | + phi::dtype::float16, |
| 73 | + phi::dtype::bfloat16) {} |
0 commit comments