Discrete math 1 problem set: sets, counting, proofs, induction, relations, bijections
Overview
Section titled “Overview”| Module | S-M05 · solve · none · Pass 2 · 8 to 10 h |
| You build | answers in solve/S-M05.toml (34 checked by SymPy) and 18 proofs in solve/S-M05/qN.md (self-graded against their rubrics) |
| Contract | none: a pen and paper set |
| Tests | course/solve/S-M05/key.toml (hidden): typed answers plus reject canaries; the problems are in course/solve/S-M05/problems.md and in section 4 |
| Needs | high-school algebra. Reading: the Discrete Math 1 topic, sections 1 to 6 |
| Used by | no call site (a solve set). Do it before M06.1 (toposort correctness is an induction), M06.3 (PCG32 is counting and modular arithmetic), M05.1 (parameter, FLOP, and KV-byte counts), M05.2 (the GPT-2 byte bijection), L5.2 (mask algebra), and L9.2 (the online softmax invariant) |
| Milestone | MS-P2 (the Pass 2 gate runs ol check on every solve part of the pass) |
| Optional depth | Hammack, Book of Proof (free), ch. 1 to 10 and 12; Velleman, How to Prove It, ch. 3 and 6 |
Key Takeaways
Section titled “Key Takeaways”- 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 your 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 of
L9.2is 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).
How to work this chapter
Section titled “How to work this chapter”ol start S-M05 # writes solve/S-M05.toml and one file per proofol check S-M05 # SymPy checks the answers, then asks each proof rubric (y/n)ol check S-M05 --regrade # ask the rubrics again after you change a proof1. Why now
Section titled “1. Why now”Pass 1 gave you a running tracer: a byte bigram trained in Python, served from Rust, behind a Go gateway. Pass 2 turns it into a stack you can reason about, and the next build modules are arguments as much as code. M06.1 must order every node of the autograd graph before backward runs, and the only convincing evidence that it does is an induction over the traversal. M06.3 builds PCG32, whose state wraps modulo and whose output is a counted rotation of bits. M05.1 turns a ModelConfig into parameter, FLOP, and KV-byte counts that decide whether a model fits on your laptop. L5.2 builds attention masks from boolean algebra, and L9.2 streams a softmax in one pass under a loop invariant. This set gives you the vocabulary and the proof habits those modules assume: sets and logic, counting, the standard proof methods, induction, relations, and functions.
2. Principles
Section titled “2. Principles”| Symbol | Meaning | Type / shape |
|---|---|---|
| is an element of the set | ||
| every element of is in | ||
| union (in either), intersection (in both), difference (in , not in ) | sets | |
| complement inside a universe | set | |
| the number of elements of a finite set | integer | |
| not, and, or, implies, if and only if | propositions | |
| “for every”, “there exists” | quantifiers | |
| , with | integer | |
| , the number of -element subsets of an -set | integer | |
| divides : for an integer | relation | |
| a function: each has exactly one image | ||
| composition, | function |
2.1 Sets
Section titled “2.1 Sets”A set is an unordered collection of distinct elements, written by listing, , or by a rule, . Order and repetition do not matter: . Two sets are equal when each is a subset of the other, which is how set identities are proved: take an arbitrary element of one side and show it is in the other, then the reverse (“double inclusion”). The power set is the set of all subsets of ; it has elements, because each element is independently in or out.
A boolean mask is a set in disguise. An attention mask over positions is a subset of , the pairs (query , key ) that may interact, stored as a 0/1 matrix. The causal mask is , a padding mask is , and applying both is their intersection, elementwise AND.
2.2 Logic and quantifiers
Section titled “2.2 Logic and quantifiers”A proposition is a statement that is true or false. Connectives build new ones, defined by truth tables: is true when both are, when at least one is, flips , and is false only when is true and false. Two propositions are logically equivalent when they agree on every row of the truth table. The contrapositive is equivalent to ; the converse is not. De Morgan’s laws move a negation inward and swap the connective: and . For sets they read .
A predicate becomes a proposition once is fixed. Quantifiers close it over a domain: (“for every ”) and (“there exists ”). Negation swaps them: , and . With nested quantifiers, swap each one and negate the innermost predicate.
2.3 Counting
Section titled “2.3 Counting”Four rules count almost everything in this course.
- Product rule. A choice made in independent steps with options has outcomes. Strings of length over an alphabet of size : .
- Ordered without repetition. Arranging of distinct items in order: . All of them: .
- Unordered without repetition. Choosing a -subset: , the ordered count divided by the orders of each subset.
- Unordered with repetition (“stars and bars”). Placing identical items into distinct boxes is arranging stars and bars in a row: .
The sum rule adds the sizes of disjoint cases. When the cases overlap, inclusion-exclusion corrects the double count: .
Model sizes are counts of this kind. A matrix of shape holds numbers; a layer’s parameter count is the sum over its matrices and vectors. A matrix product with of shape and of shape computes outputs, each a sum of products, so it takes multiplications and as many additions: floating-point operations (FLOPs). A forward pass over parameters costs about FLOPs per token, and training about (the backward pass costs twice the forward), so training on tokens costs about . The KV cache stores one key vector and one value vector of length per KV head per layer per token, at bytes per number: bytes per token.
2.4 Proof methods
Section titled “2.4 Proof methods”A proof is a finite chain of statements, each a definition, an assumption, an earlier result, or a logical consequence of earlier lines, ending in the claim. The methods differ in what they assume first.
| Method | To prove | Use it when |
|---|---|---|
| Direct | assume , derive | definitions unfold forward (q17) |
| Contrapositive | assume , derive | gives you something to compute with (q18) |
| Contradiction | assume and , derive a false statement | the claim says something does not exist (q19, q20) |
| Cases | split into exhaustive cases, prove each | parity, signs, ranges (q21) |
| Counterexample | exhibit one with | disproving a claim (q23) |
| Pigeonhole | more than objects in boxes put two in one box | collisions (q24) |
Definitions are the raw material: an integer is even if and odd if for an integer ; a number is rational if it is for integers with ; an integer is prime if its only positive divisors are 1 and .
2.5 Induction and loop invariants
Section titled “2.5 Induction and loop invariants”Induction proves for every integer in two steps: the base case , and the inductive step “if then ” for every . The assumption is the induction hypothesis, and the step must use it rather than the conclusion. Strong induction assumes for every with and proves ; it suits claims where breaks into smaller pieces of arbitrary size, such as factorizations.
A loop invariant is induction over iterations. To prove a loop correct, show three things: the invariant holds before the first iteration (initialization), one iteration preserves it (maintenance), and the loop stops (termination: some nonnegative integer strictly decreases). At exit, the invariant plus the exit condition imply the result. L9.2 and M06.1 both rest on arguments of this shape.
2.6 Relations
Section titled “2.6 Relations”A relation on a set is a set of ordered pairs from ; write for . It is reflexive if for every , symmetric if , antisymmetric if , and transitive if .
An equivalence relation is reflexive, symmetric, and transitive. Its equivalence classes partition into disjoint nonempty blocks, and every partition comes from exactly one equivalence relation, so counting equivalence relations on an -set is counting its partitions (the Bell number : ). A partial order is reflexive, antisymmetric, and transitive, like on numbers, on sets, or “must run before” on the nodes of a computation graph (M06.1).
2.7 Functions and bijections
Section titled “2.7 Functions and bijections”A function is injective (one-to-one) if , surjective (onto) if every equals some , and bijective if both; a bijection has an inverse . Between finite sets of equal size, injective and surjective imply each other. The number of functions from a -set to an -set is , the injective ones number , and the bijections of an -set to itself number . To prove injective, take and show , often by cases on where and lie.
2.8 How your answers are checked
Section titled “2.8 How your answers are checked”ol check parses each answer as ASCII math and compares it with the key in SymPy: an [expr] must equal the key as a formula (symbolically, or at 32 random points of its domain), a [number] marked exact must be an exact value (3/4, not 0.75), a [set] matches element for element in any order, and a [matrix] entry by entry. Feedback names what differs, never the expected answer. A proof is graded by you: ol check prints each line of its rubric and you answer y or n, and every line must be a yes.
3. Worked example by hand
Section titled “3. Worked example by hand”This is a sibling of q11 and q26, not one of the graded problems.
Count. SmolLM2-135M’s attention uses grouped-query attention: width , 9 query heads and 3 key/value heads of size , no biases. How many parameters do its four projections hold?
The query projection maps width 576 to outputs: a matrix, parameters. The key and value projections each map 576 to outputs: each. The output projection maps the 576 concatenated head outputs back to 576: . Total: . In solve/ this would be answer = "2*576*576 + 2*576*192": any expression with the right exact value passes. Over 30 layers that is , about a fifth of the model.
Proof. Claim: for every integer .
Method: induction on . Base case : the left side is . Induction hypothesis: for some , . Inductive step: by the hypothesis, , the claim for . By induction the claim holds for every .
Read it against the proof rubric (course/rubrics/proof.md): the claim and every symbol are stated, the method is named, the base case is explicit, the hypothesis is stated for and used in the step (not the conclusion), and the last line restates the claim. Your proofs for q26 to q30 should look like this.
4. The problem set
Section titled “4. The problem set”Write each answer in solve/S-M05.toml; lettered parts are their own tables:
[q4.a]answer = "[[1, 0, 0, 0], [1, 1, 0, 0], [1, 1, 1, 0], [1, 1, 1, 0]]"[q9]answer = "m*n + m"[q6]proof = "S-M05/q6.md"ASCII math: x^2, 2^32, binomial(10, 3), factorial(5), exp(-1), sets {1, 2}, matrices as lists of rows. The tag after each problem is its answer type.
Sets and logic
Section titled “Sets and logic”q1. Let , and .
(a) . (b) . (c) The complement of in . [set]
q2. How many subsets of contain but not ? [number]
q3. For propositions and : (a) is logically equivalent to ? (b) Is logically equivalent to ? [bool]
q4. An attention mask is a 0/1 matrix whose row is a query position and column a key position; means “query may attend to key ”. With positions, the causal mask allows and the padding mask allows (position 3 is padding).
(a) Give the combined mask (causal AND padding) as a matrix. [matrix]
(b) How many pairs does it allow? [number]
q5. Which statement is the negation of “for every query row there is a key with ”? [choice]
(a) There is a row such that for every key .
(b) for every row and every key .
(c) There is a row and a key with .
(d) For every row there is a key with .
q6. Prove De Morgan’s law for subsets of a universe . [proof]
Counting
Section titled “Counting”q7. (a) In how many ways can you choose 3 of 10 attention heads to prune? (b) How many byte strings of length 4 are there? (c) How many entries does a byte bigram table have (ordered pairs of bytes, repeats allowed)? [number]
q8. In how many ways can 5 identical tokens be placed into 3 distinct buckets, empty buckets allowed? [number]
q9. A linear layer maps to . How many parameters does it have? [expr in m, n]
q10. A language model has an embedding table with rows of width and an untied output head that maps width to logits without a bias. How many parameters do the two hold together? [expr in V, d]
q11. A decoder block of width has four attention projections () without biases, an MLP (two matrices, no biases), and two RMSNorm gain vectors of length . How many parameters does the block have? [expr in d]
q12. Counting one multiplication and one addition as two floating-point operations, how many FLOPs does the product of an matrix and a matrix take? [expr in m, k, n]
q13. The training-compute rule of thumb is FLOPs for parameters and tokens. Give for and . [number]
q14. SmolLM2-135M has 30 layers, 3 key/value heads, and head dimension 64, and caches keys and values in bf16 (2 bytes per number). How many bytes of KV cache does one token take? [number]
q15. How many bytes does that cache take for a context of 2048 tokens? [number]
q16. How many integers in are divisible by 3 or by 5? [number]
Proof techniques
Section titled “Proof techniques”q17. Prove directly: if is an odd integer, then is odd. [proof]
q18. Prove by contrapositive: if is an integer and is even, then is even. [proof]
q19. Prove by contradiction: is irrational. [proof]
q20. Prove that there are infinitely many primes. [proof]
q21. Prove by cases: is even for every integer . [proof]
q22. Prove: for real , , with equality exactly when . [proof]
q23. Claim: is prime for every integer . (a) Is the claim true? [bool] (b) Give the smallest for which is not prime. [number]
q24. Prove (pigeonhole): any function from a set of 257 byte strings to 8-bit hash values maps two different strings to the same value. [proof]
q25. Prove: the sum of a rational number and an irrational number is irrational. [proof]
Induction and loop invariants
Section titled “Induction and loop invariants”q26. Prove by induction: for every integer . [proof]
q27. Prove by induction: for every integer . [proof]
q28. Prove by strong induction: every integer is a product of one or more primes. [proof]
q29. Prove that this loop returns for every integer , using the invariant (the same squaring trick computes PCG32’s jump-ahead):
r = 1; b = x; e = nwhile e > 0: if e is odd: r = r * b b = b * b e = floor(e / 2)return r[proof]
q30. The online softmax (the C kernel of L9.2) reads once, keeping a running maximum and a running sum . It starts at , (with ), and for each new sets and . Prove that after reading it holds that and . [proof]
q31. Give a closed form for . [expr in n]
q32. Give a closed form for . [expr in n]
q33. Run the loop of q30 on . Give the final exactly. [number]
Relations
Section titled “Relations”q34. Let on . Is (a) reflexive, (b) symmetric, (c) transitive? [bool]
q35. How many equivalence relations are there on a 4-element set? [number]
q36. Prove: for any function , the relation is an equivalence relation on . (Unicode normalization in L1.1 is this relation with .) [proof]
q37. Prove: divisibility ( when for some integer ) is a partial order on the positive integers. [proof]
Functions and bijections
Section titled “Functions and bijections”q38. Let , . Is (a) injective, (b) surjective? [bool]
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]
q40. Prove: if and are bijections, then is a bijection. [proof]
q41. GPT-2’s byte-to-character map (M05.2) 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]
5. Pitfalls
Section titled “5. Pitfalls”| Pitfall | Symptom | Caught by |
|---|---|---|
| Combining masks with OR, or forgetting the padding key | a pad position gets attention weight | q4 (canary: the causal-only mask) |
| Negating only the inner predicate of a nested quantifier | you test “some entry is 0” instead of “some row is all 0” | q5 (canary: choice c) |
| Counting multiply-adds instead of FLOPs | FLOP and roofline numbers off by 2 | q12 (canary mkn) |
| Using (forward only) for training compute | a training budget 3 times too small | q13 (canary 2*10^15) |
| Caching keys only, or sizing the cache by query heads | KV memory off by 2 or by the GQA group size | q14 (canaries 11520 and 69120) |
| Adding overlapping cases twice | inclusion-exclusion overcount | q16 (canary 53) |
| Checking many cases instead of proving | a “for every ” claim believed from to | q23 (canary 40) |
| Using the conclusion inside the inductive step | a circular proof that the rubric rejects | q26 to q30 rubric line 2 or 3 |
| Forgetting the running sum must be rescaled when the max changes | online softmax overflows or is wrong after a new max | q30 rubric, q33 (canary without the shift) |
| Confusing all functions with injective ones | where was asked | q39 (canaries 3125 and 125) |
6. Where it’s used next
Section titled “6. Where it’s used next”| Direction | Module | How it uses this |
|---|---|---|
| Forward | M06.1 | toposort over the autograd graph; its correctness proof is an induction over the traversal (S-M06a q20) |
| Forward | M06.3 | PCG32 and SplitMix64: counting bits, rotations, and arithmetic modulo |
| Forward | M05.1 | param_count, flops_per_token, kv_bytes_per_token, memory_plan are q9 to q15 as code |
| Forward | M05.2 | bytes_to_unicode is the bijection of q41; its tests check injective and surjective |
| Forward | L5.2 | causal and padding masks combined as sets (q4, q5) |
| Forward | L9.2 | the online softmax kernel in C; q30 is its correctness argument |
| Forward | L1.1 | Unicode normalization as an equivalence relation (q36) |
| Forward | S-M06a | graphs, modular arithmetic, and hashing build on these counting and proof tools |