Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion paddle/phi/kernels/xpu/weight_only_linear_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,34 @@ void WeightOnlyLinearKernel(const Context& dev_ctx,
input_y, nullptr, m, n, n, false};
baidu::xpu::xblas::FcFusionTensor<XPUType> tensor_y{
input_y, nullptr, m, n, n, false};
DenseTensor weight_scale_fp32;
if (weight_scale.dtype() != phi::DataType::FLOAT32 &&
weight_scale.dims().size() != 0) {
weight_scale_fp32.Resize(weight_scale.dims());
dev_ctx.template Alloc<float>(&weight_scale_fp32);
int r = baidu::xpu::api::cast<XPUType, float>(
dev_ctx.x_context(),
reinterpret_cast<const XPUType*>(weight_scale.data<T>()),
weight_scale_fp32.data<float>(),
weight_scale.numel());
PADDLE_ENFORCE_XDNN_SUCCESS(r, "cast");
}
const float* weight_scale_ptr = nullptr;
if (weight_scale.dims().size() != 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么上面不需要考虑weight_scale.dims().size() != 0?这种情况调用cast会出错吗?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

考虑scalar tensor,dims().size()可能为0,但是numel为1,考虑是否是从numel来判断

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原有实现中dims().size()等于零时直接传nullptr进算子,所以不需要考虑scalar。cast出错风险我修一下

if (weight_scale.dtype() == phi::DataType::FLOAT32) {
weight_scale_ptr = weight_scale.data<float>();
} else {
weight_scale_ptr = weight_scale_fp32.data<float>();
}
}
baidu::xpu::xblas::FcFusionEpilogue<float, float> epilogue{
api::Activation_t::LINEAR,
bias.is_initialized() ? (bias.get().dtype() == phi::DataType::FLOAT16
? bias_fp32.data<float>()
: bias.get().data<float>())
: nullptr,
nullptr,
weight_scale.dims().size() != 0 ? weight_scale.data<float>() : nullptr,
weight_scale_ptr,
0,
1,
nullptr};
Expand Down
Loading