Ubuntu
tests
5ee43e9
#!/usr/bin/env python3
# MobileBERT text-classification on Neuron
import argparse
import logging
import time
import torch
from transformers import AutoTokenizer, MobileBertForSequenceClassification
import torch_neuronx # ensures Neuron backend
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def main():
parser = argparse.ArgumentParser(description="Run MobileBERT on Neuron")
parser.add_argument(
"--model",
type=str,
default="google/mobilebert-uncased",
help="MobileBERT model name on Hugging Face Hub",
)
parser.add_argument("--batch-size", type=int, default=1, help="Batch size")
args = parser.parse_args()
torch.set_default_dtype(torch.float32)
torch.manual_seed(42)
# load tokenizer & model
tokenizer = AutoTokenizer.from_pretrained(args.model)
model = MobileBertForSequenceClassification.from_pretrained(
args.model, torch_dtype=torch.float32, attn_implementation="eager"
).eval()
# tokenize sample
text = "MobileBERT is a compact BERT for on-device NLP."
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
# pre-run to lock shapes
with torch.no_grad():
_ = model(**inputs).logits
# compile
model.forward = torch.compile(model.forward, backend="neuron", fullgraph=True)
# warmup
warmup_start = time.time()
with torch.no_grad():
_ = model(**inputs)
warmup_time = time.time() - warmup_start
# benchmark run
run_start = time.time()
with torch.no_grad():
logits = model(**inputs).logits
run_time = time.time() - run_start
# top-1 label
predicted_class_id = logits.argmax().item()
predicted_label = model.config.id2label[predicted_class_id]
logger.info("Warmup: %.2f s, Run: %.4f s", warmup_time, run_time)
logger.info("Predicted label: %s", predicted_label)
if __name__ == "__main__":
main()