Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
24 changes: 24 additions & 0 deletions src/transformers/modeling_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,15 @@ def _load_state_dict_into_model(model_to_load, state_dict, start_prefix):
new_key = None
if "gamma" in key:
new_key = key.replace("gamma", "weight")
logger.warning(
"A parameter name that contains `gamma` will be renamed internally. Please use a different name "
"to suppress this warning."
)
if "beta" in key:
logger.warning(
"A parameter name that contains `beta` will be renamed internally. Please use a different name "
"to suppress this warning."
)
new_key = key.replace("beta", "bias")
if new_key:
old_keys.append(key)
Expand Down Expand Up @@ -807,8 +815,16 @@ def _load_state_dict_into_meta_model(
for key in state_dict.keys():
new_key = None
if "gamma" in key:
logger.warning(
"A parameter name that contains `gamma` will be renamed internally. Please use a different name "
"to suppress this warning."
)
new_key = key.replace("gamma", "weight")
if "beta" in key:
logger.warning(
"A parameter name that contains `beta` will be renamed internally. Please use a different name "
"to suppress this warning."
)
new_key = key.replace("beta", "bias")
if new_key:
old_keys.append(key)
Expand Down Expand Up @@ -3980,8 +3996,16 @@ def _load_pretrained_model(

def _fix_key(key):
if "beta" in key:
logger.warning(
"A parameter name that contains `beta` will be renamed internally. Please use a different name "
"to suppress this warning."
)
return key.replace("beta", "bias")
if "gamma" in key:
logger.warning(
"A parameter name that contains `gamma` will be renamed internally. Please use a different name "
"to suppress this warning."
)
return key.replace("gamma", "weight")
Comment thread
OmarManzoor marked this conversation as resolved.
return key

Expand Down
43 changes: 43 additions & 0 deletions tests/utils/test_modeling_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,49 @@ def test_model_from_pretrained_from_mlx(self):
outputs_from_saved = new_model(input_ids)
self.assertTrue(torch.allclose(outputs_from_saved["logits"], outputs["logits"]))

def test_warning_for_beta_gamma_parameters(self):
class TestModelGamma(PreTrainedModel):
def __init__(self, config):
super().__init__(config)
self.gamma_param = nn.Parameter(torch.ones(10))
self.post_init()

def forward(self):
return self.gamma_param.sum()

logger = logging.get_logger("transformers.modeling_utils")
config = PretrainedConfig()
warning_msg_gamma = "A parameter name that contains `gamma` will be renamed internally"
model = TestModelGamma(config)
Comment on lines 1514 to 1515

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

More importantly, we should check that the parameter is renamed as well

@OmarManzoor OmarManzoor Jul 8, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I tried this out and it seems that the parameter is not renamed at all. Basically when we load the model using from_pretrained it seems that the parameter is still present with the name gamma_param.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It shouldn't rename the value in the model, but will rename the value in the state_dict, I believe. Could you dive into the loading logic and verify what's happening?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I tried updating the tests. Could you kindly have a look?


with tempfile.TemporaryDirectory() as tmp_dir:
model.save_pretrained(tmp_dir)
with LoggingLevel(logging.WARNING):
with CaptureLogger(logger) as cl1:
TestModelGamma.from_pretrained(tmp_dir, config=config)

self.assertIn(warning_msg_gamma, cl1.out)

class TestModelBeta(PreTrainedModel):
def __init__(self, config):
super().__init__(config)
self.beta_param = nn.Parameter(torch.ones(10))
self.post_init()

def forward(self):
return self.beta_param.sum()

warning_msg_beta = "A parameter name that contains `beta` will be renamed internally"
model = TestModelBeta(config)

with tempfile.TemporaryDirectory() as tmp_dir:
model.save_pretrained(tmp_dir)
with LoggingLevel(logging.WARNING):
with CaptureLogger(logger) as cl2:
TestModelBeta.from_pretrained(tmp_dir, config=config)

self.assertIn(warning_msg_beta, cl2.out)


@slow
@require_torch
Expand Down