()
| 55 | |
| 56 | @torch.no_grad() |
| 57 | def check_forward_equal_with_pytorch_float(): |
| 58 | value = torch.rand(N, S, M, D).cuda() * 0.01 |
| 59 | sampling_locations = torch.rand(N, Lq, M, L, P, 2).cuda() |
| 60 | attention_weights = torch.rand(N, Lq, M, L, P).cuda() + 1e-5 |
| 61 | attention_weights /= attention_weights.sum(-1, |
| 62 | keepdim=True).sum(-2, |
| 63 | keepdim=True) |
| 64 | im2col_step = 2 |
| 65 | output_pytorch = ms_deform_attn_core_pytorch( |
| 66 | value, shapes, sampling_locations, attention_weights).detach().cpu() |
| 67 | output_cuda = MSDeformAttnFunction.apply(value, shapes, level_start_index, |
| 68 | sampling_locations, |
| 69 | attention_weights, |
| 70 | im2col_step).detach().cpu() |
| 71 | fwdok = torch.allclose(output_cuda, output_pytorch, rtol=1e-2, atol=1e-3) |
| 72 | max_abs_err = (output_cuda - output_pytorch).abs().max() |
| 73 | max_rel_err = ((output_cuda - output_pytorch).abs() / |
| 74 | output_pytorch.abs()).max() |
| 75 | |
| 76 | print( |
| 77 | f'* {fwdok} check_forward_equal_with_pytorch_float: max_abs_err {max_abs_err:.2e} max_rel_err {max_rel_err:.2e}' |
| 78 | ) |
| 79 | |
| 80 | |
| 81 | def check_gradient_numerical(channels=4, |
no test coverage detected