| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | import uuid |
| 5 | |
| 6 | import torch |
| 7 | |
| 8 | MASK_64_BITS = (1 << 64) - 1 |
| 9 | |
| 10 | |
| 11 | def random_uuid() -> str: |
| 12 | return f"{uuid.uuid4().int & MASK_64_BITS:016x}" # 16 hex chars |
| 13 | |
| 14 | |
| 15 | def length_from_prompt_token_ids_or_embeds( |
| 16 | prompt_token_ids: list[int] | torch.Tensor | None, |
| 17 | prompt_embeds: torch.Tensor | None, |
| 18 | ) -> int: |
| 19 | """Calculate the request length (in number of tokens) give either |
| 20 | prompt_token_ids or prompt_embeds. |
| 21 | """ |
| 22 | prompt_token_len = None if prompt_token_ids is None else len(prompt_token_ids) |
| 23 | prompt_embeds_len = None if prompt_embeds is None else len(prompt_embeds) |
| 24 | |
| 25 | if prompt_token_len is None: |
| 26 | if prompt_embeds_len is None: |
| 27 | raise ValueError("Neither prompt_token_ids nor prompt_embeds were defined.") |
| 28 | return prompt_embeds_len |
| 29 | else: |
| 30 | if prompt_embeds_len is not None and prompt_embeds_len != prompt_token_len: |
| 31 | raise ValueError( |
| 32 | "Prompt token ids and prompt embeds had different lengths" |
| 33 | f" prompt_token_ids={prompt_token_len}" |
| 34 | f" prompt_embeds={prompt_embeds_len}" |
| 35 | ) |
| 36 | return prompt_token_len |
| 37 | |
| 38 | |
| 39 | def is_moe_layer(module: torch.nn.Module) -> bool: |
| 40 | # TODO(bnell): Should use isinstance but can't due to circular dependencies. |
| 41 | def _check_bases(cls): |
| 42 | if cls.__name__ == "MoERunnerInterface": |
| 43 | return True |
| 44 | |
| 45 | for b in cls.__bases__: |
| 46 | if _check_bases(b): |
| 47 | return True |
| 48 | |
| 49 | return _check_bases(module.__class__) |