(input_, dim, kernel_size)
| 195 | |
| 196 | |
| 197 | def _pass_from_previous_rank(input_, dim, kernel_size): |
| 198 | # Bypass the function if kernel size is 1 |
| 199 | if kernel_size == 1: |
| 200 | return input_ |
| 201 | |
| 202 | group = get_context_parallel_group() |
| 203 | cp_rank = get_context_parallel_rank() |
| 204 | cp_group_rank = get_context_parallel_group_rank() |
| 205 | cp_world_size = get_context_parallel_world_size() |
| 206 | |
| 207 | # print('in _pass_from_previous_rank, cp_rank:', cp_rank, 'input_size:', input_.shape) |
| 208 | |
| 209 | global_rank = torch.distributed.get_rank() |
| 210 | global_world_size = torch.distributed.get_world_size() |
| 211 | |
| 212 | input_ = input_.transpose(0, dim) |
| 213 | |
| 214 | # pass from last rank |
| 215 | send_rank = global_rank + 1 |
| 216 | recv_rank = global_rank - 1 |
| 217 | if send_rank % cp_world_size == 0: |
| 218 | send_rank -= cp_world_size |
| 219 | if recv_rank % cp_world_size == cp_world_size - 1: |
| 220 | recv_rank += cp_world_size |
| 221 | |
| 222 | if cp_rank < cp_world_size - 1: |
| 223 | req_send = torch.distributed.isend(input_[-kernel_size + 1 :].contiguous(), send_rank, group=group) |
| 224 | if cp_rank > 0: |
| 225 | recv_buffer = torch.empty_like(input_[-kernel_size + 1 :]).contiguous() |
| 226 | req_recv = torch.distributed.irecv(recv_buffer, recv_rank, group=group) |
| 227 | |
| 228 | if cp_rank == 0: |
| 229 | input_ = torch.cat([input_[:1]] * (kernel_size - 1) + [input_], dim=0) |
| 230 | else: |
| 231 | req_recv.wait() |
| 232 | input_ = torch.cat([recv_buffer, input_], dim=0) |
| 233 | |
| 234 | input_ = input_.transpose(0, dim).contiguous() |
| 235 | |
| 236 | # print('out _pass_from_previous_rank, cp_rank:', cp_rank, 'input_size:', input_.shape) |
| 237 | |
| 238 | return input_ |
| 239 | |
| 240 | |
| 241 | def _fake_cp_pass_from_previous_rank(input_, dim, kernel_size, cache_padding=None): |
no test coverage detected