llm_int8_matmul_kernel.cu 2.6 KB
Newer Older
FormlessUnit's avatar
FormlessUnit 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2022 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.

FormlessUnit's avatar
FormlessUnit 已提交
15
#include "paddle/phi/kernels/llm_int8_matmul_kernel.h"
FormlessUnit's avatar
FormlessUnit 已提交
16 17 18 19
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/common/amp_type_traits.h"
#include "paddle/phi/core/kernel_registry.h"
#ifndef PADDLE_WITH_HIP
FormlessUnit's avatar
FormlessUnit 已提交
20
#include "paddle/phi/kernels/impl/llm_int8_matmul_kernel_impl.h"
FormlessUnit's avatar
FormlessUnit 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#endif

namespace phi {

template <typename T, typename Context>
void llm_int8_compute(const Context& dev_ctx,
                      const DenseTensor& x,
                      const DenseTensor& weight,
                      const DenseTensor& weight_scale,
                      const float threshold,
                      DenseTensor* out) {
#if defined(PADDLE_WITH_HIP)
  LOG(ERROR) << "Please compile with cublaslt, ROCM platform isn't support it";
#else
  DenseTensor cublaslt_workspace;
  cublaslt_workspace.Resize({{3000000}});
  dev_ctx.template Alloc<int8_t>(&cublaslt_workspace);
  const auto x_dims = x.dims();
  const auto w_dims = weight.dims();
  int k = w_dims[1];
  int n = w_dims[0];
  int m = x.numel() / k;
  // mk * transpose(nk) = mn
  llm_int8::LLMGemm<T>(dev_ctx,
                       &weight,
                       &x,
                       &weight_scale,
                       threshold,
                       out,
                       &cublaslt_workspace,
                       "llm_int8_mat_mul",
                       m,
                       k,
                       n);
#endif
}

template <typename T, typename Context>
FormlessUnit's avatar
FormlessUnit 已提交
59
void LLMInt8MatmulKernel(const Context& dev_ctx,
FormlessUnit's avatar
FormlessUnit 已提交
60 61 62 63 64 65 66 67 68 69 70
                         const DenseTensor& x,
                         const DenseTensor& weight,
                         const DenseTensor& weight_scale,
                         const float threshold,
                         DenseTensor* out) {
  dev_ctx.template Alloc<T>(out);
  llm_int8_compute<T, Context>(
      dev_ctx, x, weight, weight_scale, threshold, out);
}
}  // namespace phi

FormlessUnit's avatar
FormlessUnit 已提交
71
PD_REGISTER_KERNEL(llm_int8_matmul,
FormlessUnit's avatar
FormlessUnit 已提交
72 73
                   GPU,
                   ALL_LAYOUT,
FormlessUnit's avatar
FormlessUnit 已提交
74
                   phi::LLMInt8MatmulKernel,
FormlessUnit's avatar
FormlessUnit 已提交
75
                   phi::dtype::float16) {}