def unpack_small_tensors(replica_grads, packing):
    'Undo the structure alterations to replica_grads done by pack_small_tensors.\n\n  Args:\n    replica_grads: List of List of (grad, var) tuples.\n    packing: A dict generated by pack_small_tensors describing the changes\n      it made to replica_grads.\n\n  Returns:\n    new_replica_grads: identical to replica_grads except that concatenations\n      of small tensors have been split apart and returned to their original\n      positions, paired with their original variables.\n  '
    if (not packing):
        return replica_grads
    new_replica_grads = []
    num_devices = len(replica_grads)
    num_packed = (len(packing.keys()) // num_devices)
    for (dev_idx, gv_list) in enumerate(replica_grads):
        gv_list = list(gv_list)
        new_gv_list = gv_list[num_packed:]
        for i in range(num_packed):
            k = ('%d:%d' % (dev_idx, i))
            gpt = packing[k]
            gv = unpack_grad_tuple(gv_list[i], gpt)
            for (gi, idx) in enumerate(gpt.indices):
                assert (idx == gpt.indices[gi])
                new_gv_list.insert(idx, gv[gi])
        new_replica_grads.append(new_gv_list)
    return new_replica_grads
Undo the structure alterations to replica_grads done by pack_small_tensors.

Args:
  replica_grads: List of List of (grad, var) tuples.
  packing: A dict generated by pack_small_tensors describing the changes
    it made to replica_grads.

Returns:
  new_replica_grads: identical to replica_grads except that concatenations
    of small tensors have been split apart and returned to their original
    positions, paired with their original variables.
def unpack_small_tensors(replica_grads, packing):
    'Undo the structure alterations to replica_grads done by pack_small_tensors.\n\n  Args:\n    replica_grads: List of List of (grad, var) tuples.\n    packing: A dict generated by pack_small_tensors describing the changes\n      it made to replica_grads.\n\n  Returns:\n    new_replica_grads: identical to replica_grads except that concatenations\n      of small tensors have been split apart and returned to their original\n      positions, paired with their original variables.\n  '
    if (not packing):
        return replica_grads
    new_replica_grads = []
    num_devices = len(replica_grads)
    num_packed = (len(packing.keys()) // num_devices)
    for (dev_idx, gv_list) in enumerate(replica_grads):
        gv_list = list(gv_list)
        new_gv_list = gv_list[num_packed:]
        for i in range(num_packed):
            k = ('%d:%d' % (dev_idx, i))
            gpt = packing[k]
            gv = unpack_grad_tuple(gv_list[i], gpt)
            for (gi, idx) in enumerate(gpt.indices):
                assert (idx == gpt.indices[gi])
                new_gv_list.insert(idx, gv[gi])
        new_replica_grads.append(new_gv_list)
    return new_replica_grads