def nmt_lm_fusion_translate(FLAGS):
    GlobalNames.USE_GPU = FLAGS.use_gpu
    config_path = os.path.abspath(FLAGS.config_path)
    with open(config_path.strip()) as f:
        configs = yaml.load(f)
    data_configs = configs['data_configs']
    nmt_model_configs = configs['nmt_model_configs']
    lm_model_configs = configs['lm_model_configs']
    timer = Timer()
    INFO('Loading data...')
    timer.tic()
    vocab_src = Vocabulary(**data_configs['vocabularies'][0])
    vocab_tgt = Vocabulary(**data_configs['vocabularies'][1])
    valid_dataset = TextLineDataset(data_path=FLAGS.source_path, vocabulary=vocab_src)
    valid_iterator = DataIterator(dataset=valid_dataset, batch_size=FLAGS.batch_size, use_bucket=True, buffer_size=100000, numbering=True)
    INFO('Done. Elapsed time {0}'.format(timer.toc()))
    INFO('Building model...')
    timer.tic()
    nmt_model_path = FLAGS.nmt_model_path
    lm_model_path = FLAGS.lm_model_path
    nmt_model = build_model(n_src_vocab=vocab_src.max_n_words, n_tgt_vocab=vocab_tgt.max_n_words, **nmt_model_configs)
    lm_model = build_model(n_words=vocab_tgt.max_n_words, **lm_model_configs)
    nmt_model.eval()
    lm_model.eval()
    INFO('Done. Elapsed time {0}'.format(timer.toc()))
    INFO('Reloading model parameters...')
    timer.tic()
    nmt_params = load_model_parameters(nmt_model_path, map_location='cpu')
    lm_params = load_model_parameters(lm_model_path, map_location='cpu')
    nmt_model.load_state_dict(nmt_params)
    lm_model.load_state_dict(lm_params)
    if GlobalNames.USE_GPU:
        nmt_model.cuda()
        lm_model.cuda()
    INFO('Done. Elapsed time {0}'.format(timer.toc()))
    INFO('Begin...')
    result_numbers = []
    result = []
    n_words = 0
    timer.tic()
    infer_progress_bar = tqdm(total=len(valid_iterator), desc=' - (Infer)  ', unit='sents')
    valid_iter = valid_iterator.build_generator()
    for batch in valid_iter:
        (numbers, seqs_x) = batch
        batch_size_t = len(seqs_x)
        x = prepare_data(seqs_x=seqs_x, cuda=GlobalNames.USE_GPU)
        with torch.no_grad():
            word_ids = nmt_lm_fusion_beam_search(nmt_model=nmt_model, lm_model=lm_model, beam_size=FLAGS.beam_size, max_steps=FLAGS.max_steps, src_seqs=x, alpha=FLAGS.alpha, beta=FLAGS.beta)
        word_ids = word_ids.cpu().numpy().tolist()
        result_numbers += numbers
        for sent_t in word_ids:
            sent_t = [[wid for wid in line if (wid != PAD)] for line in sent_t]
            result.append(sent_t)
            n_words += len(sent_t[0])
        infer_progress_bar.update(batch_size_t)
    infer_progress_bar.close()
    INFO('Done. Speed: {0:.2f} words/sec'.format((n_words / timer.toc(return_seconds=True))))
    translation = []
    for sent in result:
        samples = []
        for trans in sent:
            sample = []
            for w in trans:
                if (w == vocab_tgt.EOS):
                    break
                sample.append(vocab_tgt.id2token(w))
            samples.append(vocab_tgt.tokenizer.detokenize(sample))
        translation.append(samples)
    origin_order = np.argsort(result_numbers).tolist()
    translation = [translation[ii] for ii in origin_order]
    with open(FLAGS.saveto, 'w') as f:
        for trans in translation:
            f.write(('%s\n' % trans[0]))
def nmt_lm_fusion_translate(FLAGS):
    GlobalNames.USE_GPU = FLAGS.use_gpu
    config_path = os.path.abspath(FLAGS.config_path)
    with open(config_path.strip()) as f:
        configs = yaml.load(f)
    data_configs = configs['data_configs']
    nmt_model_configs = configs['nmt_model_configs']
    lm_model_configs = configs['lm_model_configs']
    timer = Timer()
    INFO('Loading data...')
    timer.tic()
    vocab_src = Vocabulary(**data_configs['vocabularies'][0])
    vocab_tgt = Vocabulary(**data_configs['vocabularies'][1])
    valid_dataset = TextLineDataset(data_path=FLAGS.source_path, vocabulary=vocab_src)
    valid_iterator = DataIterator(dataset=valid_dataset, batch_size=FLAGS.batch_size, use_bucket=True, buffer_size=100000, numbering=True)
    INFO('Done. Elapsed time {0}'.format(timer.toc()))
    INFO('Building model...')
    timer.tic()
    nmt_model_path = FLAGS.nmt_model_path
    lm_model_path = FLAGS.lm_model_path
    nmt_model = build_model(n_src_vocab=vocab_src.max_n_words, n_tgt_vocab=vocab_tgt.max_n_words, **nmt_model_configs)
    lm_model = build_model(n_words=vocab_tgt.max_n_words, **lm_model_configs)
    nmt_model.eval()
    lm_model.eval()
    INFO('Done. Elapsed time {0}'.format(timer.toc()))
    INFO('Reloading model parameters...')
    timer.tic()
    nmt_params = load_model_parameters(nmt_model_path, map_location='cpu')
    lm_params = load_model_parameters(lm_model_path, map_location='cpu')
    nmt_model.load_state_dict(nmt_params)
    lm_model.load_state_dict(lm_params)
    if GlobalNames.USE_GPU:
        nmt_model.cuda()
        lm_model.cuda()
    INFO('Done. Elapsed time {0}'.format(timer.toc()))
    INFO('Begin...')
    result_numbers = []
    result = []
    n_words = 0
    timer.tic()
    infer_progress_bar = tqdm(total=len(valid_iterator), desc=' - (Infer)  ', unit='sents')
    valid_iter = valid_iterator.build_generator()
    for batch in valid_iter:
        (numbers, seqs_x) = batch
        batch_size_t = len(seqs_x)
        x = prepare_data(seqs_x=seqs_x, cuda=GlobalNames.USE_GPU)
        with torch.no_grad():
            word_ids = nmt_lm_fusion_beam_search(nmt_model=nmt_model, lm_model=lm_model, beam_size=FLAGS.beam_size, max_steps=FLAGS.max_steps, src_seqs=x, alpha=FLAGS.alpha, beta=FLAGS.beta)
        word_ids = word_ids.cpu().numpy().tolist()
        result_numbers += numbers
        for sent_t in word_ids:
            sent_t = [[wid for wid in line if (wid != PAD)] for line in sent_t]
            result.append(sent_t)
            n_words += len(sent_t[0])
        infer_progress_bar.update(batch_size_t)
    infer_progress_bar.close()
    INFO('Done. Speed: {0:.2f} words/sec'.format((n_words / timer.toc(return_seconds=True))))
    translation = []
    for sent in result:
        samples = []
        for trans in sent:
            sample = []
            for w in trans:
                if (w == vocab_tgt.EOS):
                    break
                sample.append(vocab_tgt.id2token(w))
            samples.append(vocab_tgt.tokenizer.detokenize(sample))
        translation.append(samples)
    origin_order = np.argsort(result_numbers).tolist()
    translation = [translation[ii] for ii in origin_order]
    with open(FLAGS.saveto, 'w') as f:
        for trans in translation:
            f.write(('%s\n' % trans[0]))