|
| 1 | +/* Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +you may not use this file except in compliance with the License. |
| 4 | +You may obtain a copy of the License at |
| 5 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +Unless required by applicable law or agreed to in writing, software |
| 7 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +See the License for the specific language governing permissions and |
| 10 | +limitations under the License. */ |
| 11 | + |
| 12 | +#include "paddle/phi/kernels/quantize_kernel.h" |
| 13 | +#include "paddle/phi/backends/onednn/onednn_reuse.h" |
| 14 | +#include "paddle/phi/core/compat/convert_utils.h" |
| 15 | +#include "paddle/phi/core/enforce.h" |
| 16 | +#include "paddle/phi/core/expect.h" |
| 17 | +#include "paddle/phi/core/kernel_registry.h" |
| 18 | +#include "paddle/phi/core/utils/data_type.h" |
| 19 | + |
| 20 | +namespace phi { |
| 21 | + |
| 22 | +using dnnl::memory; |
| 23 | + |
| 24 | +template <typename T, typename Context> |
| 25 | +void QuantOpKernel(const Context& dev_ctx, |
| 26 | + const DenseTensor& input, |
| 27 | + bool is_negative_input, |
| 28 | + const float scale, |
| 29 | + const float shift, |
| 30 | + const std::string& output_format, |
| 31 | + bool bfloat16, |
| 32 | + DenseTensor* output) { |
| 33 | + const auto quantization_shift = static_cast<int32_t>(shift); |
| 34 | + const bool with_scale = scale != 1.0f; |
| 35 | + const bool with_shift = quantization_shift != 0.0f; |
| 36 | + |
| 37 | + PADDLE_ENFORCE_NE(scale, |
| 38 | + 0.0f, |
| 39 | + phi::errors::InvalidArgument( |
| 40 | + "Quantization scale must be different than 0.0f")); |
| 41 | + PADDLE_ENFORCE(quantization_shift <= 255 && quantization_shift >= 0, |
| 42 | + phi::errors::InvalidArgument( |
| 43 | + "Quantization shift must be lower or equal to ", |
| 44 | + "255 and greater or equal to 0, but got %f", |
| 45 | + quantization_shift)); |
| 46 | + |
| 47 | + auto x_tz = common::vectorize<int64_t>(input.dims()); |
| 48 | + dnnl::primitive_attr attrs; |
| 49 | + static constexpr int32_t mask = 0; |
| 50 | + |
| 51 | + if (with_scale) { |
| 52 | + attrs.set_scales_mask(DNNL_ARG_SRC, mask); |
| 53 | + } |
| 54 | + |
| 55 | + if (with_shift) { |
| 56 | + attrs.set_zero_points_mask(DNNL_ARG_DST, mask); |
| 57 | + } |
| 58 | + |
| 59 | + auto x_type = phi::funcs::ToOneDNNDataType(input.dtype()); |
| 60 | + DataType out_dtype; |
| 61 | + |
| 62 | + if (bfloat16) { |
| 63 | + out_dtype = DataType::BFLOAT16; |
| 64 | + } else if (is_negative_input && !with_shift) { |
| 65 | + out_dtype = DataType::INT8; |
| 66 | + } else { |
| 67 | + out_dtype = DataType::UINT8; |
| 68 | + } |
| 69 | + |
| 70 | + auto out_type = phi::funcs::ToOneDNNDataType(out_dtype); |
| 71 | + |
| 72 | + phi::funcs::ReorderOneDNNHandler reorder_handler( |
| 73 | + x_tz, input.dtype(), x_type, out_dtype, out_type, dev_ctx.GetEngine()); |
| 74 | + |
| 75 | + auto reorder_src_memory_p = reorder_handler.AcquireSrcMemory( |
| 76 | + input.mem_desc(), phi::funcs::to_void_cast(input.data<T>())); |
| 77 | + auto reorder_dst_memory_p = reorder_handler.AcquireDstMemory( |
| 78 | + output, input.mem_desc(), dev_ctx.GetPlace()); |
| 79 | + |
| 80 | + auto reorder_p = reorder_handler.AcquireReorder( |
| 81 | + reorder_dst_memory_p, reorder_src_memory_p, attrs); |
| 82 | + |
| 83 | + auto& astream = phi::OneDNNContext::tls().get_stream(); |
| 84 | + |
| 85 | + auto scales_md = dnnl::memory::desc( |
| 86 | + {1}, dnnl::memory::data_type::f32, dnnl::memory::format_tag::x); |
| 87 | + auto scales_mem = dnnl::memory( |
| 88 | + scales_md, dev_ctx.GetEngine(), phi::funcs::to_void_cast<float>(&scale)); |
| 89 | + auto zero_points_md = dnnl::memory::desc( |
| 90 | + {1}, dnnl::memory::data_type::s32, dnnl::memory::format_tag::x); |
| 91 | + auto zero_points_mem = |
| 92 | + dnnl::memory(zero_points_md, |
| 93 | + dev_ctx.GetEngine(), |
| 94 | + phi::funcs::to_void_cast<int32_t>(&quantization_shift)); |
| 95 | + |
| 96 | + std::unordered_map<int, dnnl::memory> reorder_args; |
| 97 | + reorder_args.insert({DNNL_ARG_SRC, *reorder_src_memory_p}); |
| 98 | + reorder_args.insert({DNNL_ARG_DST, *reorder_dst_memory_p}); |
| 99 | + if (with_scale) { |
| 100 | + reorder_args.insert({DNNL_ARG_ATTR_SCALES | DNNL_ARG_SRC, scales_mem}); |
| 101 | + } |
| 102 | + if (with_shift) { |
| 103 | + reorder_args.insert( |
| 104 | + {DNNL_ARG_ATTR_ZERO_POINTS | DNNL_ARG_DST, zero_points_mem}); |
| 105 | + } |
| 106 | + |
| 107 | + reorder_p->execute(astream, reorder_args); |
| 108 | + astream.wait(); |
| 109 | + |
| 110 | + output->set_mem_desc(reorder_dst_memory_p->get_desc()); |
| 111 | +} |
| 112 | +} // namespace phi |
| 113 | + |
| 114 | +PD_REGISTER_KERNEL(quantize, OneDNN, ONEDNN, phi::QuantOpKernel, float) {} |
0 commit comments