11.3.4 Relations, functions, and validation
This part of Discrete math 1 problem set: sets, counting, proofs, induction, relations, bijections develops the worked mechanisms below.
Concepts from other sections (12)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Partial order: a relation that is reflexive, antisymmetric ( and imply ), and transitive, such as or “is a subset of”; some pairs may be incomparable, which is why dependency graphs need a topological sort. Read more: Chapter 11: Discrete Math 1
- Injective: no two inputs share an output; for a dict, no value appears twice. Read more: 11.2 Injective, surjective, bijective: the GPT-2 byte map
- GPT-2 byte map: the bijection from the 256 bytes to printable code points that gives byte tokens their strings. Read more: 7.1 The tokenizer protocol and the char tokenizer in AI Engineering
- roofline: the bound on a loop’s speed from peak compute , memory bandwidth , and arithmetic intensity . Read more: 22.3 Cache-blocked, packed, batch-invariant matmul in C in AI Engineering
- GQA / MLA: attention variants that shrink the KV cache by sharing K/V heads across groups of query heads, or by compressing K/V into a low-rank latent. Read more: 5.3 Model landscape and lifecycle in AI Engineering
- Inclusion-exclusion: counting a union by alternately adding and subtracting the sizes of intersections. Read more: 12.5 Discrete math 2 problem set, part b: trees, birthday bounds, MinHash and LSH, Bloom filters, recurrences
- online softmax: computing the maximum and the normalizer in one read by rescaling the running sum whenever the maximum grows. Read more: 22.4 Softmax in C: three-pass and online two-pass in AI Engineering
- PCG32: an LCG whose output is a permutation (xorshift, then a state-chosen rotation) of the old state. Read more: 12.3 Modular arithmetic, hashing, and PCG32 in Python
- SplitMix64: the mixer used to derive child seeds that depend only on the seed and the purpose. Read more: 12.3 Modular arithmetic, hashing, and PCG32 in Python
- Padding mask: a per-step boolean that freezes a finished sequence’s state and zeros its output. Read more: 9.2 LSTM (torch gate order) in AI Engineering
- Fully masked row: a row of all , defined to give softmax zeros and LSE . Read more: 15.2 Stable numerics: logsumexp, softmax, and compensated sums
- Byte map: GPT-2’s fixed bijection from the 256 byte values to 256 visible, non-whitespace characters. Read more: 11.2 Injective, surjective, bijective: the GPT-2 byte map
Learning objectives
- Check relation properties and prove function/bijection claims with explicit domains.
Relations
Section titled “Relations”q34. Let on . Is (a) reflexive, (b) symmetric, (c) transitive? [bool]
Answer
(a) True, (b) true, (c) true. (a) All of are in . (b) The only non-diagonal pairs and come together. (c) Every chain stays inside or inside , and both blocks are fully related. is the equivalence relation with classes and .
q35. How many equivalence relations are there on a 4-element set? [number]
Answer
. Equivalence relations on a set are its partitions, so the answer is the Bell number . Count partitions of by block sizes: (1 way), (4), (3), (6), (1), total 15. Common mistakes: 16 counts subsets, and 24 counts orderings.
q36. Prove: for any function , the relation is an equivalence relation on . (Unicode normalization in The tokenizer protocol and the char tokenizer is this relation with .) [proof]
Answer
Claim: for any function , the relation is an equivalence relation on .
Method: check the three defining properties directly; each reduces to the same property of equality in .
Reflexive: for every , , so .
Symmetric: if then , so , so .
Transitive: if and then and , so , so .
So is an equivalence relation; its classes are the nonempty preimages .
Model proof also in q36.md.
q37. Prove: divisibility ( when for some integer ) is a partial order on the positive integers. [proof]
Answer
Claim: on the positive integers, (there is an integer with ) is a partial order: reflexive, antisymmetric, and transitive.
Method: check each property from the definition.
Reflexive: , so .
Antisymmetric: suppose and , so and for integers . Then , and since , . Both and are positive because , and the only positive integers with product 1 are . So .
Transitive: if and , then with an integer, so .
So divisibility is a partial order on the positive integers. Antisymmetry needs positivity: on all integers and but .
Model proof also in q37.md.
Functions and bijections
Section titled “Functions and bijections”q38. Let , . Is (a) injective, (b) surjective? [bool]
Answer
(a) True, (b) false. (a) gives . (b) is always odd, so no even integer (for example 0) is an image. Injective and surjective are the two halves of the bijection test in Injective, surjective, bijective: the GPT-2 byte map, and they are independent.
q39. (a) How many bijections are there from a 5-element set to itself? (b) How many injective functions are there from a 3-element set to a 5-element set? [number]
Answer
(a) , (b) . (a) A bijection of a 5-set is an ordering: . (b) The first element has 5 images, the next 4, the last 3: . Common mistakes: and count every function, and forgets that which element goes where matters.
q40. Prove: if and are bijections, then is a bijection. [proof]
Answer
Claim: if and are bijections, then , , is a bijection.
Method: prove injective and surjective separately.
Injective: suppose . Since is injective, ; since is injective, .
Surjective: let . Since is surjective there is with ; since is surjective there is with . Then .
Being injective and surjective, is a bijection (its inverse is ).
Model proof also in q40.md.
q41. GPT-2’s byte-to-character map (Injective, surjective, bijective: the GPT-2 byte map) keeps the 188 printable bytes as themselves () and sends the other 68 bytes, in increasing order, to . Prove that is injective, so it is a bijection onto its image. [proof]
Answer
Claim: the GPT-2 byte map is injective on .
Setup: has elements, so its complement has elements. List in increasing order as ; then for and .
Method: take bytes and show , in three cases that cover every pair.
Both in : .
Both in : , with (the listing has no repeats), so .
One in each, say , : .
So is injective. A function is always surjective onto its image, so is a bijection from the 256 bytes onto its 256 image characters, and unicode_to_bytes is its inverse.
Model proof also in q41.md.
What can go wrong
Section titled “What can go wrong”The common wrong answers discussed under the problems come from a small number of recurring mistakes. Each has a symptom you would see in real code, and a habit that prevents it:
| Pitfall | Symptom | How to avoid it |
|---|---|---|
| Combining masks with OR, or forgetting the padding key | a pad position gets attention weight | intersect the two masks (AND) and clear the pad column (q4) |
| Negating only the inner predicate of a nested quantifier | one tests “some entry is 0” instead of “some row is all 0” | swap every quantifier, outermost first, then negate the predicate (q5) |
| Counting multiply-adds instead of FLOPs | FLOP and roofline numbers off by 2 | count a multiplication and an addition separately: per output entry (q12) |
| Using (forward only) for training compute | a training budget 3 times too small | use : 2 for the forward pass, 4 for the backward (q13) |
| Caching keys only, or sizing the cache by query heads | KV memory off by 2 or by the GQA group size | write the factor 2 for keys and values and use , not (q14) |
| Adding overlapping cases twice | inclusion-exclusion overcount | subtract the intersection once (q16) |
| Checking many cases instead of proving | a “for every ” claim believed from to | look for one counterexample, , instead of checking cases (q23) |
| Using the conclusion inside the inductive step | a circular proof | state the hypothesis for and use it only to derive the case (q26 to q30) |
| Forgetting the running sum must be rescaled when the max changes | online softmax overflows or is wrong after a new max | multiply by on every step, so every term is shifted by the current max (q30, q33) |
| Confusing all functions with injective ones | where was asked | count images one element at a time without reuse: (q39) |
Where this leads
Section titled “Where this leads”The tools of this set are used directly in code throughout the book and in AI Engineering:
- Graphs, DAGs, and an iterative topological sort orders the autograd graph; its correctness proof is an induction over the traversal (q20 of the first discrete math 2 problem set).
- Modular arithmetic, hashing, and PCG32 builds PCG32 and SplitMix64 from counting bits, rotations, and arithmetic modulo .
- Counting: parameters, FLOPs, KV bytes, and memory plans turns q9 to q15 into code:
param_count,flops_per_token,kv_bytes_per_token, andmemory_plan. - Injective, surjective, bijective: the GPT-2 byte map implements the bijection of q41; a correct solution must be injective and surjective.
- Masks: causal, padding, sliding window, additive combines causal and padding masks as sets (q4, q5).
- Softmax in C: three-pass and online two-pass is the online softmax kernel; q30 is its correctness argument.
- The tokenizer protocol and the char tokenizer treats Unicode normalization as an equivalence relation (q36).
- The first discrete math 2 problem set builds graphs, modular arithmetic, and hashing on these counting and proof tools.
Further reading
Section titled “Further reading”- Hammack, Book of Proof (free), chapters 1 to 10 and 12.
- Velleman, How to Prove It, chapters 3 and 6.
Key terms
Section titled “Key terms”- Set: an unordered collection of distinct elements.
- Contrapositive: , logically equivalent to .
- Binomial coefficient: , the number of -element subsets of an -element set.
- Induction hypothesis: the assumption used to prove .
- Loop invariant: a statement that holds before a loop, survives each iteration, and with the exit condition implies the result.
- Equivalence relation: a reflexive, symmetric, transitive relation; its classes partition the set.
- Bijection: a function that is both injective and surjective, and so has an inverse.
Summary
Section titled “Summary”- An attention mask is a set of allowed (query, key) pairs, so combining masks is set intersection and a fully masked row is the negation of “every row has a key” (q4, q5).
- Every size in the system is a count: parameters are sums of matrix shapes, FLOPs are per matmul, and KV-cache bytes are per token (q9 to q15).
- A loop is correct when an invariant holds before it, survives one iteration, and implies the result at exit; the online softmax kernel is proved this way (q29, q30).
- An equivalence relation is the same thing as “has the same image under some function”, which is how Unicode normalization groups strings (q36).
- A bijection is injective plus surjective; GPT-2’s byte map is checked by exactly those two halves (q38 to q41).
Review questions
Section titled “Review questions”-
Why are equal finite cardinalities alone not enough to prove a proposed function is a bijection?
Answer
A specific function still needs the relevant injectivity or surjectivity proof; cardinality alone does not establish its mapping behavior.