**[Bug] Fixed NoneType Slicing Error in `modeling_phi4mm.py`: “bad operand type for unary -”**

#46
by TheWW - opened

Phi-4-MM Bad Operand Type Error

Overview

When running sample_inference_phi4mm.py, an error of the form:

TypeError: bad operand type for unary -: 'NoneType'

could appear if num_logits_to_keep is None. This occurs because Python cannot evaluate [start: -num_logits_to_keep] when num_logits_to_keep has no value.

Root Cause

In the model code (typically found in modeling_phi4mm.py), there is a slice operation:

logits = self.lm_head(hidden_states[:, -num_logits_to_keep:, :])

If num_logits_to_keep is None, the unary - operator triggers the error.

Fix

Ensure num_logits_to_keep is assigned before slicing. Below is an example fix:

# filepath: ...microsoft--Phi-4-multimodal-instruct/modeling_phi4mm.py
# ...existing code...

if num_logits_to_keep is None:
    num_logits_to_keep = hidden_states.size(1)

logits = self.lm_head(hidden_states[:, -num_logits_to_keep:, :])

# ...existing code...

This ensures num_logits_to_keep always has a valid numeric value before the slice is computed. Once applied, test_phi4_multi.py should run without the "bad operand type" error.

+1 For this

Microsoft org

If you have inference errors, please double check the environ and dependencies, this is what we suggest
https://huggingface.co/microsoft/Phi-4-multimodal-instruct#requirements

Alternatively you can look at this dockerfile
https://github.com/anastasiosyal/phi4-multimodal-instruct-server/blob/main/dockerfile

generate_ids = model.generate(
**inputs,
max_new_tokens=1000,
generation_config=generation_config,
num_logits_to_keep=1
)
Hi all, you can pass the num_logits_to_keep in model.generate

@ZhaoweiWang12138 thank you life saver

Your need to confirm your account before you can post a new comment.

Sign up or log in to comment