text
stringlengths
1
93.6k
parser.add_argument('dataset', type=str, help='Quantisation dataset')
parser.add_argument('--num_samples', type=int, default=128, help='Number of dataset samples')
parser.add_argument('--trust_remote_code', action="store_true", help='Trust remote code')
parser.add_argument('--cache_examples', type=int, default=1, help='Cache examples on GPU')
parser.add_argument('--use_fast', action="store_true", help='Use fast tokenizer')
parser.add_argument('--use_triton', action="store_true", help='Use Triton for quantization')
parser.add_argument('--bits', type=int, nargs='+', default=[4], help='Quantize bit(s)')
parser.add_argument('--group_size', type=int, nargs='+', default=[128], help='Quantize group size(s)')
parser.add_argument('--damp', type=float, nargs='+', default=[0.01], help='Quantize damp_percent(s)')
parser.add_argument('--desc_act', type=int, nargs='+', default=[0], help='Quantize desc_act(s) - 1 = True, 0 = False')
parser.add_argument('--dtype', type=str, choices=['float16', 'float32', 'bfloat16'], default='float16', help='Unquantised model dtype')
parser.add_argument('--seqlen', type=int, default=2048, help='Model sequence length')
parser.add_argument('--batch_size', type=int, default=1, help='Quantize batch size for processing dataset samples')
parser.add_argument('--stop_file', type=str, help='Filename to look for to stop inference, specific to this instance')
parser.add_argument('--make_folders', action="store_true", help='Make folders for each quantization using params in folder name')
args = parser.parse_args()
quantizer = QuantAutoGPTQ(args.pretrained_model_dir,
args.output_dir_base,
args.dataset,
num_samples=args.num_samples,
trust_remote_code=args.trust_remote_code,
cache_examples=args.cache_examples,
use_fast=args.use_fast,
use_triton=args.use_triton,
bits=args.bits,
group_size=args.group_size,
desc_act=args.desc_act,
damp=args.damp,
dtype=args.dtype,
seqlen=args.seqlen,
batch_size=args.batch_size,
stop_file=args.stop_file,
make_folder=args.make_folders)
quantizer.run_quantization()
# <FILESEP>
import argparse
import copy
import json
import os
import sys
import uuid
from collections import OrderedDict
from os.path import abspath, dirname
from types import SimpleNamespace
import torch
import torch.nn as nn
from tqdm import tqdm
from logger import logger, setup_logger
from model import CCF, HYM, F
from utils import KHotCrossEntropyLoss, checkpoint, eval_classification, get_data, init_random, plot, smooth_one_hot, set_seed
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def get_model_and_buffer(args, sample_q):
if args.pxycontrast > 0 or args.pxcontrast > 0:
f = HYM(args)
else:
model_cls = F if args.uncond else CCF
f = model_cls(args)
if not args.uncond:
assert args.buffer_size % args.n_classes == 0, "Buffer size must be divisible by args.n_classes"
if args.load_path is None:
replay_buffer = init_random(args.buffer_size)
else:
print(f"loading model from {args.load_path}")
ckpt_dict = torch.load(args.load_path)
f.load_state_dict(ckpt_dict["model_state_dict"])
replay_buffer = ckpt_dict["replay_buffer"]
f = f.to(device)
return f, replay_buffer
def get_sample_q(args):
def sample_p_0(replay_buffer, bs, y=None):
if len(replay_buffer) == 0:
return init_random(bs), []
buffer_size = len(replay_buffer) if y is None else len(replay_buffer) // args.n_classes
inds = torch.randint(0, buffer_size, (bs,))
# if cond, convert inds to class conditional inds
if y is not None:
inds = y.cpu() * buffer_size + inds
assert not args.uncond, "Can't drawn conditional samples without giving me y"
buffer_samples = replay_buffer[inds]
random_samples = init_random(bs)
choose_random = (torch.rand(bs) < args.reinit_freq).float()[:, None, None, None]
samples = choose_random * random_samples + (1 - choose_random) * buffer_samples
return samples.to(device), inds
def sample_q(f, replay_buffer, y=None, n_steps=args.n_steps, contrast=False):
"""this func takes in replay_buffer now so we have the option to sample from
scratch (i.e. replay_buffer==[]). See test_wrn_ebm.py for example.
"""
f.eval()
# get batch size