Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions sentence_transformers/losses/AdaptiveLayerLoss.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,16 @@ def call_grow_cache(self, features: dict[str, Tensor]) -> dict[str, Tensor]:
# Restore original setting
self.transformer.auto_model.config.output_hidden_states = original_output_hidden_states

if original_output_hidden_states:
if not original_output_hidden_states:
del output["all_layer_embeddings"]

return output

def call_use_cache(self, features: dict[str, Tensor]) -> dict[str, Tensor]:
return {**self.features[self.call_idx], "token_embeddings": self.embeddings[self.call_idx][self.layer_idx]}
output = {**self.features[self.call_idx], "token_embeddings": self.embeddings[self.call_idx][self.layer_idx]}
if self.transformer.auto_model.config.output_hidden_states and "all_layer_embeddings" in features:
output["all_layer_embeddings"] = features["all_layer_embeddings"]
Comment on lines +81 to +82
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The call_use_cache method returns cached data without calling original_forward, so the input features parameter will not contain all_layer_embeddings. This check will always fail because features is the raw input to the forward method, not the output. If all_layer_embeddings needs to be included when output_hidden_states is enabled, it should be reconstructed from the cached self.embeddings data instead of reading from features.

Suggested change
if self.transformer.auto_model.config.output_hidden_states and "all_layer_embeddings" in features:
output["all_layer_embeddings"] = features["all_layer_embeddings"]
if self.transformer.auto_model.config.output_hidden_states:
# Reconstruct all_layer_embeddings from cached self.embeddings
output["all_layer_embeddings"] = self.embeddings[self.call_idx]

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.embeddings doesn't contain all of the hidden states/layer embeddings. Perhaps call_grow_cache() should also storeself.all_layer_embeddings

return output


class ForwardDecorator:
Expand Down