api.py 3.59 KB
Newer Older
mshoeybi's avatar
working  
mshoeybi committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# coding=utf-8
# Copyright (c) 2020, NVIDIA CORPORATION.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Inference API."""


import torch

mshoeybi's avatar
mshoeybi committed
21
from megatron import mpu
mshoeybi's avatar
working  
mshoeybi committed
22
23
from .communication import broadcast_float_list
from .generation import generate_tokens_probs_and_return_on_first_stage
mshoeybi's avatar
mshoeybi committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from .tokenization import (
    tokenize_prompts,
    detokenize_generations)


def generate_and_post_process(model,
                              prompts=None,
                              tokens_to_generate=0,
                              return_output_log_probs=False,
                              return_all_log_probs=False,
                              temperature=1.0,
                              add_BOS=False):
    """TO DO ..."""

    # Main inference.
    tokens, lengths, output_log_probs, all_log_probs = generate(
        model,
        prompts=prompts,
        tokens_to_generate=tokens_to_generate,
        return_output_log_probs=return_output_log_probs,
        return_all_log_probs=return_all_log_probs,
        temperature=temperature,
        add_BOS=add_BOS)

    # Only post-process on first stage.
    if mpu.is_pipeline_first_stage():

        tokens, prompts_plus_generations, prompts_plus_generations_segments = \
            detokenize_generations(tokens, lengths, True)

        if return_output_log_probs:
            output_log_probs = output_log_probs.cpu().numpy().tolist()
        if return_all_log_probs:
            all_log_probs = all_log_probs.cpu().numpy() #.tolist()

        return prompts_plus_generations, prompts_plus_generations_segments, \
            output_log_probs, all_log_probs, tokens

    return None
mshoeybi's avatar
working  
mshoeybi committed
63
64
65
66
67
68
69


def generate(model,
             prompts=None,
             tokens_to_generate=0,
             return_output_log_probs=False,
             return_all_log_probs=False,
mshoeybi's avatar
mshoeybi committed
70
71
             temperature=1.0,
             add_BOS=False):
mshoeybi's avatar
working  
mshoeybi committed
72
73
74
75
    """TO DO ..."""

    # Make sure input params are avaialble to all ranks.
    values = [tokens_to_generate, return_output_log_probs,
mshoeybi's avatar
mshoeybi committed
76
77
              return_all_log_probs, temperature, add_BOS]
    values_float_tensor = broadcast_float_list(5, float_list=values)
mshoeybi's avatar
working  
mshoeybi committed
78
79
80
    tokens_to_generate = int(values_float_tensor[0].item())
    return_output_log_probs = bool(values_float_tensor[1].item())
    return_all_log_probs = bool(values_float_tensor[2].item())
mshoeybi's avatar
mshoeybi committed
81
82
    temperature = values_float_tensor[3].item()
    add_BOS = bool(values_float_tensor[4].item())
mshoeybi's avatar
working  
mshoeybi committed
83
84
85
86
87

    # Tokenize prompts and get the batch.
    # Note that these tensors are broadcaseted to all ranks.
    if torch.distributed.get_rank() == 0:
        assert prompts is not None
mshoeybi's avatar
mshoeybi committed
88
        assert tokens_to_generate > 0
mshoeybi's avatar
working  
mshoeybi committed
89
    context_tokens_tensor, context_length_tensor = tokenize_prompts(
mshoeybi's avatar
mshoeybi committed
90
        prompts=prompts, tokens_to_generate=tokens_to_generate, add_BOS=add_BOS)
mshoeybi's avatar
working  
mshoeybi committed
91
92
93
94
95
96
97
98

    # Main inference function.
    # Note that the outputs are available on the first stage.
    return generate_tokens_probs_and_return_on_first_stage(
        model, context_tokens_tensor, context_length_tensor,
        return_output_log_probs=return_output_log_probs,
        return_all_log_probs=return_all_log_probs,
        temperature=temperature)