48 lines
1.7 KiB
Python
48 lines
1.7 KiB
Python
# Copyright 2023-2024 SGLang Team
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
# ==============================================================================
|
|
"""Qwen3 Reward Model for RLHF and best-of-N sampling."""
|
|
|
|
from typing import Optional
|
|
|
|
from torch import nn
|
|
from transformers import Qwen2Config # Qwen3 uses Qwen2Config
|
|
|
|
from sglang.srt.layers.pooler import Pooler, PoolingType
|
|
from sglang.srt.layers.quantization.base_config import QuantizationConfig
|
|
from sglang.srt.models.qwen3_classification import Qwen3ForPooledOutput
|
|
|
|
|
|
class Qwen3ForRewardModel(Qwen3ForPooledOutput):
|
|
"""Qwen3 Reward Model with 2-layer MLP scoring head for RLHF."""
|
|
|
|
def __init__(
|
|
self,
|
|
config: Qwen2Config,
|
|
quant_config: Optional[QuantizationConfig] = None,
|
|
prefix: str = "",
|
|
) -> None:
|
|
super().__init__(config, quant_config, prefix)
|
|
self.num_labels = 1
|
|
self.score = nn.Sequential(
|
|
nn.Linear(config.hidden_size, config.hidden_size),
|
|
nn.ReLU(),
|
|
nn.Linear(config.hidden_size, self.num_labels),
|
|
)
|
|
self.pooler = Pooler(pooling_type=PoolingType.LAST, normalize=False)
|
|
|
|
|
|
EntryClass = [
|
|
Qwen3ForRewardModel,
|
|
]
|