Lab 9: Running and Inspecting a Pretrained Language Model with Hugging Face

Overview & Start Here

In this lab, you will run a pretrained language model and inspect how messages become model inputs and generated text. Begin with a working example, then examine tokenization, chat formatting, logits, and decoding.

Model: Qwen/Qwen2.5-0.5B-Instruct

Use a standard CPU runtime in Google Colab by default. The runtime downloads the checkpoint to its Hugging Face cache.

Timberlea JupyterHub is optional. Choose a kernel that passes the checks on page 01. The kernel needs Python 3.10 or newer, PyTorch 2.5 or newer, and Transformers 5.16.1. It reads the checkpoint from the course's shared directory at /data/ugrad/csci4152-6509-2026fall-lab9/Qwen2.5-0.5B-Instruct.

Personally, I highly recommend using Google Colab instead of Jupyter Notebook, as it can save you a lot of time and hassle. ๐Ÿซก๐Ÿซก๐Ÿซก

The lab runs on CPU and loads the weights in FP32 in either environment.

Working format: One notebook throughout the lab, with the code, outputs, and short written responses kept together.

The model has approximately 0.49 billion parameters. Its small size makes it useful for inspecting inference; the quality of its answers is not the basis for assessment.

Learning objectives

By the end of the lab, you should be able to:

  1. Explain the roles of the Hugging Face Hub, Transformers, tokenizer files, model configuration, learned weights, and default generation settings.

  2. Load a pretrained causal language model and trace text through tokenization, a chat template, and input tensors.

  3. Interpret the shape of model logits and explain how logits relate to probabilities.

  4. Generate and decode a response, distinguishing model computation from greedy decoding and sampling.

  5. Identify where the checkpoint is distributed, where your notebook runtime reads the model files, and where PyTorch runs inference.

Your route through the lab

Page

What you will do

Requirement

01 โ€” Getting Started and Running the Model

Set up your notebook environment, explore the model repository, and run pipeline().

Core: both courses

02 โ€” Inspecting the Inference Pipeline

Inspect tokens, chat formatting, input IDs, logits, and explicit generation.

Core: both courses

03 โ€” Decoding Experiments and Runtime Inspection

Compare decoding strategies, inspect the model files used by your runtime, and synthesize the workflow.

Core: both courses

04 โ€” CSCI 6509 Extension

Inspect the top-five next-token candidates; optionally explore temperature and entropy.

Required: CSCI 6509; optional: CSCI 4152

05 โ€” Optional Local Challenge

Run the same model on your own computer.

Optional: both courses

06 โ€” Troubleshooting & References

Resolve common problems and consult references.

Use as needed

CSCI 4152: Complete pages 01 โ†’ 02 โ†’ 03, then finish responses R1โ€“R6 below.

CSCI 6509: Complete pages 01 โ†’ 02 โ†’ 03 โ†’ 04, then finish R1โ€“R6 and G1.

Time and completion plan

Activity

Estimated time

Page 01: setup, download, repository, first response

25โ€“30 minutes

Page 02: inspect inputs, logits, and generation

35โ€“40 minutes

Page 03: decoding and runtime inspection

20โ€“25 minutes

Page 04: required CSCI 6509 extension

15โ€“20 minutes

Written responses R1โ€“R6; G1 for CSCI 6509

Allow a further 30โ€“40 minutes

Page 05: optional local challenge

30โ€“45 minutes, plus installation/download delays

The core experiments total approximately 80โ€“95 minutes; including the required CSCI 6509 extension gives 95โ€“115 minutes, before writing up responses. These are planning estimates: download speed and CPU runtime performance vary. The optional entropy investigation takes about another 5 minutes.

During class, prioritize running the required experiments, recording observations, and saving the notebook with its outputs. Written responses may be completed after class by the instructor's stated deadline. In a two-hour session, CSCI 6509 students should not expect to finish all experiments and written responses in class. Leave the last five minutes to save and check your work.

Start your working notebook

Make an editable copy of the starter notebook supplied by your instructor or TA. If no starter has been supplied, create a notebook named Lab9_HuggingFace_YourName in your chosen environment.

