Convert formatting to use ruff instead of yapf + isort (#26247)
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
@@ -12,14 +12,16 @@ from vllm.config import CacheConfig, VllmConfig
|
||||
from vllm.distributed import get_pp_group
|
||||
from vllm.model_executor.layers.layernorm import RMSNorm
|
||||
from vllm.model_executor.layers.quantization import QuantizationConfig
|
||||
from vllm.model_executor.models.internlm2 import (InternLM2Attention,
|
||||
InternLM2ForCausalLM,
|
||||
InternLM2MLP, InternLM2Model)
|
||||
from vllm.model_executor.models.internlm2 import (
|
||||
InternLM2Attention,
|
||||
InternLM2ForCausalLM,
|
||||
InternLM2MLP,
|
||||
InternLM2Model,
|
||||
)
|
||||
from vllm.sequence import IntermediateTensors
|
||||
|
||||
|
||||
class InternLM2VEDecoderLayer(nn.Module):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config: PretrainedConfig,
|
||||
@@ -31,8 +33,7 @@ class InternLM2VEDecoderLayer(nn.Module):
|
||||
self.hidden_size = config.hidden_size
|
||||
rope_theta = getattr(config, "rope_theta", 10000)
|
||||
rope_scaling = getattr(config, "rope_scaling", None)
|
||||
max_position_embeddings = getattr(config, "max_position_embeddings",
|
||||
8192)
|
||||
max_position_embeddings = getattr(config, "max_position_embeddings", 8192)
|
||||
self.attention = InternLM2Attention(
|
||||
hidden_size=self.hidden_size,
|
||||
num_heads=config.num_attention_heads,
|
||||
@@ -58,8 +59,7 @@ class InternLM2VEDecoderLayer(nn.Module):
|
||||
quant_config=quant_config,
|
||||
prefix=f"{prefix}.feed_forward_ve",
|
||||
)
|
||||
self.attention_norm = RMSNorm(config.hidden_size,
|
||||
eps=config.rms_norm_eps)
|
||||
self.attention_norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
||||
self.ffn_norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
||||
|
||||
def forward(
|
||||
@@ -74,8 +74,7 @@ class InternLM2VEDecoderLayer(nn.Module):
|
||||
residual = hidden_states
|
||||
hidden_states = self.attention_norm(hidden_states)
|
||||
else:
|
||||
hidden_states, residual = self.attention_norm(
|
||||
hidden_states, residual)
|
||||
hidden_states, residual = self.attention_norm(hidden_states, residual)
|
||||
hidden_states = self.attention(
|
||||
positions=positions,
|
||||
hidden_states=hidden_states,
|
||||
@@ -84,27 +83,25 @@ class InternLM2VEDecoderLayer(nn.Module):
|
||||
# Fully Connected
|
||||
hidden_states, residual = self.ffn_norm(hidden_states, residual)
|
||||
if visual_token_mask is not None and visual_token_mask.any():
|
||||
visual_token_mask = visual_token_mask.repeat(
|
||||
1, self.hidden_size).bool()
|
||||
visual_token_mask = visual_token_mask.repeat(1, self.hidden_size).bool()
|
||||
text_token_mask = ~visual_token_mask
|
||||
hidden_states[visual_token_mask] = self.feed_forward_ve(
|
||||
hidden_states[visual_token_mask].reshape(
|
||||
-1, self.hidden_size)).flatten()
|
||||
hidden_states[visual_token_mask].reshape(-1, self.hidden_size)
|
||||
).flatten()
|
||||
if text_token_mask.any():
|
||||
hidden_states[text_token_mask] = self.feed_forward(
|
||||
hidden_states[text_token_mask].reshape(
|
||||
-1, self.hidden_size)).flatten()
|
||||
hidden_states[text_token_mask].reshape(-1, self.hidden_size)
|
||||
).flatten()
|
||||
else:
|
||||
hidden_states = self.feed_forward(hidden_states)
|
||||
return hidden_states, residual
|
||||
|
||||
|
||||
class InternLM2VEModel(InternLM2Model):
|
||||
|
||||
def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
|
||||
super().__init__(vllm_config=vllm_config,
|
||||
prefix=prefix,
|
||||
layer_type=InternLM2VEDecoderLayer)
|
||||
super().__init__(
|
||||
vllm_config=vllm_config, prefix=prefix, layer_type=InternLM2VEDecoderLayer
|
||||
)
|
||||
|
||||
def forward(
|
||||
self,
|
||||
@@ -132,17 +129,15 @@ class InternLM2VEModel(InternLM2Model):
|
||||
visual_token_mask=visual_token_mask,
|
||||
)
|
||||
if not get_pp_group().is_last_rank:
|
||||
return IntermediateTensors({
|
||||
"hidden_states": hidden_states,
|
||||
"residual": residual
|
||||
})
|
||||
return IntermediateTensors(
|
||||
{"hidden_states": hidden_states, "residual": residual}
|
||||
)
|
||||
hidden_states, _ = self.norm(hidden_states, residual)
|
||||
return hidden_states
|
||||
|
||||
|
||||
class InternLM2VEForCausalLM(InternLM2ForCausalLM):
|
||||
|
||||
def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
|
||||
super().__init__(vllm_config=vllm_config,
|
||||
prefix=prefix,
|
||||
model_type=InternLM2VEModel)
|
||||
super().__init__(
|
||||
vllm_config=vllm_config, prefix=prefix, model_type=InternLM2VEModel
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user