|
| 1 | +# coding=utf-8 |
| 2 | +# Adapted from |
| 3 | +# https://huggingface.co/Qwen/Qwen2.5-Math-RM-72B/blob/main/modeling_qwen2_rm.py |
| 4 | +# Copyright 2024 Kakao Corp. (Kanana-X Team) |
| 5 | +# Copyright 2024 The Qwen team. |
| 6 | +# Copyright 2023 The vLLM team. |
| 7 | +"""Inference-only Qwen2-Classification model compatible with HF weights.""" |
| 8 | +from typing import Iterable, List, Optional, Tuple |
| 9 | + |
| 10 | +import torch |
| 11 | +from torch import nn |
| 12 | +from transformers import Qwen2Config |
| 13 | + |
| 14 | +from vllm.attention import AttentionMetadata |
| 15 | +from vllm.config import CacheConfig, LoRAConfig |
| 16 | +from vllm.model_executor.layers.linear import RowParallelLinear |
| 17 | +from vllm.model_executor.layers.pooler import Pooler, PoolingType |
| 18 | +from vllm.model_executor.layers.quantization.base_config import ( |
| 19 | + QuantizationConfig) |
| 20 | +from vllm.model_executor.models.qwen2 import Qwen2Model |
| 21 | +from vllm.model_executor.pooling_metadata import PoolingMetadata |
| 22 | +from vllm.sequence import IntermediateTensors, PoolerOutput |
| 23 | + |
| 24 | +from .utils import AutoWeightsLoader |
| 25 | + |
| 26 | + |
| 27 | +class Qwen2ForSequenceClassification(nn.Module): |
| 28 | + packed_modules_mapping = { |
| 29 | + "qkv_proj": [ |
| 30 | + "q_proj", |
| 31 | + "k_proj", |
| 32 | + "v_proj", |
| 33 | + ], |
| 34 | + "gate_up_proj": [ |
| 35 | + "gate_proj", |
| 36 | + "up_proj", |
| 37 | + ], |
| 38 | + } |
| 39 | + |
| 40 | + # LoRA specific attributes |
| 41 | + supported_lora_modules = [ |
| 42 | + "qkv_proj", |
| 43 | + "o_proj", |
| 44 | + "gate_up_proj", |
| 45 | + "down_proj", |
| 46 | + ] |
| 47 | + embedding_modules = {} |
| 48 | + embedding_padding_modules = [] |
| 49 | + |
| 50 | + def __init__( |
| 51 | + self, |
| 52 | + config: Qwen2Config, |
| 53 | + cache_config: Optional[CacheConfig] = None, |
| 54 | + quant_config: Optional[QuantizationConfig] = None, |
| 55 | + lora_config: Optional[LoRAConfig] = None, |
| 56 | + ) -> None: |
| 57 | + # TODO (@robertgshaw2): see if this can be moved out |
| 58 | + if (cache_config.sliding_window is not None |
| 59 | + and hasattr(config, "max_window_layers")): |
| 60 | + raise ValueError("Sliding window for some but all layers is not " |
| 61 | + "supported. This model uses sliding window " |
| 62 | + "but `max_window_layers` = %s is less than " |
| 63 | + "`num_hidden_layers` = %s. Please open an issue " |
| 64 | + "to discuss this feature." % ( |
| 65 | + config.max_window_layers, |
| 66 | + config.num_hidden_layers, |
| 67 | + )) |
| 68 | + |
| 69 | + super().__init__() |
| 70 | + |
| 71 | + self.config = config |
| 72 | + self.lora_config = lora_config |
| 73 | + |
| 74 | + self.quant_config = quant_config |
| 75 | + self.model = Qwen2Model(config, cache_config, quant_config) |
| 76 | + |
| 77 | + self.score = RowParallelLinear(config.hidden_size, |
| 78 | + config.num_labels, |
| 79 | + quant_config=quant_config) |
| 80 | + self._pooler = Pooler(pooling_type=PoolingType.LAST, |
| 81 | + normalize=False, |
| 82 | + softmax=True) |
| 83 | + |
| 84 | + def forward( |
| 85 | + self, |
| 86 | + input_ids: torch.Tensor, |
| 87 | + positions: torch.Tensor, |
| 88 | + kv_caches: List[torch.Tensor], |
| 89 | + attn_metadata: AttentionMetadata, |
| 90 | + intermediate_tensors: Optional[IntermediateTensors] = None, |
| 91 | + ) -> torch.Tensor: |
| 92 | + hidden_states = self.model(input_ids, positions, kv_caches, |
| 93 | + attn_metadata, intermediate_tensors) |
| 94 | + logits, _ = self.score(hidden_states) |
| 95 | + return logits |
| 96 | + |
| 97 | + def pooler( |
| 98 | + self, |
| 99 | + hidden_states: torch.Tensor, |
| 100 | + pooling_metadata: PoolingMetadata, |
| 101 | + ) -> Optional[PoolerOutput]: |
| 102 | + return self._pooler(hidden_states, pooling_metadata) |
| 103 | + |
| 104 | + def load_weights(self, weights: Iterable[Tuple[str, torch.Tensor]]): |
| 105 | + loader = AutoWeightsLoader(self, |
| 106 | + ignore_unexpected_prefixes=["lm_head."]) |
| 107 | + loader.load_weights(weights) |
0 commit comments