11.3.2 Sets, logic, and counting
This part of Discrete math 1 problem set: sets, counting, proofs, induction, relations, bijections develops the worked mechanisms below.
Concepts from other sections (15)
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.
- Product rule / quotient rule: and , the rules for differentiating a product or a ratio of two functions. Read more: Chapter 7: Calculus 1
- causal mask: the rule ; each position reads itself and the past. Read more: 11.2 Masks: causal, padding, sliding window, additive in AI Engineering
- 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
- Stars and bars: the number of ways to distribute identical objects into distinct bins is , counted as arrangements of stars and dividers in a row. Read more: Chapter 11: Discrete Math 1
- embedding table: the matrix whose row is the learned vector for token , shared across every window. Read more: 8.2 Bengio’s neural probabilistic language model in AI Engineering
- logits: unnormalized log-scores that softmax turns into probabilities; here the table stores . Read more: 6.1 Byte bigram: counts, logits, safetensors v0 in AI Engineering
- Decoder block: two Pre-LN residual steps, attention then MLP. Read more: 13.9 The Llama-Family Model: Config, Weight Loading, and Download in AI Engineering
- RMSNorm: divides a vector by its root mean square plus eps (inside the root) and multiplies by a learned gain; no mean subtraction, no bias. Read more: 13.1 Pre-LN and RMSNorm in AI Engineering
- Dot product: ; it measures how much two vectors point the same way, and it is zero exactly when they are perpendicular. Read more: Chapter 10: Calculus 3
- 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
- BF16: a 16-bit float with float32’s sign and exponent and 7 mantissa bits; the top half of a rounded float32. Read more: 6.7 Safetensors for every dtype, atomic checkpoints, and the token stream in AI Engineering
- KV cache: the stored keys and values of earlier tokens, bytes per token. Read more: 11.1 Counting: parameters, FLOPs, KV bytes, and memory plans
- 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
Learning objectives
- Work the set, quantified-logic, and counting problems before revealing solutions.
Sets and logic
Section titled “Sets and logic”q1. Let , and .
(a) . (b) . (c) The complement of in . [set]
Answer
(a) , (b) , (c) . (a) The elements in both: . (b) The elements of that are not in : . (c) , and the rest of is . Common mistakes: the union for (a); for (b), since difference is not symmetric; and for (c), the complement of alone. These are the operations of the mask algebra in Masks: causal, padding, sliding window, additive in AI Engineering.
q2. How many subsets of contain but not ? [number]
Answer
. The product rule: 1 must be in, 2 must be out, and each of the other 4 elements is independently in or out, so . A common mistake is 32, which fixes only one of the two elements.
q3. For propositions and : (a) is logically equivalent to ? (b) Is logically equivalent to ? [bool]
Answer
(a) True, (b) false. (a) The contrapositive has the same truth table as : both are false only when is true and is false. (b) The converse is not equivalent: with false and true, is true but is false. The contrapositive is the move behind q18; the converse is the classic slip.
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]
Answer
(a) , (b) . (a) Entry is 1 when (causal) and (padding): the lower triangle with column 3 cleared. (b) Rows allow pairs. Common mistakes: the causal mask alone, which keeps (10 pairs) and lets the pad key receive attention; and the upper triangle , which lets a query look at the future.
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 .
Answer
(a). The statement is . Negating swaps each quantifier and negates the predicate: , a row with no allowed key. That fully masked row is the NaN case of Masks: causal, padding, sliding window, additive and of Stable numerics: logsumexp, softmax, and compensated sums. A common mistake is (c), which negates only the inner predicate and describes any matrix with a single 0; (b) is much stronger than the negation.
q6. Prove De Morgan’s law for subsets of a universe . [proof]
Answer
Claim: for subsets of a universe , , where .
Method: show each side is a subset of the other (double inclusion).
() Let . Then and . By the definition of union, means ” or ”, so means “not ( or )”, which by De Morgan’s law for propositions is ” and ”. Hence and , so .
() Let . Then , , and . So ” or ” is false, that is , and .
Each set contains the other, so .
Model proof also in q6.md.
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]
Answer
(a) , (b) , (c) . (a) An unordered choice: . (b) Four positions with 256 options each: . (c) Ordered pairs with repetition: . Common mistakes: 720 for (a) counts ordered choices; 1024 for (b) mixes up base and exponent; 32,640 for (c) counts unordered pairs of distinct bytes, but a bigram table needs and and .
q8. In how many ways can 5 identical tokens be placed into 3 distinct buckets, empty buckets allowed? [number]
Answer
. Stars and bars: 5 stars and 2 bars in a row, . Common mistakes: treats the tokens as distinct, and forgets that the bars are placed among the stars.
q9. A linear layer maps to . How many parameters does it have? [expr in m, n]
Answer
. is , so weights, plus one bias per output: . This is how param_count in Counting: parameters, FLOPs, KV bytes, and memory plans adds biases. Common mistakes: forgets the bias, and sizes the bias by the input.
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]
Answer
. The embedding is a table, and the untied head is a matrix: each. Tying the two (tie_embeddings in ModelConfig) halves this to , which is the common wrong answer here.
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]
Answer
. Attention: . MLP: . Norms: . This is where the rule "" comes from. Common mistakes: forgets the norm gains, and counts only one MLP matrix.
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]
Answer
. has entries, and each is a dot product of length : multiplications and additions into the accumulator, FLOPs. That is the of Vectors, matrices, row-major layout, and matmul and of the roofline in the matmul kernel. A common mistake is , which counts multiply-adds, not FLOPs.
q13. The training-compute rule of thumb is FLOPs for parameters and tokens. Give for and . [number]
Answer
. . The 6 is 2 for the forward pass and 4 for the backward pass. A common mistake is , which uses , the forward pass only.
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]
Answer
. bytes. Common mistakes: 11,520 caches keys only (no factor 2), and 69,120 sizes the cache by the 9 query heads instead of the 3 key/value heads, which is exactly what GQA saves.
q15. How many bytes does that cache take for a context of 2048 tokens? [number]
Answer
. bytes, exactly 45 MiB. Keys only would give half, 23,592,960.
q16. How many integers in are divisible by 3 or by 5? [number]
Answer
. Inclusion-exclusion: . A common mistake is 53, which counts the multiples of 15 twice.
Review questions
Section titled “Review questions”-
How can a quantified claim be disproved?
Answer
Give a counterexample with the required domain membership and show it violates the predicate.