Skip to content

Commit 847c35f

Browse files
authored
model-conversion : add trust_remote_code for embedding scripts (#18288)
This commit adds the trust_remote_code=True parameter when loading models and configurations in the embedding model conversion scripts. It also adds a cast to float for models that might use a data type that is not supported by python, for example bfloat16. The motivation for this is that some models may require custom code to be executed during loading, and setting trust_remote_code to True avoids getting prompted for confirmation. Future work will consolidate the embedding conversion scripts with the causal conversion scripts to avoid code duplication. But in the mean time it would be nice to have this fix in place.
1 parent a6a552e commit 847c35f

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

examples/model-conversion/scripts/embedding/run-original-model.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def read_prompt_from_file(file_path):
4545
else:
4646
tokenizer = AutoTokenizer.from_pretrained(model_path)
4747

48-
config = AutoConfig.from_pretrained(model_path)
48+
config = AutoConfig.from_pretrained(model_path, trust_remote_code=True)
4949

5050
# This can be used to override the sliding window size for manual testing. This
5151
# can be useful to verify the sliding window attention mask in the original model
@@ -64,12 +64,12 @@ def read_prompt_from_file(file_path):
6464

6565
try:
6666
model_class = getattr(importlib.import_module(unreleased_module_path), class_name)
67-
model = model_class.from_pretrained(model_path, config=config)
67+
model = model_class.from_pretrained(model_path, config=config, trust_remote_code=True)
6868
except (ImportError, AttributeError) as e:
6969
print(f"Failed to import or load model: {e}")
7070
exit(1)
7171
else:
72-
model = AutoModel.from_pretrained(model_path, config=config)
72+
model = AutoModel.from_pretrained(model_path, config=config, trust_remote_code=True)
7373
print(f"Model class: {type(model)}")
7474
print(f"Model file: {type(model).__module__}")
7575

@@ -123,7 +123,7 @@ def read_prompt_from_file(file_path):
123123
outputs = model(**encoded)
124124
hidden_states = outputs.last_hidden_state # Shape: [batch_size, seq_len, hidden_size]
125125

126-
all_embeddings = hidden_states[0].cpu().numpy() # Shape: [seq_len, hidden_size]
126+
all_embeddings = hidden_states[0].float().cpu().numpy() # Shape: [seq_len, hidden_size]
127127

128128
print(f"Hidden states shape: {hidden_states.shape}")
129129
print(f"All embeddings shape: {all_embeddings.shape}")

examples/model-conversion/scripts/utils/semantic_check.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def main():
166166
# Load the python model to get configuration information and also to load the tokenizer.
167167
print("Loading model and tokenizer using AutoTokenizer:", args.model_path)
168168
tokenizer = AutoTokenizer.from_pretrained(args.model_path)
169-
config = AutoConfig.from_pretrained(args.model_path)
169+
config = AutoConfig.from_pretrained(args.model_path, trust_remote_code=True)
170170

171171
if unreleased_model_name:
172172
model_name_lower = unreleased_model_name.lower()
@@ -186,9 +186,9 @@ def main():
186186
exit(1)
187187
else:
188188
if args.causal:
189-
model = AutoModelForCausalLM.from_pretrained(args.model_path)
189+
model = AutoModelForCausalLM.from_pretrained(args.model_path, trust_remote_code=True)
190190
else:
191-
model = AutoModel.from_pretrained(args.model_path)
191+
model = AutoModel.from_pretrained(args.model_path, trust_remote_code=True)
192192

193193
encoded = tokenizer(prompt, return_tensors="pt")
194194
tokens = tokenizer.convert_ids_to_tokens(encoded['input_ids'][0])

0 commit comments

Comments
 (0)