Helper function to copy gradient from array interface to linalg::Matrix
| 1168 | |
| 1169 | // Helper function to copy gradient from array interface to linalg::Matrix |
| 1170 | void CopyGradientFromArrays(Context const *ctx, ArrayInterface<2, false> const &i_grad, |
| 1171 | ArrayInterface<2, false> const &i_hess, |
| 1172 | linalg::Matrix<GradientPair> *out_gpair) { |
| 1173 | auto grad_is_cuda = ArrayInterfaceHandler::IsCudaPtr(i_grad.data); |
| 1174 | auto hess_is_cuda = ArrayInterfaceHandler::IsCudaPtr(i_hess.data); |
| 1175 | CHECK_EQ(grad_is_cuda, hess_is_cuda) << "gradient and hessian should be on the same device."; |
| 1176 | |
| 1177 | if (!grad_is_cuda) { |
| 1178 | out_gpair->Reshape(i_grad.Shape<0>(), i_grad.Shape<1>()); |
| 1179 | auto h_gpair = out_gpair->HostView(); |
| 1180 | DispatchDType(i_grad, DeviceOrd::CPU(), [&](auto &&t_grad) { |
| 1181 | DispatchDType(i_hess, DeviceOrd::CPU(), [&](auto &&t_hess) { |
| 1182 | common::ParallelFor(h_gpair.Size(), ctx->Threads(), |
| 1183 | detail::CustomGradHessOp{t_grad, t_hess, h_gpair}); |
| 1184 | }); |
| 1185 | }); |
| 1186 | } else { |
| 1187 | CopyGradientFromCudaArrays(ctx, i_grad, i_hess, out_gpair); |
| 1188 | } |
| 1189 | } |
| 1190 | } // namespace xgboost |
| 1191 | |
| 1192 | XGB_DLL int XGBoosterTrainOneIter(BoosterHandle handle, DMatrixHandle dtrain, int iter, |
no test coverage detected