Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slow packing times for GPTQ #1943

Open
2 of 4 tasks
zankner opened this issue Jul 4, 2024 · 3 comments
Open
2 of 4 tasks

Slow packing times for GPTQ #1943

zankner opened this issue Jul 4, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@zankner
Copy link

zankner commented Jul 4, 2024

System Info

optimum=1.20.0, python=3.11.9, torch=2.3.1+cu121, system=ubuntu 20.04

Who can help?

No response

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction (minimal, reproducible, runnable)

I'm quantizing opt-350m using gptq. The actual quantization is fast but then packing layers is slow. The code to quantize the models is as follows:

quant_config = GPTQConfig(
    bits=args.bits,
    group_size=128,
    tokenizer=tokenizer,
    dataset=["c4-new"],
)

quantized_model = AutoModelForCausalLM.from_pretrained(
    "facebook/opt-125m",
    quantization_config=quant_config,
    torch_dtype=torch.bfloat16,
    trust_remote_code=True, # Remove later
    device_map="cuda:0"
)

I added some timings to GPTQ packing (

def pack_model(
):

import time
for name in qlayers:
    logger.info(name)
    start = time.time()
    quantizers[name], scale, zero, g_idx = quantizers[name]
    # so far can only pack layer on CPU
    layer_device = qlayers[name].device
    qlayers[name].to("cpu")
    layers[name], scale, zero, g_idx = layers[name].to("cpu"), scale.to("cpu"), zero.to("cpu"), g_idx.to("cpu")
    qlayers[name].pack(layers[name], scale, zero, g_idx)
    qlayers[name].to(layer_device)
    print(f"Time to pack {name}: {time.time() - start}")

This has timings:

Time for transformer.blocks.0.attn.Wqkv: 0.24
Time for transformer.blocks.0.attn.out_proj: 0.08
Time for transformer.blocks.0.ffn.down_proj: 0.25
Time for transformer.blocks.0.ffn.up_proj: 91.95

However, if I run it in parallel (which I think preserves everything) as:

from concurrent.futures import ThreadPoolExecutor
import time

def pack_layer(name):
    logger.info(name)
    start = time.time()
    quantizers[name], scale, zero, g_idx = quantizers[name]
    layer_device = qlayers[name].device
    qlayers[name].to("cpu")
    layers[name], scale, zero, g_idx = layers[name].to("cpu"), scale.to("cpu"), zero.to("cpu"), g_idx.to("cpu")
    qlayers[name].pack(layers[name], scale, zero, g_idx)
    qlayers[name].to(layer_device)
    print(f"Time for {name}: {time.time() - start}")

with ThreadPoolExecutor() as executor:
    executor.map(pack_layer, qlayers.keys())

The timings are
Time for transformer.blocks.0.attn.Wqkv: 0.21
Time for transformer.blocks.0.attn.out_proj: 0.10
Time for transformer.blocks.0.ffn.down_proj: 0.22
Time for transformer.blocks.0.ffn.up_proj: 3.64

Expected behavior

Whats strange is that the packing time is so long, taking ~ 90 seconds to pack the up projection. Whats even stranger is that the individual packing times per layer are lower when run in parallel (not just the overall time). Ie when run sequentially the time to pack the up proj is 90 seconds but this goes down to 3 seconds when running packing each layer in parallel.

@zankner zankner added the bug Something isn't working label Jul 4, 2024
@Qubitium
Copy link

@zankner I have fixed this in both GPTQModel and back-ported fix to main of AutoGPTQ.

@zankner
Copy link
Author

zankner commented Aug 12, 2024

Awesome thanks!

@zankner zankner closed this as completed Aug 12, 2024
@zankner
Copy link
Author

zankner commented Aug 13, 2024

@Qubitium sorry to re-open but still needs to be ported into optimum.

@zankner zankner reopened this Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants