|
| 1 | +import tempfile |
| 2 | +from pathlib import Path |
| 3 | +from typing import Iterator, List, Optional, Union |
| 4 | + |
| 5 | +import qwen_cpp._C as _C |
| 6 | + |
| 7 | + |
| 8 | +class Pipeline(_C.Pipeline): |
| 9 | + def __init__( |
| 10 | + self, model_path: str, tiktoken_path: str, *, dtype: Optional[str] = None |
| 11 | + ) -> None: |
| 12 | + if Path(model_path).is_file() and Path(tiktoken_path).is_file(): |
| 13 | + super().__init__(str(model_path), str(tiktoken_path)) |
| 14 | + else: |
| 15 | + from qwen_cpp.convert import convert |
| 16 | + |
| 17 | + if dtype is None: |
| 18 | + dtype = "q4_0" # default dtype |
| 19 | + |
| 20 | + with tempfile.NamedTemporaryFile("wb") as f: |
| 21 | + convert(f, model_path, dtype=dtype) |
| 22 | + super().__init__(f.name, str(tiktoken_path)) |
| 23 | + |
| 24 | + def chat( |
| 25 | + self, |
| 26 | + history: List[str], |
| 27 | + *, |
| 28 | + max_length: int = 2048, |
| 29 | + max_context_length: int = 512, |
| 30 | + do_sample: bool = True, |
| 31 | + top_k: int = 0, |
| 32 | + top_p: float = 0.7, |
| 33 | + temperature: float = 0.95, |
| 34 | + repetition_penalty: float = 1.0, |
| 35 | + num_threads: int = 0, |
| 36 | + stream: bool = False, |
| 37 | + ) -> Union[Iterator[str], str]: |
| 38 | + input_ids = self.tokenizer.encode_history(history, max_context_length) |
| 39 | + return self._generate( |
| 40 | + input_ids=input_ids, |
| 41 | + max_length=max_length, |
| 42 | + max_context_length=max_context_length, |
| 43 | + do_sample=do_sample, |
| 44 | + top_k=top_k, |
| 45 | + top_p=top_p, |
| 46 | + temperature=temperature, |
| 47 | + repetition_penalty=repetition_penalty, |
| 48 | + num_threads=num_threads, |
| 49 | + stream=stream, |
| 50 | + ) |
| 51 | + |
| 52 | + def _generate( |
| 53 | + self, |
| 54 | + input_ids: List[int], |
| 55 | + *, |
| 56 | + max_length: int = 2048, |
| 57 | + max_context_length: int = 512, |
| 58 | + do_sample: bool = True, |
| 59 | + top_k: int = 0, |
| 60 | + top_p: float = 0.7, |
| 61 | + temperature: float = 0.95, |
| 62 | + repetition_penalty: float = 1.0, |
| 63 | + num_threads: int = 0, |
| 64 | + stream: bool = False, |
| 65 | + ) -> Union[Iterator[str], str]: |
| 66 | + gen_config = _C.GenerationConfig( |
| 67 | + max_length=max_length, |
| 68 | + max_context_length=max_context_length, |
| 69 | + do_sample=do_sample, |
| 70 | + top_k=top_k, |
| 71 | + top_p=top_p, |
| 72 | + temperature=temperature, |
| 73 | + repetition_penalty=repetition_penalty, |
| 74 | + num_threads=num_threads, |
| 75 | + ) |
| 76 | + |
| 77 | + generate_fn = self._stream_generate if stream else self._sync_generate |
| 78 | + return generate_fn(input_ids=input_ids, gen_config=gen_config) |
| 79 | + |
| 80 | + def _stream_generate( |
| 81 | + self, input_ids: List[int], gen_config: _C.GenerationConfig |
| 82 | + ) -> Iterator[str]: |
| 83 | + input_ids = [x for x in input_ids] # make a copy |
| 84 | + n_past = 0 |
| 85 | + n_ctx = len(input_ids) |
| 86 | + |
| 87 | + token_cache = [] |
| 88 | + print_len = 0 |
| 89 | + while len(input_ids) < gen_config.max_length: |
| 90 | + next_token_id = self.model.generate_next_token( |
| 91 | + input_ids, gen_config, n_past, n_ctx |
| 92 | + ) |
| 93 | + n_past = len(input_ids) |
| 94 | + input_ids.append(next_token_id) |
| 95 | + |
| 96 | + token_cache.append(next_token_id) |
| 97 | + output = self.tokenizer.decode(token_cache) |
| 98 | + |
| 99 | + if output.endswith("\n"): |
| 100 | + yield output[print_len:] |
| 101 | + token_cache = [] |
| 102 | + print_len = 0 |
| 103 | + elif output.endswith((",", "!", ":", ";", "?", "�")): |
| 104 | + pass |
| 105 | + else: |
| 106 | + yield output[print_len:] |
| 107 | + print_len = len(output) |
| 108 | + |
| 109 | + if next_token_id in ( |
| 110 | + self.model.config.eos_token_id, |
| 111 | + self.model.config.im_start_id, |
| 112 | + self.model.config.im_end_id, |
| 113 | + ): |
| 114 | + break |
| 115 | + |
| 116 | + output = self.tokenizer.decode(token_cache) |
| 117 | + yield output[print_len:] |
| 118 | + |
| 119 | + def _sync_generate( |
| 120 | + self, input_ids: List[int], gen_config: _C.GenerationConfig |
| 121 | + ) -> str: |
| 122 | + input_ids = [x for x in input_ids] # make a copy |
| 123 | + n_past = 0 |
| 124 | + n_ctx = len(input_ids) |
| 125 | + |
| 126 | + while len(input_ids) < gen_config.max_length: |
| 127 | + next_token_id = self.model.generate_next_token( |
| 128 | + input_ids, gen_config, n_past, n_ctx |
| 129 | + ) |
| 130 | + n_past = len(input_ids) |
| 131 | + input_ids.append(next_token_id) |
| 132 | + if next_token_id in ( |
| 133 | + self.model.config.eos_token_id, |
| 134 | + self.model.config.im_start_id, |
| 135 | + self.model.config.im_end_id, |
| 136 | + ): |
| 137 | + break |
| 138 | + |
| 139 | + output = self.tokenizer.decode(input_ids[n_ctx:]) |
| 140 | + return output |
0 commit comments