v9.1.1
Menu

QLoRA Memory Footprint Estimate

mediumPROEfficient Adaptation & Hallucination MetricsVision-Language ModelsVLM 1: Vision-Language Models

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

Input
print(qlora_bytes(7000000000, 40000000))
Output
{'base_bytes': 3500000000, 'adapter_bytes': 80000000, 'total_bytes': 3580000000}
Reasoning
  • Calculate the storage for the frozen base model by multiplying the parameter count by the bit-width and converting to bytes: 7,000,000,000×4/8=3,500,000,0007,000,000,000 \times 4 / 8 = 3,500,000,000 bytes.
  • Calculate the storage for the trainable LoRA adapters using their specific parameter count and higher precision: 40,000,000×16/8=80,000,00040,000,000 \times 16 / 8 = 80,000,000 bytes.
  • Sum the individual byte counts to determine the total memory footprint required for fine-tuning: 3,500,000,000+80,000,000=3,580,000,0003,500,000,000 + 80,000,000 = 3,580,000,000 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.

Learn this in

More from VLM 1: Vision-Language Models

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.