QLoRA Memory Footprint Estimate
Problem Statement
QLoRA fine-tunes a 4-bit-quantized base model while training only LoRA adapters in higher precision. Estimate the bytes needed to store the base weights plus the trainable adapters.
Background
For a model with base_params frozen weights quantized to base_bits bits each, the base storage is base_params * base_bits / 8 bytes. The LoRA adapters add adapter_params trainable weights stored at adapter_bits bits each: adapter_params * adapter_bits / 8 bytes. Total is their sum (as an integer number of bytes, floored).
This is the calculation behind "fine-tune a 7B model on a single 24 GB GPU": 4-bit base + tiny bf16 adapters.
Your Task
Implement:
def qlora_bytes(base_params, adapter_params, base_bits=4, adapter_bits=16):
Return a dict with "base_bytes", "adapter_bytes", "total_bytes" (all ints, floored).
Input Format
- base_params, adapter_params (int).
- base_bits, adapter_bits (int).
Output Format
- A dict of three ints.
Sample
print(qlora_bytes(7000000000, 40000000))
Output:
{'base_bytes': 3500000000, 'adapter_bytes': 80000000, 'total_bytes': 3580000000}
Example
print(qlora_bytes(7000000000, 40000000))
{'base_bytes': 3500000000, 'adapter_bytes': 80000000, 'total_bytes': 3580000000}- Calculate the storage for the frozen base model by multiplying the parameter count by the bit-width and converting to bytes: bytes.
- Calculate the storage for the trainable LoRA adapters using their specific parameter count and higher precision: bytes.
- Sum the individual byte counts to determine the total memory footprint required for fine-tuning: bytes.
- The final output is
{'base_bytes': 3500000000, 'adapter_bytes': 80000000, 'total_bytes': 3580000000}
Constraints
- All inputs are non-negative ints.
- Bytes = params * bits / 8, floored to an int.
total_bytes = base_bytes + adapter_bytes.