02-parameter-counting answers "how many numbers does this model have." That's not yet "will this model fit on my GPU" — the actual memory cost depends on how many bytes each number takes, which depends on precision (float32 vs float16 vs int8), and on the fact that training needs far more than just the parameters themselves: gradients and optimizer state add up fast.
Implement estimate_memory_bytes(params, dtype="float32"), the raw storage cost of a parameter list at a given precision, and estimate_optimizer_memory_bytes(params, dtype="float32", optimizer="sgd"), the full training-time memory cost including gradients and optimizer state.
dtype is one of "float32" (4 bytes/element), "float16"/"bfloat16" (2 bytes/element), "int8" (1 byte/element).estimate_memory_bytes returns total elements times bytes-per-element for the given dtype.estimate_optimizer_memory_bytes returns parameter bytes + gradient bytes (always equal to parameter bytes) + optimizer state bytes, where optimizer state is 0x param bytes for "sgd", 1x for "momentum", 2x for "adam".params list returns 0 bytes in both functions.Open one at a time. Each gives away a little more than the last.
Reuse the same total-element-count idea as 02-parameter-counting, then multiply by however many bytes one element of dtype takes.
Every optimizer needs to store a gradient the same size as the parameters it's updating — that's always 1x extra, regardless of which optimizer. Beyond that, sgd needs nothing more, momentum needs one more same-sized buffer, adam needs two (its first and second moment estimates).
No results yet
Run your code to check it against the examples, or submit to run every test.