Count tokens and find the embedding table
CPU · your Macnotebook75 minrequiredGoal · Produce a token-count comparison across tokenizers and compute the embedding matrix's share of GPT-2 parameters.
- 1Load AutoTokenizer for gpt2 and TinyLlama/TinyLlama-1.1B-Chat-v1.0.
- 2Tokenize five texts (an English paragraph, Python code, a JSON blob, a non-English paragraph, a string of digits) with both; tabulate token counts and tokens per word.
- 3Print tokenizer.convert_ids_to_tokens for the code sample and note how indentation and leading spaces are encoded.
- 4Load gpt2 with AutoModelForCausalLM; find model.transformer.wte.weight and print its shape.
- 5Compute wte.numel() / total parameters and check whether model.lm_head.weight shares data_ptr() with wte.
- 6Pick the token for ' king' and list the 10 nearest tokens by cosine similarity of embedding rows.
- Deliverable
- A notebook with the token-count table, the parameter-share number and the nearest-neighbour list.
- Check
- GPT-2 wte has shape (50257, 768) and is 38,597,376 parameters, about 31 percent of 124,439,808; lm_head shares storage with wte.