MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / string_list_all_gather

Function string_list_all_gather

monai/utils/dist.py:143–171  ·  view source on GitHub ↗

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")

Source from the content-addressed store, hash-verified

141
142
143def 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
174class RankFilter(Filter):

Callers 2

__call__Method · 0.90
_finalizeMethod · 0.90

Calls 3

splitMethod · 0.80
decodeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…