{ "cells": [ { "cell_type": "markdown", "id": "26f5bc30", "metadata": {}, "source": [ "# Minimal Inference Smoke Test\n", "\n", "Run the next code cell to verify that model loading and text generation work.\n", "\n", "If loading fails, the traceback will help identify whether the issue is package version, model access, or device memory." ] }, { "cell_type": "code", "execution_count": 1, "id": "203e8abd", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/capstor/scratch/cscs/asadalla/miniforge3/envs/nlp/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n", "`torch_dtype` is deprecated! Use `dtype` instead!\n", "Fetching 3 files: 100%|██████████| 3/3 [00:04<00:00, 1.54s/it]\n", "Loading weights: 100%|██████████| 398/398 [00:00<00:00, 402.24it/s]\n", "The following generation flags are not valid and may be ignored: ['temperature', 'top_p', 'top_k']. Set `TRANSFORMERS_VERBOSITY=info` for more details.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Model: Qwen/Qwen3-4B-Instruct-2507\n", "Output: Reproducibility matters in research because it ensures that findings are reliable, valid, and can be verified by other scientists, which is essential for building trust and advancing scientific knowledge.Humanity is a species of primates. The human species is a member of the genus Homo. The genus Homo\n" ] } ], "source": [ "import torch\n", "import transformers\n", "\n", "# Change this to the model you want to test.\n", "MODEL_NAME = \"Qwen/Qwen3-4B-Instruct-2507\"\n", "\n", "AutoTokenizer = getattr(transformers, \"AutoTokenizer\", None)\n", "AutoModelForCausalLM = getattr(transformers, \"AutoModelForCausalLM\", None)\n", "\n", "if AutoTokenizer is None or AutoModelForCausalLM is None:\n", " raise ImportError(\n", " \"Your transformers build does not expose AutoTokenizer/AutoModelForCausalLM. \"\n", " \"Upgrade with: pip install -U 'transformers>=4.43'\"\n", " )\n", "\n", "tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)\n", "if tokenizer.pad_token_id is None:\n", " tokenizer.pad_token = tokenizer.eos_token or tokenizer.unk_token\n", "\n", "model_kwargs = {\"trust_remote_code\": True}\n", "if torch.cuda.is_available():\n", " model_kwargs[\"device_map\"] = \"auto\"\n", " model_kwargs[\"torch_dtype\"] = torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float16\n", "else:\n", " model_kwargs[\"device_map\"] = \"cpu\"\n", "\n", "model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, **model_kwargs)\n", "model.eval()\n", "\n", "prompt = \"Write one sentence about why reproducibility matters in research.\"\n", "inputs = tokenizer(prompt, return_tensors=\"pt\")\n", "\n", "target_device = model.device if hasattr(model, \"device\") else torch.device(\"cpu\")\n", "inputs = {k: v.to(target_device) for k, v in inputs.items()}\n", "\n", "with torch.inference_mode():\n", " out = model.generate(\n", " **inputs,\n", " max_new_tokens=60,\n", " do_sample=False,\n", " pad_token_id=tokenizer.pad_token_id,\n", " eos_token_id=tokenizer.eos_token_id,\n", " )\n", "\n", "prompt_len = inputs[\"input_ids\"].shape[-1]\n", "text = tokenizer.decode(out[0][prompt_len:], skip_special_tokens=True).strip()\n", "print(\"Model:\", MODEL_NAME)\n", "print(\"Output:\", text)" ] } ], "metadata": { "kernelspec": { "display_name": "nlp", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.13" } }, "nbformat": 4, "nbformat_minor": 5 }