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

fix bug with c4 data loading #69

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def get_wikitext2(nsamples, seed, seqlen, tokenizer):
# Load and process c4 dataset
def get_c4(nsamples, seed, seqlen, tokenizer):
# Load train and validation datasets
traindata = load_dataset('allenai/c4', 'allenai--c4', data_files={'train': 'en/c4-train.00000-of-01024.json.gz'}, split='train')
valdata = load_dataset('allenai/c4', 'allenai--c4', data_files={'validation': 'en/c4-validation.00000-of-00008.json.gz'}, split='validation')
traindata = load_dataset('allenai/c4', data_files={'train': 'en/c4-train.00000-of-01024.json.gz'}, split='train')
valdata = load_dataset('allenai/c4', data_files={'validation': 'en/c4-validation.00000-of-00008.json.gz'}, split='validation')

# Generate samples from training set
random.seed(seed)
Expand Down Expand Up @@ -70,4 +70,4 @@ def get_loaders(name, nsamples=128, seed=0, seqlen=2048, tokenizer=None):
if 'wikitext2' in name:
return get_wikitext2(nsamples, seed, seqlen, tokenizer)
if "c4" in name:
return get_c4(nsamples, seed, seqlen, tokenizer)
return get_c4(nsamples, seed, seqlen, tokenizer)
2 changes: 1 addition & 1 deletion lib/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


# Function to evaluate perplexity (ppl) on a specified model and tokenizer
def eval_ppl(args, model, tokenizer, device=torch.device("cuda:0")):
def eval_ppl(model, tokenizer, device=torch.device("cuda:0")):
# Set dataset
dataset = "wikitext2"

Expand Down
7 changes: 4 additions & 3 deletions lib/prune.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import heapq
import torch
import torch.nn as nn
import tqdm
from .sparsegpt import SparseGPT
from .layerwrapper import WrappedGPT
from .data import get_loaders
Expand Down Expand Up @@ -128,14 +129,14 @@ def prune_wanda(args, model, tokenizer, device=torch.device("cuda:0"), prune_n=0
use_cache = model.config.use_cache
model.config.use_cache = False

print("loading calibdation data")
print("loading calibration data")
dataloader, _ = get_loaders("c4",nsamples=args.nsamples,seed=args.seed,seqlen=model.seqlen,tokenizer=tokenizer)
print("dataset loading complete")
with torch.no_grad():
inps, outs, attention_mask, position_ids = prepare_calibration_input(model, dataloader, device)

layers = model.model.layers
for i in range(len(layers)):
for i in tqdm.tqdm(range(len(layers))):
layer = layers[i]
subset = find_layers(layer)

Expand All @@ -162,7 +163,7 @@ def tmp(_, inp, out):
h.remove()

for name in subset:
print(f"pruning layer {i} name {name}")
#print(f"pruning layer {i} name {name}")
W_metric = torch.abs(subset[name].weight.data) * torch.sqrt(wrapped_layers[name].scaler_row.reshape((1,-1)))

W_mask = (torch.zeros_like(W_metric) == 1) ## initialize a mask to be all False
Expand Down
7 changes: 4 additions & 3 deletions lib/prune_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import heapq
import torch
import torch.nn as nn
import tqdm
from .sparsegpt import SparseGPT
from .layerwrapper import WrappedGPT
from .data import get_loaders
Expand Down Expand Up @@ -125,14 +126,14 @@ def prune_wanda(args, model, tokenizer, device=torch.device("cuda:0"), prune_n=0
use_cache = model.config.use_cache
model.config.use_cache = False

print("loading calibdation data")
print("loading calibration data")
dataloader, _ = get_loaders("c4",nsamples=args.nsamples,seed=args.seed,seqlen=model.seqlen,tokenizer=tokenizer)
print("dataset loading complete")
with torch.no_grad():
inps, outs, attention_mask = prepare_calibration_input(model, dataloader, device)

layers = model.model.decoder.layers
for i in range(len(layers)):
for i in tqdm.tqdm(range(len(layers))):
layer = layers[i]
subset = find_layers(layer)

Expand All @@ -159,7 +160,7 @@ def tmp(_, inp, out):
h.remove()

for name in subset:
print(f"pruning layer {i} name {name}")
#print(f"pruning layer {i} name {name}")
W_metric = torch.abs(subset[name].weight.data) * torch.sqrt(wrapped_layers[name].scaler_row.reshape((1,-1)))

W_mask = (torch.zeros_like(W_metric) == 1) ## initialize a mask to be all False
Expand Down