def batchify(instances, batch_size=32):
  """splits instances into batches, each of which contains at most batch_size"""
  batches = [instances[i:i + batch_size] if i + batch_size <= len(instances)
             else instances[i:] for i in range(0, len(instances), batch_size)]
  return batches