Model Size Calculation
The program estimates a model's memory size by multiplying the total parameter count by 4, assuming each parameter is a float32 value of 4 bytes.
Why a Parameter Count Is Not Yet a Size
A model's parameters are the numbers that make up the model, and a program can estimate how much memory those numbers occupy. The estimate is a short chain of arithmetic: start from a total parameter count, turn it into a number of bytes, and then turn the bytes into megabytes. Each step answers a different question, and the final printed value depends on all of them. This article follows that chain one step at a time, so that you can predict the printed result and locate the step responsible if the result looks wrong.
What do you think happens?
The model in the program has 10,000,000 parameters. Using only the rule that each parameter is a float32 value of 4 bytes and that bytes are divided by 1024 * 1024 to get megabytes, what number does the last line print, to two decimal places?
Reveal answer
38.15
Multiply 10,000,000 by 4 to get 40,000,000 bytes. Then divide 40,000,000 by 1,048,576 (which is 1024 * 1024) to get about 38.1470, which prints as 38.15 with two digits after the decimal point.
Three Steps in the Size Program
The program has three calculation steps, and each one answers a different question. The first step asks how many bytes the parameters need. The program assumes every parameter is a float32 number, which takes 4 bytes, so the byte count is the parameter count multiplied by 4. The second step converts bytes into megabytes by dividing by 1024 * 1024. The third step is the print statement, which formats the megabyte value with two digits after the decimal point.
Estimated size: 38.15 MBtotal_paramsholds the total number of parameters in the modeltotal_size_bytesis the intermediate value: 4 bytes for each float32 parametertotal_size_mbis the final value: bytes divided by 1,048,576- The print line formats the megabyte value with two digits after the decimal point
The code above follows the three steps described in the source, with the same 10,000,000 parameter count used in the prediction. Because the value of total_size_mb depends on total_size_bytes, which in turn depends on total_params, the three variables form one chain. An error introduced at any point in the chain is carried into every later value.
Why Each Parameter Costs Four Bytes
The first step rests on one assumption: every parameter is a float32 number, and a float32 value takes 4 bytes. That is why the byte count is always the parameter count multiplied by 4. The assumption is fixed in the program rather than read from the model, so the program's result is an estimate built on that assumption. If the parameters were stored in a different format, the multiplier would change, and the same chain would produce a different size.
Converting Bytes to Megabytes
The second step divides the byte count by 1024 * 1024, which is 1,048,576. The divisor is not one million. Using one million gives a different and incorrect result, because one megabyte in this program is defined as 1,048,576 bytes. Written as a calculation, the conversion for the 10,000,000 parameter model is $\frac{40{,}000{,}000}{1{,}048{,}576} \approx 38.1470$, which the print line rounds to 38.15.
Tracing a model of 2,500,000 parameters
A model has 2,500,000 parameters. Trace total_params, total_size_bytes, and total_size_mb, and state the printed value with two decimals.
- 1Set the count
total_params= 2,500,000. - 2Convert to bytes
total_size_bytes= 2,500,000 * 4 = 10,000,000 bytes. - 3Convert to megabytes
total_size_mb= 10,000,000 / 1,048,576, which is about 9.5367. - 4Format the output
The print line shows the value with two decimals, so it prints 9.54 MB.
The program prints 9.54 MB for a model of 2,500,000 parameters.
Counting Parameters in Layers and Modules
The source names two modules as a key comparison: the feed forward module and the multi-head attention module. To compare them fairly, count both with the same method. Count the parameters of each layer, including its biases. Add the layer counts to get a module total. Only after every module total is computed should the totals be compared with each other. The source does not give specific counts for these two modules, so the method, not a memorized number, is what carries forward.
The same method applies at both levels. For a single layer, the parameter count includes its weight terms and its bias terms. For a module, the module total is the sum of the counts of the layers that belong to it. Because both levels use the same rule, a module total can be compared with another module total directly. A comparison that mixes methods, such as counting weights for one module and weights plus biases for the other, is not a fair comparison.
Finding the First Wrong Value
When the printed size looks wrong, do not start by rechecking the final line. Inspect the intermediate values in order: total_params, then total_size_bytes, then total_size_mb. Two relationships must hold. The first is that total_size_bytes should be exactly four times total_params. The second is that total_size_mb should equal total_size_bytes divided by 1,048,576. The first value that breaks one of these relationships marks the step where the error was introduced. Later values can look plausible, but they inherit the error from that point on.
Suppose a learner's program prints 38.15 MB for the 10,000,000 parameter model, but the learner's new version prints 76.29 MB. Checking the first relationship, total_size_bytes should be 4 times 10,000,000, which is 40,000,000. If total_size_bytes shows 80,000,000, the first checkpoint fails. The error is in the bytes step, where the multiplier was set to 8 instead of 4. Rechecking total_size_mb would only confirm a value that is already wrong, so the search should stop at the bytes step.
Mistakes Beginners Make
Common mistake Dividing bytes by 1,000,000 to get megabytes
What it looks like40,000,000 bytes / 1,000,000 gives 40.00 instead of 38.15.Why it failsThe program defines a megabyte as 1024 * 1024, which is 1,048,576 bytes.
Do this insteadDivide by 1024 * 1024, or 1,048,576, in the conversion step.
Common mistake Counting weights but leaving out biases
Why it failsModule parameter counts must include biases before they are compared, so a count without biases is not the same quantity as the module total.
Do this insteadCount every layer's weights and biases with the same method, then add the layers to get the module total.
Common mistake Rechecking the final printed line first
Why it failstotal_size_mbdepends ontotal_size_bytes, which depends ontotal_params, so the final value cannot be trusted until the earlier values are verified.Do this insteadCheck total_params, then total_size_bytes, then total_size_mb, and stop at the first value that breaks its relationship.
Common mistake Comparing module totals that were counted differently
Why it failsA mismatched method makes the comparison unfair even when both numbers look reasonable.
Do this insteadUse one counting method for every layer in every module before comparing totals.
Practice Comparing Two Modules
Module A has two layers. Layer 1 has 512 weights and 512 biases. Layer 2 has 256 weights and 256 biases. Module B has one layer with 1,000 weights and 24 biases. Using the same counting method for both modules, compute each module total. Then compute the size in megabytes for Module A alone, assuming float32 parameters, and state which module has more parameters.
Hints
- Add the weights and the biases of each layer first, then add the layer counts to get the module total.
- Module A total is 512 + 512 + 256 + 256 parameters.
- For megabytes, multiply the Module A total by 4 to get bytes, then divide by 1,048,576 and format to two decimals.
Checking Your Answer on the Module Comparison
Solution to the module comparison
Compare the Module A and Module B totals, then convert the Module A total to megabytes.
- 1Module A total
Layer 1 gives 512 + 512 = 1,024. Layer 2 gives 256 + 256 = 512. The module total is 1,024 + 512 = 1,536 parameters.
- 2Module B total
The single layer gives 1,000 + 24 = 1,024 parameters.
- 3Comparison
Module A has 1,536 parameters and Module B has 1,024, so Module A has more parameters.
- 4Bytes for Module A
1,536 * 4 = 6,144 bytes.
- 5Megabytes for Module A
6,144 / 1,048,576 is about 0.00586, which prints as 0.01 MB with two decimals.
Module A has 1,536 parameters, Module B has 1,024, and Module A's size prints as 0.01 MB.
Key Takeaways
- The program estimates model memory in three linked steps: parameter count to bytes (multiply by 4 for float32), bytes to megabytes (divide by 1024 * 1024), and formatting for print.
- A megabyte in this calculation is 1,048,576 bytes, so dividing by one million gives a different and incorrect result.
- Module parameter counts, including biases, must be computed with the same method before modules are compared.
- When a result looks wrong, check
total_params,total_size_bytes, andtotal_size_mbin order and stop at the first value that breaks its expected relationship.