Skip to content

fix(granite_speech): convert int to float for multiplier fields in text_config#44904

Closed
vivekvar-dl wants to merge 1 commit intohuggingface:mainfrom
vivekvar-dl:fix/granite-speech-int-to-float-44877
Closed

fix(granite_speech): convert int to float for multiplier fields in text_config#44904
vivekvar-dl wants to merge 1 commit intohuggingface:mainfrom
vivekvar-dl:fix/granite-speech-int-to-float-44877

Conversation

@vivekvar-dl
Copy link
Copy Markdown

Fix granite_speech config loading failure with int multiplier fields

Fixes #44877

Problem

Loading granite_speech configs fails with StrictDataclassFieldValidationError when multiplier fields (e.g., embedding_multiplier) are stored as integers in the Hub config but the underlying GraniteConfig expects floats:

from transformers import AutoConfig
config = AutoConfig.from_pretrained("ibm-granite/granite-4.0-1b-speech")
# ❌ StrictDataclassFieldValidationError: Field 'embedding_multiplier' expected float, got int (value: 12)

Root Cause

  • The model's config.json stores embedding_multiplier: 12 (integer, no decimal point)
  • GraniteConfig defines embedding_multiplier: float = 1.0
  • huggingface_hub's strict dataclass validation rejects the type mismatch

Solution

Convert int→float for multiplier fields before instantiating the nested text_config:

# In GraniteSpeechConfig.__post_init__()
float_fields = ["embedding_multiplier", "logits_scaling", "residual_multiplier", "attention_multiplier"]
for field in float_fields:
    if field in self.text_config and isinstance(self.text_config[field], int):
        self.text_config[field] = float(self.text_config[field])

Changes

  • File: src/transformers/models/granite_speech/configuration_granite_speech.py
  • Lines: 122-127 (added type conversion in __post_init__)

Safety

Non-breaking: Only converts when text_config is a dict AND field is an int
Preserves values: 1212.0 (semantically identical)
Minimal scope: Only affects 4 known float fields in GraniteConfig
Backward compatible: Doesn't change existing behavior for valid configs

Testing

Before:

>>> config = AutoConfig.from_pretrained("ibm-granite/granite-4.0-1b-speech")
StrictDataclassFieldValidationError: Field 'embedding_multiplier' expected float, got int (value: 12)

After:

>>> config = AutoConfig.from_pretrained("ibm-granite/granite-4.0-1b-speech")
>>> config.text_config.embedding_multiplier
12.0  # ✅ Successfully converted to float
>>> type(config.text_config.embedding_multiplier)
<class 'float'>

Why Not Fix the Hub Config?

Fixing the JSON on the Hub wouldn't help users with existing cached configs or custom configs stored with integers. This fix handles the issue at load time, making the library more robust.

Related


Checklist:

  • Fix addresses the root cause
  • Minimal, focused change
  • Backward compatible
  • Clear commit message
  • Issue linked

cc @zucchini-nlp (mentioned in original issue)

…xt_config

Fixes huggingface#44877

When loading granite_speech models (e.g., ibm-granite/granite-4.0-1b-speech),
the config loading fails with StrictDataclassFieldValidationError because
embedding_multiplier and similar fields are stored as integers in config.json
but GraniteConfig expects them as floats.

This commit adds type conversion from int to float for the following fields
before instantiating the text_config:
- embedding_multiplier
- logits_scaling
- residual_multiplier
- attention_multiplier

The conversion only happens when text_config is a dict (not an already
instantiated config object) and only for fields that exist and are ints.

Root cause: huggingface_hub's strict dataclass validation rejects type
mismatches, and config.json values are deserialized as ints when they
don't have decimal points.

Test: Verified config loads successfully with embedding_multiplier=12 (int)
      which gets converted to 12.0 (float) before validation.
@github-actions
Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: granite_speech

@vivekvar-dl vivekvar-dl deleted the fix/granite-speech-int-to-float-44877 branch March 23, 2026 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Strict config prevents loading granite_speech config

2 participants