{"slug": "is-transformers-using-gpu-by-default", "title": "Is Transformers using GPU by default?", "summary": "Hugging Face Transformers models loaded with AutoModelForSequenceClassification.from_pretrained() start on the CPU, not the GPU, so the model must be moved to \"cuda:0\" once and each tokenized batch moved to the same device, according to a Hugging Face documentation-based walkthrough. For 10,000 sentences, the walkthrough recommends starting with batches of 32 and measuring sentences per second, since short one-at-a-time predictions make a one-minute GPU utilization average hard to interpret. If utilization still looks low, printing model.device and inputs[\"input_ids\"].device inside the loop should both show cuda:0.", "body_md": "With `AutoModelForSequenceClassification.from_pretrained()` as shown above, the model starts on the CPU. Move it to `cuda:0` once, then move each batch of tokenized inputs to the same device.\n\nFor 10,000 sentences, I’d start with batches of 32 and measure sentences per second. Short, one-at-a-time predictions can make a one-minute GPU utilization average hard to interpret. [Hugging Face](https://huggingface.co/docs/transformers/pipeline_tutorial?utm_source=chatgpt.com)\n\n``` python\nimport torchmodel = model.to(\"cuda:0\").eval()batch_size = 32predictions = []with torch.inference_mode():    for start in range(0, len(sentences), batch_size):        batch = sentences[start : start + batch_size]        inputs = tokenizer(            batch, padding=True, truncation=True, return_tensors=\"pt\"        ).to(model.device)        label_ids = model(**inputs).logits.argmax(dim=-1).cpu().tolist()        predictions.extend(model.config.id2label[i] for i in label_ids)\n```\n\nIf utilization still looks low, print `model.device` and `inputs[\"input_ids\"].device` inside the loop. Both should show `cuda:0`.", "url": "https://wpnews.pro/news/is-transformers-using-gpu-by-default", "canonical_source": "https://discuss.huggingface.co/t/is-transformers-using-gpu-by-default/8500#post_9", "published_at": "2026-12-04 04:12:39+00:00", "updated_at": "2026-12-04 04:14:38.826548+00:00", "lang": "en", "topics": ["artificial-intelligence", "machine-learning", "natural-language-processing", "ai-infrastructure"], "entities": ["Hugging Face", "Transformers", "AutoModelForSequenceClassification", "PyTorch"], "also_reported_by": [], "takeaway": "Move the model to \"cuda:0\" once with model.to(\"cuda:0\").eval() and send each tokenized batch to model.device, batching 10,000 sentences at 32 per batch and measuring sentences per second rather than a one-minute GPU utilization average.", "recommendation": {"advice": "Move the model to cuda:0 once and each tokenized batch to model.device, then batch 32 sentences at a time and measure sentences per second instead of one-minute GPU utilization.", "playbook": "inference-serving", "playbook_url": "https://wpnews.pro/playbooks/inference-serving"}, "alternates": {"html": "https://wpnews.pro/news/is-transformers-using-gpu-by-default", "markdown": "https://wpnews.pro/news/is-transformers-using-gpu-by-default.md", "text": "https://wpnews.pro/news/is-transformers-using-gpu-by-default.txt", "jsonld": "https://wpnews.pro/news/is-transformers-using-gpu-by-default.jsonld"}}