Use this one notebook for all required pages. Run the cells in order and add Markdown cells for your responses. Page changes do not create a new Python session: keep the same runtime and the variables created by earlier cells. If the runtime restarts, rerun the preceding cells.

Begin with 01 โ€” Getting Started and Running the Model.

โš ๏ธ Required written responses

The discussion checkpoints on the activity pages are prompts for observation, partner discussion, and TA check-ins. Submit the following short responses in your notebook; separate written answers to every discussion checkpoint are not required.

Unless your instructor specifies otherwise, aim for two to four sentences per core response, using your own observed outputs as evidence.

Response

What to include

R1 โ€” Model repository

Explain the roles of config.json, the tokenizer files, model.safetensors, and generation_config.json.

R2 โ€” Tokenization

Show one string you tested and its tokens or token IDs. Use it to explain why one word need not equal one token.

R3 โ€” Chat input

Identify the chat template's file/field, explain the role markers and default system message it adds, and interpret the two dimensions of your input_ids.shape.

R4 โ€” Model output

Report your outputs.logits.shape, explain its three dimensions, and describe how logits can be converted into probabilities.

R5 โ€” Decoding

Refer to your greedy and sampled outputs and Part 7's settings table. Explain which settings affect each experiment and why outputs can change while the model weights stay fixed.

R6 โ€” Execution environment

State whether you used Colab or Timberlea. Use your Part 8 output to identify where your notebook runtime read the model files and where PyTorch ran inference. Explain the roles of the Hugging Face Hub and your browser.

G1 โ€” CSCI 6509: Include a table of the top-five next-token IDs, decoded tokens, and probabilities for the Part 7 Halifax prompt. Explain whether those five probabilities sum to one and how the probabilities were obtained from logits. Identify the candidate selected by this lab's unpenalized greedy settings and include the Part 9 one-token comparison. Explain why repeated locally highest-probability choices do not necessarily yield the globally most probable or best complete response. A short paragraph plus the table and comparison output is sufficient.

The temperature/entropy investigation and Local Challenge are optional unless your instructor specifies otherwise.

Assessment and collaboration

Unless your instructor announces a different marking scheme, use the following rubric:

Component

Marks

Basis for credit

R1โ€“R6

2 each; 12 total

2: correct explanation addressing the question, with observed evidence where requested. 1: partly correct or incomplete. 0: missing or does not demonstrate the requested understanding.

G1: top-five results

1

Correct token IDs, decoded tokens, and probabilities for the specified prompt.

G1: probabilities

1

Correct explanation of softmax and why the reported five probabilities generally do not sum to one.

G1: greedy comparison

1

Correct candidate and a documented one-token comparison under the specified settings.

G1: local versus complete response

1

Correct explanation of why local choices need not maximize complete-sequence probability or response quality.

CSCI 4152: 12 marks total. CSCI 6509: 16 marks total; G1 is assessed separately from the six equally weighted core responses. The lab's contribution to the course grade follows the course syllabus. Optional activities carry no additional marks unless your instructor announces otherwise.

Collaboration: Discussion with classmates is encouraged. Each student should run and retain their own notebook and independently write R1โ€“R6 and, where required, G1 in their own words.

AI tools: Follow the course's published AI-use policy. Where that policy permits assistance, acknowledge the tool and its role, verify its suggestions against your own experiment, and submit your own explanations. If the policy does not address the intended use, ask the instructor before using AI for assessed work.

Submission / check-off

Unless your instructor specifies otherwise, prepare one completed .ipynb notebook.

  • The code and outputs for the required activity pages, including tokenization, generation, decoding comparisons, and Part 8 model-file inspection.

  • Responses R1โ€“R6, clearly labelled.

  • CSCI 6509: the next-token results and response G1.

Before submission or check-off, confirm that your notebook is saved and that the required outputs and responses are visible. Follow your instructor's directions for where and how to submit the notebook.

Assessment focuses on your explanation of the inference workflow and interpretation of the experiments. An incorrect or awkward answer from the small language model does not by itself indicate a failed lab.

Start page 01 ยท Troubleshooting & references