Skip to content
Open
Changes from all 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
23 changes: 23 additions & 0 deletions ml_mdm/language_models/call_mlx_lm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from typing import Optional
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import argparse

def call_mlx_lm(input_text: str, model_name: Optional[str] = "google/flan-t5-base") -> str:
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)

input_ids = tokenizer.encode(input_text, return_tensors="pt")
output_ids = model.generate(input_ids)

output_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
return output_text

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.

this is still using pytorch, tested by checking print(type(output_ids)) and they were still pytorch tensors.

You could essentially copy in the T5 class + loading logic from https://github.com/ml-explore/mlx-examples/blob/main/t5/t5.py


# command line arguments
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--input_text", type=str, required=True)
parser.add_argument("--model", type=str, default="google/flan-t5-base")
args = parser.parse_args()

output_text = call_mlx_lm(args.input_text, args.model)
print(output_text)