| 518 | |
| 519 | template <typename T> |
| 520 | void MHABackwardProxyBase::exec_internal(MHA_PROXY_BACKWARD_EXEC_PARAM) { |
| 521 | auto wksp_bundle = get_workspace_bundle( |
| 522 | MHA_PROXY_BACKWARD_TENSOR_TO_LAYOUT_CALL, workspace.raw_ptr); |
| 523 | auto mask_bundle = get_mask_reservespace_bundle( |
| 524 | MHA_PROXY_BACKWARD_TENSOR_TO_LAYOUT_CALL, mask_reservespace.raw_ptr()); |
| 525 | auto othr_bundle = get_othr_reservespace_bundle( |
| 526 | MHA_PROXY_BACKWARD_TENSOR_TO_LAYOUT_CALL, othr_reservespace.raw_ptr()); |
| 527 | size_t head = param.num_heads; |
| 528 | size_t one = 1; |
| 529 | TensorND mask1{mask_bundle.get_workspace(0).raw_ptr, m_mask1_layout}; |
| 530 | TensorND mask2{mask_bundle.get_workspace(1).raw_ptr, m_mask2_layout}; |
| 531 | TensorND nq, nk, nv; |
| 532 | if (param.qproj_size == 0 and param.num_heads == 1) { |
| 533 | nq = queries; |
| 534 | } else { |
| 535 | nq = TensorND{othr_bundle.get_workspace(0).raw_ptr, m_grad_nq_layout}; |
| 536 | } |
| 537 | if (param.kproj_size == 0 and param.num_heads == 1) { |
| 538 | nk = keys; |
| 539 | } else { |
| 540 | nk = TensorND{othr_bundle.get_workspace(1).raw_ptr, m_grad_nk_layout}; |
| 541 | } |
| 542 | if (param.vproj_size == 0 and param.num_heads == 1) { |
| 543 | nv = values; |
| 544 | } else { |
| 545 | nv = TensorND{othr_bundle.get_workspace(2).raw_ptr, m_grad_nv_layout}; |
| 546 | } |
| 547 | TensorND nx{othr_bundle.get_workspace(3).raw_ptr, m_grad_nx_layout}; |
| 548 | |
| 549 | // out = dropout(out) |
| 550 | TensorND grad_drop2{wksp_bundle.get_workspace(0).raw_ptr, m_grad_drop2_layout}; |
| 551 | m_dropoutbw_opr->param().drop_prob = param.out_prob; |
| 552 | m_dropoutbw_opr->exec(diff, mask2, grad_drop2, wksp_bundle.get_workspace(1)); |
| 553 | |
| 554 | // out = z @ wo + bo |
| 555 | TensorND grad_z; |
| 556 | if (param.oproj_size) { |
| 557 | TensorND z{othr_bundle.get_workspace(4).raw_ptr, m_grad_z_layout}; |
| 558 | TensorND oweight{qkvo_weight_bias.ptr<T>() + m_wo_off, m_wo_layout}; |
| 559 | grad_z = TensorND{wksp_bundle.get_workspace(2).raw_ptr, m_grad_z_layout}; |
| 560 | TensorND grad_wo{wksp_bundle.get_workspace(3).raw_ptr, m_grad_wo_layout}; |
| 561 | m_matmul_opr->param().transposeA = false; |
| 562 | m_matmul_opr->param().transposeB = true; |
| 563 | matmul_exec( |
| 564 | m_matmul_opr, grad_drop2, oweight, grad_z, |
| 565 | wksp_bundle.get_workspace(4)); |
| 566 | m_bmatmul_opr->param().transposeA = true; |
| 567 | m_bmatmul_opr->param().transposeB = false; |
| 568 | m_bmatmul_opr->exec(z, grad_drop2, grad_wo, wksp_bundle.get_workspace(5)); |
| 569 | std::swap(m_grad_wo_layout.shape[0], one); |
| 570 | TensorND doweight{dqkvo_weight_bias.ptr<T>() + m_wo_off, m_grad_wo_layout}; |
| 571 | std::swap(m_grad_wo_layout.shape[0], one); |
| 572 | m_reduce_opr->param().axis = 0; |
| 573 | m_reduce_opr->exec(grad_wo, doweight, wksp_bundle.get_workspace(6)); |
| 574 | if (param.obias) { |
| 575 | TensorND dobias{dqkvo_weight_bias.ptr<T>() + m_bo_off, m_bo_layout}; |
| 576 | TensorND grad_bo{wksp_bundle.get_workspace(7).raw_ptr, m_grad_bo_layout}; |
| 577 | m_reduce_opr->exec(grad_drop2, grad_bo, wksp_bundle.get_workspace(8)); |
nothing calls this directly
no test coverage detected