Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 7 additions & 1 deletion src/transformers/modeling_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3047,9 +3047,14 @@ def _load_pretrained_model(
offload_state_dict = True

is_sharded_safetensors = is_safetensors and sharded_metadata is not None

# tie the model weights before retrieving the state_dict
model.tie_weights()

# Retrieve missing & unexpected_keys
model_state_dict = model.state_dict()
expected_keys = list(model_state_dict.keys())

Comment thread
patrickvonplaten marked this conversation as resolved.
Outdated
prefix = model.base_model_prefix

def _fix_key(key):
Expand Down Expand Up @@ -3092,7 +3097,6 @@ def _fix_key(key):
model_buffers = {".".join([prefix, key]) for key in model_buffers}
unexpected_keys = list(unexpected_keys - model_buffers)

model.tie_weights()
if device_map is None:
ptrs = collections.defaultdict(list)
for name, tensor in model.state_dict().items():
Expand Down Expand Up @@ -3223,6 +3227,7 @@ def _find_mismatched_keys(
folder = os.path.sep.join(resolved_archive_file[0].split(os.path.sep)[:-1])
else:
folder = None

Comment thread
patrickvonplaten marked this conversation as resolved.
Outdated
if device_map is not None and is_safetensors:
param_device_map = expand_device_map(device_map, original_loaded_keys)

Expand Down Expand Up @@ -3280,6 +3285,7 @@ def _find_mismatched_keys(

if len(resolved_archive_file) > 1:
resolved_archive_file = logging.tqdm(resolved_archive_file, desc="Loading checkpoint shards")

Comment thread
patrickvonplaten marked this conversation as resolved.
Outdated
for shard_file in resolved_archive_file:
# Skip the load for shards that only contain disk-offloaded weights when using safetensors for the offload.
if shard_file in disk_only_shard_files:
Expand Down
34 changes: 34 additions & 0 deletions tests/models/wav2vec2/test_modeling_wav2vec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,40 @@ def get_logits(model, input_features):

self.assertTrue(torch.allclose(logits, logits_2, atol=1e-3))

def test_load_and_new_target_lang_adapter(self):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

might be good to add a short comment to explain a bit what this test does, as this is for a very special situation.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

even just a link to issue and this PR page is fine for me.

Comment thread
patrickvonplaten marked this conversation as resolved.
Outdated
processor = Wav2Vec2Processor.from_pretrained(
"hf-internal-testing/tiny-random-wav2vec2", return_attention_mask=True
)

def get_logits(model, input_features):
model = model.to(torch_device)
batch = processor(
input_features,
padding=True,
sampling_rate=processor.feature_extractor.sampling_rate,
return_tensors="pt",
)

with torch.no_grad():
logits = model(
input_values=batch["input_values"].to(torch_device),
attention_mask=batch["attention_mask"].to(torch_device),
).logits
return logits

input_features = [np.random.random(16_000 * s) for s in [1, 3, 2, 6]]

model = Wav2Vec2ForCTC.from_pretrained("hf-internal-testing/tiny-random-wav2vec2-adapter", target_lang="fr", ignore_mismatched_sizes=True)

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.

better test! The other tests didn't check for mismatched vocab sizes actually. This should solve it


logits = get_logits(model, input_features)

model_2 = Wav2Vec2ForCTC.from_pretrained("hf-internal-testing/tiny-random-wav2vec2-adapter")
model_2.load_adapter("fr")

logits_2 = get_logits(model_2, input_features)

self.assertTrue(torch.allclose(logits, logits_2, atol=1e-3))

def test_load_attn_adapter(self):
processor = Wav2Vec2Processor.from_pretrained(
"hf-internal-testing/tiny-random-wav2vec2", return_attention_mask=True
Expand Down