Broadcast data from rank zero of each model parallel group to the members of the same model parallel group. Arguments: keys: list of keys in the data disctionary to be broadcasted data: data dictionary of string keys and cpu tensor values. datatype: torch data type o
(keys, data, datatype)
| 80 | |
| 81 | |
| 82 | def broadcast_data(keys, data, datatype): |
| 83 | """Broadcast data from rank zero of each model parallel group to the |
| 84 | members of the same model parallel group. |
| 85 | |
| 86 | Arguments: |
| 87 | keys: list of keys in the data disctionary to be broadcasted |
| 88 | data: data dictionary of string keys and cpu tensor values. |
| 89 | datatype: torch data type of all tensors in data associated |
| 90 | with keys. |
| 91 | """ |
| 92 | # Build (key, size) and (key, number of elements) dictionaries along |
| 93 | # with the total number of elements on all ranks. |
| 94 | key_size, key_numel, total_numel = _build_key_size_numel_dictionaries(keys, data) |
| 95 | |
| 96 | # Pack on rank zero. |
| 97 | if get_tensor_model_parallel_rank() == 0: |
| 98 | # Check that all keys have the same data type. |
| 99 | _check_data_types(keys, data, datatype) |
| 100 | # Flatten the data associated with the keys |
| 101 | flatten_data = torch.cat( |
| 102 | [data[key].contiguous().view(-1) for key in keys], dim=0 |
| 103 | ).cuda() |
| 104 | else: |
| 105 | flatten_data = torch.empty( |
| 106 | total_numel, device=torch.cuda.current_device(), dtype=datatype |
| 107 | ) |
| 108 | |
| 109 | # Broadcast |
| 110 | torch.distributed.broadcast( |
| 111 | flatten_data, |
| 112 | get_tensor_model_parallel_src_rank(), |
| 113 | group=get_tensor_model_parallel_group(), |
| 114 | ) |
| 115 | |
| 116 | # Unpack |
| 117 | output = {} |
| 118 | offset = 0 |
| 119 | for key in keys: |
| 120 | size = key_size[key] |
| 121 | numel = key_numel[key] |
| 122 | output[key] = flatten_data.narrow(0, offset, numel).view(size) |
| 123 | offset += numel |
| 124 | |
| 125 | return output |
nothing calls this directly
no test coverage detected