MCPcopy Create free account
hub / github.com/OpenImagingLab/4DSloMo / AttentionStep2WithRelPosValue

Class AttentionStep2WithRelPosValue

pointops2/functions/pointops.py:521–579  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

519dot_prod_with_idx_v3 = DotProdWithIdx_v3.apply
520
521class AttentionStep2WithRelPosValue(Function):
522 @staticmethod
523 def forward(ctx, attn, v, index0, index1, table, rel_idx):
524 """
525 input: attn: (M, h), v: (N, h, hdim), index0: (M), index1: (M), table: (L, h, hdim, 3), rel_idx: (M, 3)
526 output: output: [N, h, hdim]
527 """
528 assert attn.is_contiguous() and v.is_contiguous() and index0.is_contiguous() and index1.is_contiguous() and table.is_contiguous() and rel_idx.is_contiguous()
529
530 M, h = attn.shape
531 N_v, h, hdim = v.shape
532 N_q = index0.max().item() + 1
533
534 output = torch.cuda.FloatTensor(N_q, h, hdim).zero_()
535 pointops_cuda.attention_step2_with_rel_pos_value_forward_cuda(N_q, M, h, hdim, attn, v, index0, index1, table, rel_idx, output)
536
537 # print("attn[:5,:5]: ", attn[:5, :5])
538
539 ctx.save_for_backward(attn, v, index0, index1, table, rel_idx)
540 return output
541
542 @staticmethod
543 def backward(ctx, grad_output):
544 """
545 input: grad_output: (N, h, C//h)
546 output: (M, h), (N, h, C//h), None, None, (L, h, hdim, 3), None
547 """
548 attn, v, index0, index1, table, rel_idx = ctx.saved_tensors
549 N_q, h, hdim = grad_output.shape
550 N_v = v.shape[0]
551 M = attn.shape[0]
552 L = table.shape[0]
553
554 grad_output = grad_output.contiguous()
555 # print("grad_output.is_contiguous(): ", grad_output.is_contiguous())
556 assert attn.is_contiguous() and v.is_contiguous() and index0.is_contiguous() and index1.is_contiguous() and grad_output.is_contiguous() and table.is_contiguous() and rel_idx.is_contiguous()
557
558 # print("back: attn[:5,:5]: ", attn[:5, :5])
559
560 # print("attn.shape: {} v.shape: {}, index0.shape: {}, index1.shape: {}".format(attn.shape, v.shape, index0.shape, index1.shape))
561
562 grad_attn = torch.cuda.FloatTensor(M, h).zero_()
563 grad_v = torch.cuda.FloatTensor(N_v, h, hdim).zero_()
564 grad_table = torch.cuda.FloatTensor(L, h, hdim, 3).zero_()
565
566 # print("attn.shape: {}, grad_attn.shape: {}".format(attn.shape, grad_attn.shape))
567 # print("v.shape: {}, grad_v.shape: {}".format(v.shape, grad_v.shape))
568 # print("table.shape: {}, grad_table.shape: {}".format(table.shape, grad_table.shape))
569
570 # torch.cuda.synchronize()
571 # start = time.time()
572
573 pointops_cuda.attention_step2_with_rel_pos_value_backward_cuda(N_q, M, h, hdim, grad_output, index0, index1, attn, v, table, rel_idx, grad_attn, grad_v, grad_table)
574
575 # torch.cuda.synchronize()
576 # end = time.time()
577 # print("time v10: {}".format(end - start))
578 # # input()

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected