Split the dataset into N partitions. It can support shuffle based on specified random seed. Will return a set of datasets, every dataset contains 1 partition of original dataset. And it can split the dataset based on specified ratios or evenly split into `num_partitions`. Refer to:
(
data: Sequence,
ratios: Sequence[float] | None = None,
num_partitions: int | None = None,
shuffle: bool = False,
seed: int = 0,
drop_last: bool = False,
even_divisible: bool = False,
)
| 1130 | |
| 1131 | |
| 1132 | def partition_dataset( |
| 1133 | data: Sequence, |
| 1134 | ratios: Sequence[float] | None = None, |
| 1135 | num_partitions: int | None = None, |
| 1136 | shuffle: bool = False, |
| 1137 | seed: int = 0, |
| 1138 | drop_last: bool = False, |
| 1139 | even_divisible: bool = False, |
| 1140 | ): |
| 1141 | """ |
| 1142 | Split the dataset into N partitions. It can support shuffle based on specified random seed. |
| 1143 | Will return a set of datasets, every dataset contains 1 partition of original dataset. |
| 1144 | And it can split the dataset based on specified ratios or evenly split into `num_partitions`. |
| 1145 | Refer to: https://pytorch.org/docs/stable/distributed.html#module-torch.distributed.launch. |
| 1146 | |
| 1147 | Note: |
| 1148 | It also can be used to partition dataset for ranks in distributed training. |
| 1149 | For example, partition dataset before training and use `CacheDataset`, every rank trains with its own data. |
| 1150 | It can avoid duplicated caching content in each rank, but will not do global shuffle before every epoch: |
| 1151 | |
| 1152 | .. code-block:: python |
| 1153 | |
| 1154 | data_partition = partition_dataset( |
| 1155 | data=train_files, |
| 1156 | num_partitions=dist.get_world_size(), |
| 1157 | shuffle=True, |
| 1158 | even_divisible=True, |
| 1159 | )[dist.get_rank()] |
| 1160 | |
| 1161 | train_ds = SmartCacheDataset( |
| 1162 | data=data_partition, |
| 1163 | transform=train_transforms, |
| 1164 | replace_rate=0.2, |
| 1165 | cache_num=15, |
| 1166 | ) |
| 1167 | |
| 1168 | Args: |
| 1169 | data: input dataset to split, expect a list of data. |
| 1170 | ratios: a list of ratio number to split the dataset, like [8, 1, 1]. |
| 1171 | num_partitions: expected number of the partitions to evenly split, only works when `ratios` not specified. |
| 1172 | shuffle: whether to shuffle the original dataset before splitting. |
| 1173 | seed: random seed to shuffle the dataset, only works when `shuffle` is True. |
| 1174 | drop_last: only works when `even_divisible` is False and no ratios specified. |
| 1175 | if True, will drop the tail of the data to make it evenly divisible across partitions. |
| 1176 | if False, will add extra indices to make the data evenly divisible across partitions. |
| 1177 | even_divisible: if True, guarantee every partition has same length. |
| 1178 | |
| 1179 | Examples:: |
| 1180 | |
| 1181 | >>> data = [1, 2, 3, 4, 5] |
| 1182 | >>> partition_dataset(data, ratios=[0.6, 0.2, 0.2], shuffle=False) |
| 1183 | [[1, 2, 3], [4], [5]] |
| 1184 | >>> partition_dataset(data, num_partitions=2, shuffle=False) |
| 1185 | [[1, 3, 5], [2, 4]] |
| 1186 | >>> partition_dataset(data, num_partitions=2, shuffle=False, even_divisible=True, drop_last=True) |
| 1187 | [[1, 3], [2, 4]] |
| 1188 | >>> partition_dataset(data, num_partitions=2, shuffle=False, even_divisible=True, drop_last=False) |
| 1189 | [[1, 3, 5], [2, 4, 1]] |
searching dependent graphs…