gary-boon Claude commited on
Commit
9e42df9
·
1 Parent(s): 070f9b8

Fix: Handle single-element tuple outputs in layer hook

Browse files

- Fixed "tuple index out of range" when output has only one element
- Properly checks tuple length before accessing output[1:]
- Handles all output format cases correctly

The previous fix assumed output tuples always had multiple elements,
but some layers return single-element tuples.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (1) hide show
  1. backend/model_service.py +16 -10
backend/model_service.py CHANGED
@@ -303,18 +303,24 @@ class ModelManager:
303
  # The output is (hidden_states, optional_attention_weights, ...)
304
  # We want to pass the input hidden states as if the layer did nothing
305
 
306
- if isinstance(input, tuple):
307
- # Return input hidden states, but keep any additional outputs from the layer
308
- # This maintains compatibility with the expected output format
309
- if isinstance(output, tuple) and len(output) > 1:
310
- # Keep attention weights etc. from output if present
311
- return (input[0],) + output[1:]
 
 
 
 
 
 
312
  else:
313
- # Just return the input hidden states
314
- return input[0]
315
  else:
316
- # Simple tensor input/output
317
- return input
318
  return hook
319
 
320
  # Apply hooks and log what's being disabled
 
303
  # The output is (hidden_states, optional_attention_weights, ...)
304
  # We want to pass the input hidden states as if the layer did nothing
305
 
306
+ # Get the input hidden states
307
+ if isinstance(input, tuple) and len(input) > 0:
308
+ input_hidden_states = input[0]
309
+ else:
310
+ input_hidden_states = input
311
+
312
+ # Return in the same format as the output
313
+ if isinstance(output, tuple):
314
+ # Check if there are additional elements to preserve
315
+ if len(output) > 1:
316
+ # Keep any additional outputs (like attention weights)
317
+ return (input_hidden_states,) + output[1:]
318
  else:
319
+ # Output is a single-element tuple, return the same
320
+ return (input_hidden_states,)
321
  else:
322
+ # Output is a plain tensor, return input as plain tensor
323
+ return input_hidden_states
324
  return hook
325
 
326
  # Apply hooks and log what's being disabled