| 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # This source code is licensed under the terms described in the LICENSE file in |
| 5 | # the root directory of this source tree. |
| 6 | |
| 7 | import os |
| 8 | from copy import deepcopy |
| 9 | from dataclasses import dataclass |
| 10 | from functools import partial |
| 11 | from typing import Generator, List, Optional |
| 12 | |
| 13 | from llama_models.llama3.api.chat_format import ChatFormat |
| 14 | from llama_models.llama3.api.datatypes import Message, ToolPromptFormat |
| 15 | from llama_models.llama3.api.tokenizer import Tokenizer |
| 16 | from llama_models.sku_list import resolve_model |
| 17 | |
| 18 | from .config import MetaReferenceImplConfig |
| 19 | from .generation import Llama, model_checkpoint_dir |
| 20 | from .parallel_utils import ModelParallelProcessGroup |
| 21 | |
| 22 | |
| 23 | @dataclass |
| 24 | class InferenceArgs: |
| 25 | messages: List[Message] |
| 26 | temperature: float |
| 27 | top_p: float |
| 28 | max_gen_len: int |
| 29 | logprobs: bool |
| 30 | tool_prompt_format: ToolPromptFormat |
| 31 | |
| 32 | |
| 33 | class ModelRunner: |
| 34 | def __init__(self, llama): |
| 35 | self.llama = llama |
| 36 | |
| 37 | # the `task` object is the same that is sent to `ModelParallelProcessGroup.run_inference()` |
| 38 | def __call__(self, task: InferenceArgs): |
| 39 | return self.llama.chat_completion( |
| 40 | task.messages, |
| 41 | task.temperature, |
| 42 | task.top_p, |
| 43 | task.max_gen_len, |
| 44 | task.logprobs, |
| 45 | task.tool_prompt_format, |
| 46 | ) |
| 47 | |
| 48 | |
| 49 | def init_model_cb(config: MetaReferenceImplConfig): |
| 50 | llama = Llama.build(config) |
| 51 | return ModelRunner(llama) |
| 52 | |
| 53 | |
| 54 | class LlamaModelParallelGenerator: |
| 55 | """ |
| 56 | This abstraction exists so |
| 57 | - we can run model parallel code without needing to run the CLIs via torchrun |
| 58 | - this also enables use model parallel code within a notebook context. |
| 59 | |
| 60 | A Context Manager is used to ensure that the model parallel process is started and stopped |
| 61 | correctly. This does make the ergonomics a little awkward, because it isn't immediately |
| 62 | clear at the callsite why we need to use a context manager. |
| 63 | """ |
| 64 | |
| 65 | def __init__(self, config: MetaReferenceImplConfig): |
| 66 | self.config = config |
| 67 | self.model = resolve_model(self.config.model) |
| 68 | # this is a hack because Agent's loop uses this to tokenize and check if input is too long |
| 69 | # while the tool-use loop is going |
| 70 | checkpoint_dir = model_checkpoint_dir(self.model) |
| 71 | tokenizer_path = os.path.join(checkpoint_dir, "tokenizer.model") |
| 72 | self.formatter = ChatFormat(Tokenizer(tokenizer_path)) |
| 73 | |
| 74 | def start(self): |
| 75 | self.__enter__() |
| 76 | |
| 77 | def stop(self): |
| 78 | self.__exit__(None, None, None) |
| 79 | |
| 80 | def __enter__(self): |
| 81 | self.group = ModelParallelProcessGroup( |
| 82 | self.config.model_parallel_size, |
| 83 | init_model_cb=partial(init_model_cb, self.config), |
| 84 | ) |
| 85 | self.group.start() |
| 86 | return self |
| 87 | |
| 88 | def __exit__(self, exc_type, exc_value, exc_traceback): |
| 89 | self.group.stop() |
| 90 | |
| 91 | def chat_completion( |
| 92 | self, |
| 93 | messages: List[Message], |
| 94 | temperature: float = 0.6, |
| 95 | top_p: float = 0.9, |
| 96 | max_gen_len: Optional[int] = None, |
| 97 | logprobs: bool = False, |
| 98 | tool_prompt_format: ToolPromptFormat = ToolPromptFormat.json, |
| 99 | ) -> Generator: |
| 100 | req_obj = InferenceArgs( |
| 101 | messages=deepcopy(messages), |
| 102 | temperature=temperature, |
| 103 | top_p=top_p, |
| 104 | max_gen_len=max_gen_len, |
| 105 | logprobs=logprobs, |
| 106 | tool_prompt_format=tool_prompt_format, |
| 107 | ) |
| 108 | |
| 109 | gen = self.group.run_inference(req_obj) |
| 110 | yield from gen |