From 45fcc4e1b348ba180db08dcecd1290ce70da446a Mon Sep 17 00:00:00 2001 From: Junst Date: Wed, 17 Sep 2025 15:16:46 +0900 Subject: [PATCH] Remove Mock Data fallback and force real attention extraction MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Remove Mock Data fallback mechanisms: - No more automatic fallback to mock data when real extraction fails - Force real attention extraction to work properly - Raise RuntimeError instead of silently using mock data 2. Enhanced hook debugging: - Add clear indicators when hooks are triggered (🎯 HOOK TRIGGERED!) - Log detailed information about captured attention outputs - Show total number of captured outputs - Debug logging for non-attention modules 3. Strict error handling: - If no attention outputs are captured, raise RuntimeError - Force debugging of hook registration and execution - No silent failures that lead to mock data usage This ensures that either real attention extraction works properly, or we get clear error messages about what's failing, preventing silent fallback to mock data. --- concept_attention.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/concept_attention.py b/concept_attention.py index ebadca4..45bf8da 100644 --- a/concept_attention.py +++ b/concept_attention.py @@ -36,8 +36,13 @@ class ConceptAttention: # Capture outputs from attention-related modules if any(keyword in module_name.lower() for keyword in ['attention', 'attn', 'query', 'key', 'value', 'proj']): self.attention_outputs[module_name] = output - logger.info(f"Captured output from {module_name}, shape: {output.shape}") - logger.info(f"Output type: {type(output)}, device: {output.device if hasattr(output, 'device') else 'N/A'}") + logger.info(f"🎯 HOOK TRIGGERED! Captured output from {module_name}") + logger.info(f" Shape: {output.shape}") + logger.info(f" Type: {type(output)}") + logger.info(f" Device: {output.device if hasattr(output, 'device') else 'N/A'}") + logger.info(f" Total attention outputs captured: {len(self.attention_outputs)}") + else: + logger.debug(f"Hook called on {module_name} but not capturing (not attention-related)") # Get the actual model from ModelPatcher actual_model = getattr(self.model, 'model', self.model) @@ -267,10 +272,9 @@ class ConceptAttentionProcessor: except Exception as e: logger.error(f"Error processing image: {e}") - # Fallback to mock data if real implementation fails - logger.info("Falling back to mock data") - saliency_maps = self._create_mock_saliency_maps(image, concepts) - return saliency_maps + # No fallback to mock data - force real implementation + logger.error("Real attention extraction failed. No mock data fallback.") + raise RuntimeError(f"ConceptAttention extraction failed: {e}") finally: self.concept_attention.cleanup_hooks() @@ -319,8 +323,9 @@ class ConceptAttentionProcessor: concept_maps = {} if not hasattr(self.concept_attention, 'attention_outputs') or not self.concept_attention.attention_outputs: - logger.warning("No attention outputs captured, creating mock maps") - return self._create_mock_saliency_maps(torch.randn(image_shape), list(concept_embeddings.keys())) + logger.error("No attention outputs captured! This means hooks are not working properly.") + logger.error("Available attention_outputs:", getattr(self.concept_attention, 'attention_outputs', 'None')) + raise RuntimeError("Failed to capture attention outputs from the model. Hooks may not be working correctly.") try: # Get the first available attention output