|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <c10/xpu/XPUStream.h> |
| 4 | +#include <dnnl.hpp> |
| 5 | +#include <torch/torch.h> |
| 6 | + |
| 7 | +#include "onednn_ext.h" |
| 8 | + |
| 9 | +namespace oneDNN { |
| 10 | + |
| 11 | +using bias_type_t = at::native::onednn::bias_type_t; |
| 12 | +using trans_type_t = at::native::onednn::trans_type_t; |
| 13 | +using GpuStreamManager = at::native::onednn::GpuStreamManager; |
| 14 | +using GpuEngineManager = at::native::onednn::GpuEngineManager; |
| 15 | + |
| 16 | +static inline void dnnl_matmul_w8a16_fp8( |
| 17 | + torch::Tensor& result, const torch::Tensor& mat1, const torch::Tensor& mat2, |
| 18 | + bool trans_b, const std::optional<torch::Tensor>& bias, |
| 19 | + const torch::Tensor& m2_sc, const int64_t group_size = 0) { |
| 20 | + TORCH_CHECK(mat2.scalar_type() == at::ScalarType::Float8_e5m2 || |
| 21 | + mat2.scalar_type() == at::ScalarType::Float8_e4m3fn, |
| 22 | + "weight must be f8_e5m2 or f8_e4m3fn for fp8 matmul"); |
| 23 | + auto src_sz = mat1.sizes(); |
| 24 | + auto o_sz = result.sizes(); |
| 25 | + |
| 26 | + const int m = std::reduce(src_sz.begin(), src_sz.end() - 1, 1, |
| 27 | + std::multiplies<int64_t>()); |
| 28 | + const int n = o_sz.back(); // presume channel last format |
| 29 | + const int k = *(src_sz.end() - 1); |
| 30 | + |
| 31 | + // get joint dtypes |
| 32 | + joint_dtypes_t jd; |
| 33 | + auto in_dtype = mat1.scalar_type(); |
| 34 | + auto wei_dtype = mat2.scalar_type(); |
| 35 | + if (in_dtype == at::ScalarType::Half) { |
| 36 | + jd = wei_dtype == at::ScalarType::Float8_e5m2 ? joint_dtypes_t::f16_f8_e5m2 |
| 37 | + : joint_dtypes_t::f16_f8_e4m3; |
| 38 | + } else if (in_dtype == at::ScalarType::BFloat16) { |
| 39 | + jd = wei_dtype == at::ScalarType::Float8_e5m2 |
| 40 | + ? joint_dtypes_t::bf16_f8_e5m2 |
| 41 | + : joint_dtypes_t::bf16_f8_e4m3; |
| 42 | + } else { |
| 43 | + TORCH_INTERNAL_ASSERT( |
| 44 | + false, "Unsupported data type for fp8 matmul: ", mat1.scalar_type()); |
| 45 | + } |
| 46 | + |
| 47 | + // get bias type |
| 48 | + bias_type_t b_type; |
| 49 | + if (bias.has_value() && bias.value().defined()) { |
| 50 | + auto& b = bias.value(); |
| 51 | + const auto nuelm = b.numel(); |
| 52 | + if (nuelm == 1) { |
| 53 | + b_type = bias_type_t::scalar; |
| 54 | + } else if (nuelm == m * n) { |
| 55 | + b_type = bias_type_t::mn; |
| 56 | + } else if (b.size(b.dim() - 1) == n && nuelm == n) { |
| 57 | + b_type = bias_type_t::n; |
| 58 | + } else if (b.size(b.dim() - 1) == 1 && nuelm == m) { |
| 59 | + b_type = bias_type_t::m; |
| 60 | + } else if (nuelm == 0) { |
| 61 | + b_type = bias_type_t::none; |
| 62 | + } else { |
| 63 | + TORCH_CHECK(0, "unsupported bias dim in matmul ...", b.sizes()); |
| 64 | + } |
| 65 | + } else { |
| 66 | + b_type = bias_type_t::none; |
| 67 | + } |
| 68 | + |
| 69 | + trans_type_t tt = trans_type_t::nn; |
| 70 | + if (trans_b) { |
| 71 | + // transpose mat2 |
| 72 | + tt = trans_type_t::nt; |
| 73 | + } |
| 74 | + |
| 75 | + // get lda ldb and ldc |
| 76 | + auto mat1_strides = mat1.strides(); |
| 77 | + int64_t leading_dim = -1; |
| 78 | + if (mat1.dim() == 2) { |
| 79 | + leading_dim = 0; |
| 80 | + } else if (mat1.dim() == 3) { |
| 81 | + leading_dim = mat1_strides[0] < mat1_strides[1] ? 0 : 1; |
| 82 | + } else { |
| 83 | + TORCH_CHECK(false, |
| 84 | + "Unsupported input dimension for fp8 matmul: ", mat1.dim()); |
| 85 | + } |
| 86 | + int64_t lda = mat1_strides[leading_dim]; |
| 87 | + int64_t ldb = mat2.strides()[mat2.dim() - 1] == 1 |
| 88 | + ? mat2.strides()[mat2.dim() - 2] |
| 89 | + : mat2.strides()[mat2.dim() - 1]; |
| 90 | + int64_t ldc = result.strides()[leading_dim]; |
| 91 | + |
| 92 | + auto f_attr = [&](dnnl::primitive_attr& pattr) { |
| 93 | + pattr.set_scratchpad_mode(dnnl::scratchpad_mode::user); |
| 94 | + }; |
| 95 | + |
| 96 | + int arg_off = 0; |
| 97 | + |
| 98 | + // ************************************************************ |
| 99 | + // get device, engine, stream |
| 100 | + const int dev_id = c10::xpu::getCurrentXPUStream().device_index(); |
| 101 | + at::Device curDevice = at::Device(at::kXPU, dev_id); |
| 102 | + auto engine = GpuEngineManager::Instance().get_engine(curDevice); |
| 103 | + |
| 104 | + auto& matmul_ext = matmul_primitive_create_and_cache( |
| 105 | + jd, tt, b_type, m, n, k, lda, ldb, ldc, dev_id, f_attr, group_size); |
| 106 | + |
| 107 | + matmul_ext.set_attribute(arg_off++, DNNL_ARG_ATTR_SCALES | DNNL_ARG_WEIGHTS, |
| 108 | + m2_sc.data_ptr(), [&]() { |
| 109 | + return at::native::onednn::make_onednn_memory( |
| 110 | + get_onednn_md(m2_sc), engine, |
| 111 | + m2_sc.data_ptr()); |
| 112 | + }); |
| 113 | + |
| 114 | + std::vector<std::pair<int, void*>> arg_handles; |
| 115 | + arg_handles.reserve(8); |
| 116 | + |
| 117 | + arg_handles.emplace_back(DNNL_ARG_SRC, mat1.data_ptr()); |
| 118 | + arg_handles.emplace_back(DNNL_ARG_WEIGHTS, mat2.data_ptr()); |
| 119 | + arg_handles.emplace_back(DNNL_ARG_DST, result.data_ptr()); |
| 120 | + if (b_type != bias_type_t::none) { |
| 121 | + arg_handles.emplace_back(DNNL_ARG_BIAS, bias.value().data_ptr()); |
| 122 | + } |
| 123 | + |
| 124 | + int scratchpad_size = matmul_ext.get_scratchpad_size(); |
| 125 | + torch::Tensor scratchpad_tensor = at::empty( |
| 126 | + {scratchpad_size}, mat1.options().dtype(at::kByte), c10::nullopt); |
| 127 | + arg_handles.emplace_back(DNNL_ARG_SCRATCHPAD, scratchpad_tensor.data_ptr()); |
| 128 | + |
| 129 | + auto& strm = GpuStreamManager::Instance().get_stream(); |
| 130 | + auto qfp8_matmul_event = |
| 131 | + matmul_ext.execute(strm, engine, std::move(arg_handles), arg_off); |
| 132 | +} |
| 133 | +} // namespace oneDNN |
0 commit comments