TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
←

110. Weight Memory by dtype

Easy

Given a model parameter count and a numerical dtype, return the bytes of memory the weights occupy.

Signature: def weight_memory_bytes(n_params: int, dtype: str) -> int

Supported dtypes and their bytes-per-parameter:

  • 'fp32' -> 4
  • 'fp16' -> 2
  • 'bf16' -> 2
  • 'int8' -> 1
  • 'int4' -> 0.5 (round down via integer math)

Example: A 7B parameter model in fp16 needs 7e9 * 2 = 1.4e10 = 14_000_000_000 bytes (~14 GB).

For int4, return n_params // 2 (we round down — int4 packs two values per byte).

Math

bytes=N⋅bytes_per_param(dtype)

Asked at

NumPy

import numpy as np

 

def weight_memory_bytes(...):

    pass

🔒

Premium problem

Free accounts include problems #1–20. Upgrade to unlock the editor, hidden test cases, and reference solutions for every problem.

Upgrade to PremiumBack to problems

Already premium?