Esempio n. 1
0
def init_context_word_embs_sentence_model(model_path, model_type, device, force_reload=False, 
    min_length=100, max_length=300, batch_size=32, temperature=1.0, top_k=50, top_p=0.9, 
    silence=True, use_custom_api=True):

    global CONTEXT_WORD_EMBS_SENTENCE_MODELS

    model_name = '_'.join([os.path.basename(model_path), str(device)])
    if model_name in CONTEXT_WORD_EMBS_SENTENCE_MODELS and not force_reload:
        CONTEXT_WORD_EMBS_SENTENCE_MODELS[model_name].min_length = min_length
        CONTEXT_WORD_EMBS_SENTENCE_MODELS[model_name].max_length = max_length
        CONTEXT_WORD_EMBS_SENTENCE_MODELS[model_name].temperature = temperature
        CONTEXT_WORD_EMBS_SENTENCE_MODELS[model_name].top_k = top_k
        CONTEXT_WORD_EMBS_SENTENCE_MODELS[model_name].top_p = top_p
        CONTEXT_WORD_EMBS_SENTENCE_MODELS[model_name].batch_size = batch_size
        CONTEXT_WORD_EMBS_SENTENCE_MODELS[model_name].silence = silence
        return CONTEXT_WORD_EMBS_SENTENCE_MODELS[model_name]

    if use_custom_api:
        if model_type == 'xlnet':
            model = nml.XlNet(model_path, device=device, temperature=temperature, top_k=top_k, 
                              optimize=None, silence=True)
        elif model_type == 'gpt2':
            model = nml.Gpt2(model_path, device=device, temperature=temperature, top_k=top_k, 
                             optimize=None, silence=True)
        else:
            raise ValueError('Model name value is unexpected. Only support XLNet and GPT2 model.')
    else:
        model = nml.TextGenTransformers(model_path, device=device, min_length=min_length, max_length=max_length, 
            temperature=temperature, top_k=top_k, top_p=top_p, batch_size=batch_size)

    CONTEXT_WORD_EMBS_SENTENCE_MODELS[model_name] = model
    return model
Esempio n. 2
0
def init_context_word_embs_sentence_model(model_path, device, force_reload=False, temperature=1.0, top_k=None,
                                          top_p=None, optimize=None, silence=True):
    global CONTEXT_WORD_EMBS_SENTENCE_MODELS

    model_name = os.path.basename(model_path)
    if model_name in CONTEXT_WORD_EMBS_SENTENCE_MODELS and not force_reload:
        CONTEXT_WORD_EMBS_SENTENCE_MODELS[model_name].device = device
        CONTEXT_WORD_EMBS_SENTENCE_MODELS[model_name].temperature = temperature
        CONTEXT_WORD_EMBS_SENTENCE_MODELS[model_name].top_k = top_k
        CONTEXT_WORD_EMBS_SENTENCE_MODELS[model_name].top_p = top_p
        CONTEXT_WORD_EMBS_SENTENCE_MODELS[model_name].optimize = optimize
        CONTEXT_WORD_EMBS_SENTENCE_MODELS[model_name].silence = silence
        return CONTEXT_WORD_EMBS_SENTENCE_MODELS[model_name]

    if 'xlnet' in model_path:
        model = nml.XlNet(model_path, device=device, temperature=temperature, top_k=top_k, top_p=top_p,
                          optimize=optimize, silence=True)
    elif 'gpt2' in model_path:
        model = nml.Gpt2(model_path, device=device, temperature=temperature, top_k=top_k, top_p=top_p,
                         optimize=optimize, silence=True)
    else:
        raise ValueError('Model name value is unexpected. Only support XLNet and GPT2 model.')

    CONTEXT_WORD_EMBS_SENTENCE_MODELS[model_name] = model
    return model
Esempio n. 3
0
def init_gpt2_model(model_path, device, force_reload=False):
    # Load model once at runtime
    global GPT2_MODEL
    if GPT2_MODEL and not force_reload:
        return GPT2_MODEL

    gpt2_model = nml.Gpt2(model_path, device=device)
    gpt2_model.model.eval()
    GPT2_MODEL = gpt2_model

    return gpt2_model
Esempio n. 4
0
def init_gpt2_model(model_path,
                    device,
                    force_reload=False,
                    temperature=1.0,
                    top_k=None,
                    top_p=None):
    # Load model once at runtime
    global GPT2_MODEL
    if GPT2_MODEL and not force_reload:
        GPT2_MODEL.temperature = temperature
        GPT2_MODEL.top_k = top_k
        GPT2_MODEL.top_p = top_p
        return GPT2_MODEL

    gpt2_model = nml.Gpt2(model_path,
                          device=device,
                          temperature=temperature,
                          top_k=top_k,
                          top_p=top_p)
    gpt2_model.model.eval()
    GPT2_MODEL = gpt2_model

    return gpt2_model