How much memory required for LLMGradientAttribution.attribute() on LLama2 tutorial?

rbelew: <p>I have an 8G NVIDIA GeForce RTX 3050 and it is able to run the <a href="https://github.com/pytorch/captum/blob/master/tutorials/Llama2_LLM_Attribution.ipynb" rel="noopener nofollow ugc">LLama2 demo</a> but only until it tries to build the LLMGradientAttribution. It then dies with <code>torch.OutOfMemoryError: CUDA out of memory.</code> (full trace below.). Is there a way to estimate the memory required for the LLMGradientAttribution?</p>
<div class="md-table">
<table>
<thead>
<tr>
<th></th>
<th></th>
<th>Traceback (most recent call last):</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td>File …/Llama2_LLM_Attribution.py, line 254, in </td>
</tr>
<tr>
<td></td>
<td></td>
<td>attr_res = llm_attr.attribute(inp, target=target)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/captum/attr/_core/llm_attr.py, line 533, in attribute</td>
</tr>
<tr>
<td></td>
<td></td>
<td>attr = self.attr_method.attribute(</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/captum/log/<strong>init</strong>.py, line 42, in wrapper</td>
</tr>
<tr>
<td></td>
<td></td>
<td>return func(*args, **kwargs)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/captum/attr/_core/layer/layer_integrated_gradients.py, line 496, in attribute</td>
</tr>
<tr>
<td></td>
<td></td>
<td>attributions = self.ig.attribute.<strong>wrapped</strong>(  # type: ignore</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/captum/attr/_core/integrated_gradients.py, line 286, in attribute</td>
</tr>
<tr>
<td></td>
<td></td>
<td>attributions = self._attribute(</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/captum/attr/_core/integrated_gradients.py, line 351, in _attribute</td>
</tr>
<tr>
<td></td>
<td></td>
<td>grads = self.gradient_func(</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/captum/attr/_core/layer/layer_integrated_gradients.py, line 472, in gradient_func</td>
</tr>
<tr>
<td></td>
<td></td>
<td>output = _run_forward(</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/captum/_utils/common.py, line 531, in _run_forward</td>
</tr>
<tr>
<td></td>
<td></td>
<td>output = forward_func(</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/captum/attr/_core/llm_attr.py, line 458, in _forward_func</td>
</tr>
<tr>
<td></td>
<td></td>
<td>output_logits = self.model(new_input_tensor)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/torch/nn/modules/module.py, line 1553, in _wrapped_call_impl</td>
</tr>
<tr>
<td></td>
<td></td>
<td>return self._call_impl(*args, **kwargs)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/torch/nn/modules/module.py, line 1562, in _call_impl</td>
</tr>
<tr>
<td></td>
<td></td>
<td>return forward_call(*args, **kwargs)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/accelerate/hooks.py, line 170, in new_forward</td>
</tr>
<tr>
<td></td>
<td></td>
<td>output = module._old_forward(*args, **kwargs)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/transformers/models/llama/modeling_llama.py, line 1189, in forward</td>
</tr>
<tr>
<td></td>
<td></td>
<td>outputs = self.model(</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/torch/nn/modules/module.py, line 1553, in _wrapped_call_impl</td>
</tr>
<tr>
<td></td>
<td></td>
<td>return self._call_impl(*args, **kwargs)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/torch/nn/modules/module.py, line 1562, in _call_impl</td>
</tr>
<tr>
<td></td>
<td></td>
<td>return forward_call(*args, **kwargs)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/accelerate/hooks.py, line 170, in new_forward</td>
</tr>
<tr>
<td></td>
<td></td>
<td>output = module._old_forward(*args, **kwargs)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/transformers/models/llama/modeling_llama.py, line 1001, in forward</td>
</tr>
<tr>
<td></td>
<td></td>
<td>layer_outputs = decoder_layer(</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/torch/nn/modules/module.py, line 1553, in _wrapped_call_impl</td>
</tr>
<tr>
<td></td>
<td></td>
<td>return self._call_impl(*args, **kwargs)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/torch/nn/modules/module.py, line 1562, in _call_impl</td>
</tr>
<tr>
<td></td>
<td></td>
<td>return forward_call(*args, **kwargs)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/accelerate/hooks.py, line 170, in new_forward</td>
</tr>
<tr>
<td></td>
<td></td>
<td>output = module._old_forward(*args, **kwargs)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/transformers/models/llama/modeling_llama.py, line 750, in forward</td>
</tr>
<tr>
<td></td>
<td></td>
<td>hidden_states = self.mlp(hidden_states)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/torch/nn/modules/module.py, line 1553, in _wrapped_call_impl</td>
</tr>
<tr>
<td></td>
<td></td>
<td>return self._call_impl(*args, **kwargs)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/torch/nn/modules/module.py, line 1562, in _call_impl</td>
</tr>
<tr>
<td></td>
<td></td>
<td>return forward_call(*args, **kwargs)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/accelerate/hooks.py, line 170, in new_forward</td>
</tr>
<tr>
<td></td>
<td></td>
<td>output = module._old_forward(*args, **kwargs)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/transformers/models/llama/modeling_llama.py, line 309, in forward</td>
</tr>
<tr>
<td></td>
<td></td>
<td>down_proj = self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/torch/nn/modules/module.py, line 1553, in _wrapped_call_impl</td>
</tr>
<tr>
<td></td>
<td></td>
<td>return self._call_impl(*args, **kwargs)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/torch/nn/modules/module.py, line 1562, in _call_impl</td>
</tr>
<tr>
<td></td>
<td></td>
<td>return forward_call(*args, **kwargs)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/accelerate/hooks.py, line 170, in new_forward</td>
</tr>
<tr>
<td></td>
<td></td>
<td>output = module._old_forward(*args, **kwargs)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/bitsandbytes/nn/modules.py, line 484, in forward</td>
</tr>
<tr>
<td></td>
<td></td>
<td>out = bnb.matmul_4bit(x, self.weight.t(), bias=bias, quant_state=self.weight.quant_state)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/bitsandbytes/autograd/_functions.py, line 579, in matmul_4bit</td>
</tr>
<tr>
<td></td>
<td></td>
<td>return MatMul4Bit.apply(A, B, out, bias, quant_state)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/torch/autograd/function.py, line 574, in apply</td>
</tr>
<tr>
<td></td>
<td></td>
<td>return super().apply(*args, **kwargs)  # type: ignore[misc]</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>File …/site-packages/bitsandbytes/autograd/_functions.py, line 509, in forward</td>
</tr>
<tr>
<td></td>
<td></td>
<td>output = torch.nn.functional.linear(A, F.dequantize_4bit(B, quant_state).to(A.dtype).t(), bias)</td>
</tr>
<tr>
<td></td>
<td></td>
<td>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</td>
</tr>
<tr>
<td></td>
<td></td>
<td>torch.OutOfMemoryError: CUDA out of memory. Tried to allocate 136.00 MiB. GPU 0 has a total capacity of 7.78 GiB of which 19.12 MiB is free. Including non-PyTorch memory, this process has 7.62 GiB memory in use. Of the allocated memory 7.34 GiB is allocated by PyTorch, and 170.50 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to avoid fragmentation.  See documentation for Memory Management  (<a href="https://pytorch.org/docs/stable/notes/cuda.html#environment-variables" class="inline-onebox" rel="noopener nofollow ugc">CUDA semantics — PyTorch 2.5 documentation</a>)</td>
</tr>
</tbody>
</table>
</div>