def scatter(tensor, devices, chunk_sizes=None, dim=0):
 if (chunk_sizes is None):
  chunks = tensor.chunk(len(devices), dim)
 else:
  assert (sum(chunk_sizes) == tensor.size(dim)), "given chunk sizes don't sum up to the tensor's size (sum(chunk_sizes) == {}, but expected {})".format(sum(chunk_sizes), tensor.size(dim))
  assert (min(chunk_sizes) > 0), 'got a negative chunk_size'
  chunks = [tensor.narrow(dim, (start - size), size) for (start, size) in zip(_accumulate(chunk_sizes), chunk_sizes)]
 return tuple((chunk.cuda(gpu_id, async=chunk.is_contiguous()) for (gpu_id, chunk) in zip(devices, chunks)))

def scatter ( tensor , devices , chunk_sizes = None , dim = 0 ) : if ( chunk_sizes is None ) : chunks = tensor . chunk ( len ( devices ) , dim ) else : assert ( sum ( chunk_sizes ) == tensor . size ( dim ) ) , "given chunk sizes don't sum up to the tensor's size (sum(chunk_sizes) == {}, but expected {})" . format ( sum ( chunk_sizes ) , tensor . size ( dim ) ) assert ( min ( chunk_sizes ) > 0 ) , 'got a negative chunk_size' chunks = [ tensor . narrow ( dim , ( start - size ) , size ) for ( start , size ) in zip ( _accumulate ( chunk_sizes ) , chunk_sizes ) ] return tuple ( ( chunk . cuda ( gpu_id , async = chunk . is_contiguous ( ) ) for ( gpu_id , chunk ) in zip ( devices , chunks ) ) )
slices a given variable into approximately equal chunks and distributes them accross given gpus .