|
| 1 | +import os |
| 2 | +import signal |
| 3 | +import time |
| 4 | +from . import util |
| 5 | +from typing import List, Optional, Union |
| 6 | + |
| 7 | +from xllm_export import (LLMMaster, Options, RequestOutput, |
| 8 | + RequestParams) |
| 9 | + |
| 10 | +class Embedding: |
| 11 | + def __init__( |
| 12 | + self, |
| 13 | + model: str, |
| 14 | + devices: str = 'auto', |
| 15 | + block_size: int = 128, |
| 16 | + max_cache_size: int = 0, |
| 17 | + max_memory_utilization: float = 0.9, |
| 18 | + disable_prefix_cache: bool = False, |
| 19 | + max_tokens_per_batch: int = 20000, |
| 20 | + max_seqs_per_batch: int = 256, |
| 21 | + max_tokens_per_chunk_for_prefill: int = 512, |
| 22 | + num_request_handling_threads: int = 4, |
| 23 | + communication_backend: str = 'lccl', |
| 24 | + rank_tablefile: str = '', |
| 25 | + expert_parallel_degree: int = 0, |
| 26 | + enable_mla: bool = False, |
| 27 | + disable_chunked_prefill: bool = False, |
| 28 | + instance_role: str = 'DEFAULT', |
| 29 | + nnodes: int = 1, |
| 30 | + node_rank: int = 0, |
| 31 | + dp_size: int = 1, |
| 32 | + ep_size: int = 1, |
| 33 | + enable_shm: bool = False, |
| 34 | + is_local: bool = True, |
| 35 | + **kwargs, |
| 36 | + ) -> None: |
| 37 | + if not os.path.exists(model): |
| 38 | + raise ValueError(f"model {model} not exists") |
| 39 | + |
| 40 | + options = Options() |
| 41 | + options.model_path = model |
| 42 | + options.task_type = "embed" |
| 43 | + options.devices = devices |
| 44 | + options.draft_model_path = None |
| 45 | + options.draft_devices = None |
| 46 | + options.block_size = block_size |
| 47 | + options.max_cache_size = max_cache_size |
| 48 | + options.max_memory_utilization = max_memory_utilization |
| 49 | + if disable_prefix_cache: |
| 50 | + options.enable_prefix_cache = False |
| 51 | + else: |
| 52 | + options.enable_prefix_cache = True |
| 53 | + options.max_tokens_per_batch = max_tokens_per_batch |
| 54 | + options.max_seqs_per_batch = max_seqs_per_batch |
| 55 | + options.max_tokens_per_chunk_for_prefill = max_tokens_per_chunk_for_prefill |
| 56 | + options.num_request_handling_threads = num_request_handling_threads |
| 57 | + options.communication_backend = communication_backend |
| 58 | + options.rank_tablefile = rank_tablefile |
| 59 | + options.expert_parallel_degree = expert_parallel_degree |
| 60 | + options.enable_mla = enable_mla |
| 61 | + if disable_chunked_prefill: |
| 62 | + options.enable_chunked_prefill = False |
| 63 | + else: |
| 64 | + options.enable_chunked_prefill = True |
| 65 | + free_port = util.get_free_port() |
| 66 | + options.master_node_addr = "127.0.0.1:" + str(free_port) |
| 67 | + options.nnodes = nnodes |
| 68 | + options.node_rank = node_rank |
| 69 | + options.dp_size = dp_size |
| 70 | + options.ep_size = ep_size |
| 71 | + options.enable_disagg_pd = False |
| 72 | + options.enable_schedule_overlap = False |
| 73 | + options.enable_offline_inference = True |
| 74 | + options.spawn_worker_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 75 | + options.enable_shm = enable_shm |
| 76 | + options.is_local = is_local |
| 77 | + self.master = LLMMaster(options) |
| 78 | + |
| 79 | + def finish(self): |
| 80 | + try: |
| 81 | + #os.kill(os.getpid(), signal.SIGTERM) |
| 82 | + os.kill(os.getpid(), signal.SIGKILL) |
| 83 | + except Exception as e: |
| 84 | + pass |
| 85 | + |
| 86 | + def embedding( |
| 87 | + self, |
| 88 | + inputs: Union[str, List[str]], |
| 89 | + request_params: Optional[Union[RequestParams, List[RequestParams]]] = None, |
| 90 | + wait_schedule_done: bool = True, |
| 91 | + ) -> List[RequestOutput]: |
| 92 | + if request_params is None: |
| 93 | + request_params = RequestParams() |
| 94 | + if isinstance(inputs, str): |
| 95 | + inputs = [inputs] |
| 96 | + if isinstance(request_params, RequestParams): |
| 97 | + request_params.is_embeddings = True |
| 98 | + request_params = [request_params] |
| 99 | + else: |
| 100 | + for i in range(len(request_params)): |
| 101 | + request_params[i].is_embeddings = True |
| 102 | + |
| 103 | + outputs = [None] * len(inputs) |
| 104 | + def callback(index: int, output: RequestOutput) -> bool: |
| 105 | + outputs[index] = output |
| 106 | + return True |
| 107 | + |
| 108 | + # schedule all requests |
| 109 | + self.master.handle_batch_request( |
| 110 | + inputs, request_params, callback |
| 111 | + ) |
| 112 | + |
| 113 | + # TODO: add wait later |
| 114 | + if wait_schedule_done: |
| 115 | + pass |
| 116 | + |
| 117 | + # generate |
| 118 | + self.master.generate() |
| 119 | + |
| 120 | + # wait async output |
| 121 | + for i in range(len(outputs)): |
| 122 | + while outputs[i] is None: |
| 123 | + time.sleep(0.01) |
| 124 | + if outputs[i].status is not None and not outputs[i].status.ok: |
| 125 | + raise ValidationError(outputs[i].status.code, outputs[i].status.message) |
| 126 | + outputs[i].prompt = inputs[i] |
| 127 | + |
| 128 | + return outputs |
0 commit comments