// Copyright (c) 2025 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. #include "paddle/phi/backends/xpu/xpu_context.h" #include "paddle/phi/core/kernel_registry.h" namespace xpu = baidu::xpu::api; namespace phi { template void MoECombineBackwardKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& combine_weights, const DenseTensor& scatter_index, const DenseTensor& grad_y, DenseTensor* grad_x, DenseTensor* grad_combine_weights_helper) { int64_t seq_len = combine_weights.dims()[0]; int64_t k = combine_weights.dims()[1]; int64_t hidden_size = x.dims()[1]; using XPUType = typename XPUTypeTrait::Type; auto dy_data = reinterpret_cast(grad_y.data()); auto x_data = reinterpret_cast(x.data()); auto weight_data = reinterpret_cast(combine_weights.data()); auto index_data = scatter_index.data(); auto dx_data = reinterpret_cast(grad_x->data()); auto dw_data = reinterpret_cast(grad_combine_weights_helper->data()); int ret = xpu::constant(dev_ctx.x_context(), dx_data, x.numel(), 0.0f); PADDLE_ENFORCE_XDNN_SUCCESS(ret, "constant"); ret = xpu::moe_combine_grad(dev_ctx.x_context(), dy_data, x_data, weight_data, index_data, dx_data, dw_data, seq_len, k, hidden_size); PADDLE_ENFORCE_XDNN_SUCCESS(ret, "moe_combine_grad"); } template void MoeCombineGradKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& combine_weights, const DenseTensor& scatter_index, const DenseTensor& grad_y, DenseTensor* grad_x, DenseTensor* grad_combine_weights_helper) { PD_CHECK(x.dims().size() == 2, "The shape of X must be 2."); PD_CHECK(scatter_index.dtype() == DataType::INT32, "MoE combine only supports int32 for scatter_index"); dev_ctx.template Alloc(grad_x); dev_ctx.template Alloc(grad_combine_weights_helper); MoECombineBackwardKernel(dev_ctx, x, combine_weights, scatter_index, grad_y, grad_x, grad_combine_weights_helper); } } // namespace phi PD_REGISTER_KERNEL(moe_combine_grad, XPU, ALL_LAYOUT, phi::MoeCombineGradKernel, float, phi::bfloat16, phi::float16) {}