Cache sub-objects in __getitem__ to ensure identity stability (#22184)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
ef2d4013d7
commit
494bb86169
@@ -601,7 +601,12 @@ class GenerateReqInput(BaseReq):
|
||||
raise ValueError("Session params must be a dict or a list of dicts.")
|
||||
|
||||
def __getitem__(self, i):
|
||||
return GenerateReqInput(
|
||||
# Cache sub-objects so that repeated obj[i] calls return the same instance.
|
||||
# This avoids subtle bugs where different call sites get divergent objects.
|
||||
cache = self.__dict__.setdefault("_sub_obj_cache", {})
|
||||
if i in cache:
|
||||
return cache[i]
|
||||
sub = GenerateReqInput(
|
||||
text=self.text[i] if self.text is not None else None,
|
||||
input_ids=self.input_ids[i] if self.input_ids is not None else None,
|
||||
input_embeds=(
|
||||
@@ -665,6 +670,8 @@ class GenerateReqInput(BaseReq):
|
||||
http_worker_ipc=self.http_worker_ipc,
|
||||
received_time=self.received_time,
|
||||
)
|
||||
cache[i] = sub
|
||||
return sub
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -890,8 +897,13 @@ class EmbeddingReqInput(BaseReq):
|
||||
)
|
||||
|
||||
def __getitem__(self, i):
|
||||
# Cache sub-objects so that repeated obj[i] calls return the same instance.
|
||||
cache = self.__dict__.setdefault("_sub_obj_cache", {})
|
||||
if i in cache:
|
||||
return cache[i]
|
||||
|
||||
if self.is_cross_encoder_request:
|
||||
return EmbeddingReqInput(
|
||||
sub = EmbeddingReqInput(
|
||||
text=[self.text[i]] if self.text is not None else None,
|
||||
sampling_params=self.sampling_params[i],
|
||||
rid=self.rid[i],
|
||||
@@ -900,22 +912,24 @@ class EmbeddingReqInput(BaseReq):
|
||||
is_cross_encoder_request=True,
|
||||
http_worker_ipc=self.http_worker_ipc,
|
||||
)
|
||||
|
||||
return EmbeddingReqInput(
|
||||
text=self.text[i] if self.text is not None else None,
|
||||
input_ids=self.input_ids[i] if self.input_ids is not None else None,
|
||||
image_data=self.image_data[i] if self.image_data is not None else None,
|
||||
audio_data=self.audio_data[i] if self.audio_data is not None else None,
|
||||
video_data=self.video_data[i] if self.video_data is not None else None,
|
||||
sampling_params=self.sampling_params[i],
|
||||
rid=self.rid[i],
|
||||
lora_path=self.lora_path[i] if self.lora_path is not None else None,
|
||||
lora_id=self.lora_id[i] if self.lora_id is not None else None,
|
||||
external_trace_header=self.external_trace_header,
|
||||
dimensions=self.dimensions,
|
||||
http_worker_ipc=self.http_worker_ipc,
|
||||
received_time=self.received_time,
|
||||
)
|
||||
else:
|
||||
sub = EmbeddingReqInput(
|
||||
text=self.text[i] if self.text is not None else None,
|
||||
input_ids=self.input_ids[i] if self.input_ids is not None else None,
|
||||
image_data=self.image_data[i] if self.image_data is not None else None,
|
||||
audio_data=self.audio_data[i] if self.audio_data is not None else None,
|
||||
video_data=self.video_data[i] if self.video_data is not None else None,
|
||||
sampling_params=self.sampling_params[i],
|
||||
rid=self.rid[i],
|
||||
lora_path=self.lora_path[i] if self.lora_path is not None else None,
|
||||
lora_id=self.lora_id[i] if self.lora_id is not None else None,
|
||||
external_trace_header=self.external_trace_header,
|
||||
dimensions=self.dimensions,
|
||||
http_worker_ipc=self.http_worker_ipc,
|
||||
received_time=self.received_time,
|
||||
)
|
||||
cache[i] = sub
|
||||
return sub
|
||||
|
||||
|
||||
@dataclass
|
||||
|
||||
@@ -2335,6 +2335,11 @@ class TokenizerManager(TokenizerCommunicatorMixin, TokenizerManagerScoreMixin):
|
||||
|
||||
# Look up the LoRA ID from the registry and start tracking ongoing LoRA requests.
|
||||
obj.lora_id = await self.lora_registry.acquire(obj.lora_path)
|
||||
# Propagate lora_id to any sub-objects already cached by __getitem__.
|
||||
for i, sub_obj in obj.__dict__.get("_sub_obj_cache", {}).items():
|
||||
sub_obj.lora_id = (
|
||||
obj.lora_id[i] if isinstance(obj.lora_id, list) else obj.lora_id
|
||||
)
|
||||
|
||||
def _req_stats_init(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user