|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +from abc import ABC, abstractmethod |
| 8 | +from typing import List, Optional, TypedDict |
| 9 | + |
| 10 | +import torch |
| 11 | + |
| 12 | +from executorch.extension.llm.tokenizer.utils import get_tokenizer |
| 13 | +from executorch.examples.models.llama.runner.generation import LlamaRunner, next_token, sample_top_p |
| 14 | + |
| 15 | + |
| 16 | +class TorchTuneLlamaRunner(LlamaRunner): |
| 17 | + def __init__( |
| 18 | + self, |
| 19 | + tokenizer_path: str, |
| 20 | + max_seq_len: int, |
| 21 | + max_batch_size: int, |
| 22 | + use_kv_cache: bool, |
| 23 | + vocab_size: int, |
| 24 | + device: str = "cpu", |
| 25 | + ): |
| 26 | + super().__init__( |
| 27 | + tokenizer_path, |
| 28 | + max_seq_len, |
| 29 | + max_batch_size, |
| 30 | + use_kv_cache, |
| 31 | + vocab_size, |
| 32 | + device, |
| 33 | + ) |
| 34 | + |
| 35 | + self.causal_mask = torch.tril( |
| 36 | + torch.ones( |
| 37 | + size=(max_seq_len, max_seq_len), |
| 38 | + dtype=torch.bool, |
| 39 | + ) |
| 40 | + ) |
| 41 | + self.input_pos = torch.arange(max_seq_len) |
| 42 | + |
| 43 | + def generate( # noqa: C901 |
| 44 | + self, |
| 45 | + prompt_tokens: List[int], |
| 46 | + max_seq_len: int, |
| 47 | + temperature: float = 0.8, |
| 48 | + top_p: float = 0.9, |
| 49 | + echo: bool = False, |
| 50 | + ) -> List[int]: |
| 51 | + # Prefill |
| 52 | + seq_len = len(prompt_tokens) |
| 53 | + input_pos = self.input_pos[None, :seq_len] |
| 54 | + mask = self.causal_mask[None, :seq_len] |
| 55 | + if self.use_kv_cache: |
| 56 | + logits = self.forward( |
| 57 | + tokens=torch.tensor([prompt_tokens], dtype=torch.long, device=self.device), |
| 58 | + input_pos=input_pos, |
| 59 | + mask=mask, |
| 60 | + ) |
| 61 | + else: |
| 62 | + logits = self.forward( |
| 63 | + tokens=torch.tensor([prompt_tokens], dtype=torch.long, device=self.device), |
| 64 | + ) |
| 65 | + |
| 66 | + # Only need the last logit. |
| 67 | + current_token = next_token(logits[:, -1, :], temperature, top_p) |
| 68 | + print(f"{self.tokenizer.decode_token(current_token)}", end="", flush=True) |
| 69 | + tokens = prompt_tokens + [current_token] |
| 70 | + |
| 71 | + while len(tokens) < max_seq_len: |
| 72 | + mask = self.causal_mask[None, seq_len, None, :] |
| 73 | + input_pos = self.input_pos[None, seq_len, None] |
| 74 | + if self.use_kv_cache: |
| 75 | + logits = self.forward( |
| 76 | + tokens=torch.tensor( |
| 77 | + [[current_token]], dtype=torch.long, device=self.device |
| 78 | + ), |
| 79 | + input_pos=input_pos, |
| 80 | + mask=mask, |
| 81 | + ) |
| 82 | + else: |
| 83 | + logits = self.forward( |
| 84 | + tokens=torch.tensor([tokens], dtype=torch.long, device=self.device), |
| 85 | + ) |
| 86 | + |
| 87 | + # Only need the last logit. |
| 88 | + current_token = next_token(logits[:, -1, :], temperature, top_p) |
| 89 | + tokens.append(current_token) |
| 90 | + |
| 91 | + if current_token == self.tokenizer.eos_id or ( |
| 92 | + hasattr(self.tokenizer, "stop_tokens") |
| 93 | + and current_token in self.tokenizer.stop_tokens |
| 94 | + ): |
| 95 | + break |
| 96 | + |
| 97 | + print(f"{self.tokenizer.decode_token(current_token)}", end="", flush=True) |
| 98 | + seq_len += 1 |
| 99 | + |
| 100 | + return tokens if echo else tokens[len(prompt_tokens) :] |
| 101 | + |
0 commit comments