Utility function for distributed data parallel to all gather a list of strings. Refer to the idea of ignite `all_gather(string)`: https://pytorch.org/ignite/v0.4.5/distributed.html#ignite.distributed.utils.all_gather. Note: If has ignite installed, will execute based on ignite dist
(strings: list[str], delimiter: str = "\t")
| 141 | |
| 142 | |
| 143 | def string_list_all_gather(strings: list[str], delimiter: str = "\t") -> list[str]: |
| 144 | """ |
| 145 | Utility function for distributed data parallel to all gather a list of strings. |
| 146 | Refer to the idea of ignite `all_gather(string)`: |
| 147 | https://pytorch.org/ignite/v0.4.5/distributed.html#ignite.distributed.utils.all_gather. |
| 148 | |
| 149 | Note: If has ignite installed, will execute based on ignite distributed APIs, otherwise, if the native |
| 150 | PyTorch distributed group initialized, will execute based on native PyTorch distributed APIs. |
| 151 | |
| 152 | Args: |
| 153 | strings: a list of strings to all gather. |
| 154 | delimiter: use the delimiter to join the string list to be a long string, |
| 155 | then all gather across ranks and split to a list. default to "\t". |
| 156 | |
| 157 | """ |
| 158 | world_size: int = 1 |
| 159 | if has_ignite: |
| 160 | world_size = idist.get_world_size() |
| 161 | elif dist.is_available() and dist.is_initialized(): |
| 162 | world_size = dist.get_world_size() |
| 163 | |
| 164 | if world_size <= 1: |
| 165 | return strings |
| 166 | |
| 167 | joined = delimiter.join(strings) |
| 168 | gathered = evenly_divisible_all_gather(torch.tensor(bytearray(joined, "utf-8"), dtype=torch.long), concat=False) |
| 169 | _gathered = [bytearray(g.tolist()).decode("utf-8").split(delimiter) for g in gathered] |
| 170 | |
| 171 | return [i for k in _gathered for i in k] |
| 172 | |
| 173 | |
| 174 | class RankFilter(Filter): |
no test coverage detected
searching dependent graphs…