MCPcopy Create free account
hub / github.com/OpenGVLab/HumanBench / dev_collate

Function dev_collate

PATH/core/solvers/solver_multitask_dev.py:1228–1329  ·  view source on GitHub ↗

r""" Function that takes in a batch of data and puts the elements within the batch into a tensor with an additional outer dimension - batch size. The exact output type can be a :class:`torch.Tensor`, a `Sequence` of :class:`torch.Tensor`, a Collection of :class:`torch

(batch, nested=False)

Source from the content-addressed store, hash-verified

1226
1227
1228def dev_collate(batch, nested=False): # altered collate_fn to support 'Instance' object within batch
1229 r"""
1230 Function that takes in a batch of data and puts the elements within the batch
1231 into a tensor with an additional outer dimension - batch size. The exact output type can be
1232 a :class:`torch.Tensor`, a `Sequence` of :class:`torch.Tensor`, a
1233 Collection of :class:`torch.Tensor`, or left unchanged, depending on the input type.
1234 This is used as the default function for collation when
1235 `batch_size` or `batch_sampler` is defined in :class:`~torch.utils.data.DataLoader`.
1236
1237 Here is the general input type (based on the type of the element within the batch) to output type mapping:
1238 * :class:`torch.Tensor` -> :class:`torch.Tensor` (with an added outer dimension batch size)
1239 * NumPy Arrays -> :class:`torch.Tensor`
1240 * `float` -> :class:`torch.Tensor`
1241 * `int` -> :class:`torch.Tensor`
1242 * `str` -> `str` (unchanged)
1243 * `bytes` -> `bytes` (unchanged)
1244 * `Mapping[K, V_i]` -> `Mapping[K, dev_collate([V_1, V_2, ...])]`
1245 * `NamedTuple[V1_i, V2_i, ...]` -> `NamedTuple[dev_collate([V1_1, V1_2, ...]), dev_collate([V2_1, V2_2, ...]), ...]`
1246 * `Sequence[V1_i, V2_i, ...]` -> `Sequence[dev_collate([V1_1, V1_2, ...]), dev_collate([V2_1, V2_2, ...]), ...]`
1247
1248 Args:
1249 batch: a single batch to be collated
1250
1251 Examples:
1252 >>> # Example with a batch of `int`s:
1253 >>> dev_collate([0, 1, 2, 3])
1254 tensor([0, 1, 2, 3])
1255 >>> # Example with a batch of `str`s:
1256 >>> dev_collate(['a', 'b', 'c'])
1257 ['a', 'b', 'c']
1258 >>> # Example with `Map` inside the batch:
1259 >>> dev_collate([{'A': 0, 'B': 1}, {'A': 100, 'B': 100}])
1260 {'A': tensor([ 0, 100]), 'B': tensor([ 1, 100])}
1261 >>> # Example with `NamedTuple` inside the batch:
1262 >>> Point = namedtuple('Point', ['x', 'y'])
1263 >>> dev_collate([Point(0, 0), Point(1, 1)])
1264 Point(x=tensor([0, 1]), y=tensor([0, 1]))
1265 >>> # Example with `Tuple` inside the batch:
1266 >>> dev_collate([(0, 1), (2, 3)])
1267 [tensor([0, 2]), tensor([1, 3])]
1268 >>> # Example with `List` inside the batch:
1269 >>> dev_collate([[0, 1], [2, 3]])
1270 [tensor([0, 2]), tensor([1, 3])]
1271 """
1272 elem = batch[0]
1273 elem_type = type(elem)
1274 if isinstance(elem, torch.Tensor):
1275 if elem.ndim == 3 and nested:
1276 return nested_tensor_from_tensor_list(batch)
1277 else:
1278 out = None
1279 if torch.utils.data.get_worker_info() is not None:
1280 # If we're in a background process, concatenate directly into a
1281 # shared memory tensor to avoid an extra copy
1282 numel = sum(x.numel() for x in batch)
1283 storage = elem.storage()._new_shared(numel)
1284 out = elem.new(storage).resize_(len(batch), *list(elem.size()))
1285 return torch.stack(batch, 0, out=out)

Callers

nothing calls this directly

Calls 3

sizeMethod · 0.80
stackMethod · 0.80

Tested by

no test coverage detected