Skip to content

Commit

Permalink
[tokenizer] fixes trust_remote_code issues for DJL converter (#3569)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanktliu authored Dec 30, 2024
1 parent 37d3ced commit 88b03ef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ def save_rust_model(self, model_info, args: Namespace, temp_dir: str,
if not os.path.exists(temp_dir):
os.makedirs(temp_dir)

tokenizer = AutoTokenizer.from_pretrained(model_id)
tokenizer = AutoTokenizer.from_pretrained(
model_id, trust_remote_code=args.trust_remote_code)
include_types = config.model_type not in [
"distilbert", "mistral", "qwen2", "gemma2"
]
Expand Down Expand Up @@ -233,7 +234,7 @@ def save_pytorch_model(self, model_info, args: Namespace, temp_dir: str,
os.makedirs(temp_dir)

try:
hf_pipeline = self.load_model(model_id)
hf_pipeline = self.load_model(model_id, args.trust_remote_code)
except Exception as e:
logging.warning(f"Failed to load model: {model_id}.")
logging.warning(e, exc_info=True)
Expand Down Expand Up @@ -436,11 +437,12 @@ def verify_jit_output(self, hf_pipeline, encoding, out):

return True, None

def load_model(self, model_id: str):
def load_model(self, model_id: str, trust_remote_code: bool):
logging.info(f"Loading model: {model_id} ...")
kwargs = {
"tokenizer": model_id,
"device": -1 # always use CPU to trace the model
"device": -1, # always use CPU to trace the model
"trust_remote_code": trust_remote_code
}
return pipeline(task=self.task,
model=model_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ def __init__(self):
self.inputs = "This is an example sentence"
self.outputs = 0

def load_model(self, model_id: str):
def load_model(self, model_id: str, trust_remote_code: bool):
logging.info(f"Loading model: {model_id} ...")
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModel.from_pretrained(model_id)
tokenizer = AutoTokenizer.from_pretrained(
model_id, trust_remote_code=trust_remote_code)

model = AutoModel.from_pretrained(model_id,
trust_remote_code=trust_remote_code)

return PipelineHolder(tokenizer, model)

Expand Down Expand Up @@ -78,7 +81,8 @@ def get_extra_arguments(self, hf_pipeline, model_id: str,
if hasattr(hf_pipeline.model, "config"):
config = hf_pipeline.model.config
else:
config = AutoConfig.from_pretrained(model_id)
config = AutoConfig.from_pretrained(
model_id, trust_remote_code=trust_remote_code)
tokenizer = hf_pipeline.tokenizer
if hasattr(config, "max_position_embeddings") and hasattr(
tokenizer, "model_max_length"):
Expand Down

0 comments on commit 88b03ef

Please sign in to comment.