human
Complete this python program:
def get_gpus(num_gpu=1):
assistant
#Here is the Python program you requested.

    """Returns list of free GPUs according to nvidia-smi
    """
    try:
        list_gpus = subprocess.check_output(['nvidia-smi', '--list-gpus']).decode()
        logging.debug('all    GPUs:\n{0}'.format(list_gpus))
        gpus = [x for x in list_gpus.split('\n') if (len(x) > 0)]
        def parse_gpu(gpu_str):
            cols = gpu_str.split('    ')
            return (cols[5].split(')')[0], cols[1].split(':')[0])
        gpu_list = [parse_gpu(gpu) for gpu in gpus]
        random.shuffle(gpu_list)
        free_gpus = []
        retries = 0
        while ((len(free_gpus) < num_gpu) and (retries < MAX_RETRIES)):
            smi_output = subprocess.check_output(['nvidia-smi', '--format=csv,noheader,nounits', '--query-compute-apps=gpu_uuid']).decode()
            logging.debug('busy    GPUs:\n{0}'.format(smi_output))
            busy_uuids = [x for x in smi_output.split('\n') if (len(x) > 0)]
            for (uuid, index) in gpu_list:
                if (not (uuid in busy_uuids)):
                    free_gpus.append(index)
            if (len(free_gpus) < num_gpu):
                logging.warn('Unable    to    find    available    GPUs:    requested={0},    available={1}'.format(num_gpu, len(free_gpus)))
                retries += 1
                time.sleep((30 * retries))
                free_gpus = []
        if (len(free_gpus) < num_gpu):
            smi_output = subprocess.check_output(['nvidia-smi', '--format=csv', '--query-compute-apps=gpu_uuid,pid,process_name,used_gpu_memory']).decode()
            logging.info(':    {0}'.format(smi_output))
            raise Exception('Unable    to    find    free    GPU:\n{0}'.format(smi_output))
        return ','.join(free_gpus[:num_gpu])
    except subprocess.CalledProcessError as e:
        print('nvidia-smi    error', e.output)