Ubuntu
tests
5ee43e9
import argparse
import logging
import time
import torch
from transformers import AutoTokenizer, ConvBertForSequenceClassification
import torch_neuronx # ensure Neuron backend is available
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def main():
parser = argparse.ArgumentParser(description="Run ConvBERT on Neuron")
parser.add_argument(
"--model",
type=str,
default="YituTech/conv-bert-base",
help="ConvBERT 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 and model
tokenizer = AutoTokenizer.from_pretrained(args.model)
model = ConvBertForSequenceClassification.from_pretrained(
args.model, torch_dtype=torch.float32, attn_implementation="eager"
)
model.eval()
# Tokenize sample text
text = "ConvBERT combines self-attention and lightweight convolutions."
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
# Pre-run once to fix shapes before compilation
with torch.no_grad():
logits = model(**inputs).logits
# Compile forward pass
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
# Actual run
run_start = time.time()
with torch.no_grad():
logits = model(**inputs).logits
run_time = time.time() - run_start
# Decode result
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()
"""
<unknown>:0: error: failed to legalize operation 'torch.constant.int'
<unknown>:0: note: see current operation: %0 = "torch.constant.int"() <{value = 9 : i64}> : () -> !torch.int
"""