|
| 1 | +# This file is adapted from https://github.com/HArmonizedSS/HASS (arxiv: https://arxiv.org/abs/2408.15766) |
| 2 | +# Which is a fork of the Eagle repository: https://github.com/SafeAILab/EAGLE (arxiv: https://arxiv.org/abs/2401.15077) |
| 3 | + |
| 4 | +import argparse |
| 5 | +import os |
| 6 | + |
| 7 | +import torch |
| 8 | +from datasets import load_dataset |
| 9 | +from transformers import AutoModelForCausalLM, AutoTokenizer |
| 10 | + |
| 11 | +parser = argparse.ArgumentParser(description="sp") |
| 12 | +parser.add_argument("--start", type=int, default=0) |
| 13 | +parser.add_argument("--end", type=int, default=100) |
| 14 | +parser.add_argument("--index", type=int, default=1) |
| 15 | +parser.add_argument("--gpu_index", type=int, nargs="+", default=[0]) |
| 16 | +parser.add_argument("--outdir", type=str, default="outdir0") |
| 17 | +parser.add_argument("--data_path", type=str, default="0") |
| 18 | +parser.add_argument("--model_path", type=str, default="0") |
| 19 | +args = parser.parse_args() |
| 20 | + |
| 21 | +os.environ["CUDA_VISIBLE_DEVICES"] = str(args.gpu_index)[1:-1] |
| 22 | + |
| 23 | +bigname = args.model_path |
| 24 | + |
| 25 | + |
| 26 | +def longest_common_prefix(list1, list2): |
| 27 | + prefix_length = 0 |
| 28 | + min_length = min(len(list1), len(list2)) |
| 29 | + |
| 30 | + for i in range(min_length): |
| 31 | + if list1[i] == list2[i]: |
| 32 | + prefix_length += 1 |
| 33 | + else: |
| 34 | + break |
| 35 | + |
| 36 | + common_prefix = list1[:prefix_length] |
| 37 | + return common_prefix, prefix_length |
| 38 | + |
| 39 | + |
| 40 | +def build_dataset_rank( |
| 41 | + tokenizer, |
| 42 | + split="train", # noqa: ARG001 |
| 43 | + select=None, # noqa: ARG001 |
| 44 | +): |
| 45 | + ds = load_dataset("json", data_files=args.data_path) |
| 46 | + ds = ds["train"] |
| 47 | + ds = ds.shuffle(seed=42) |
| 48 | + ds1 = ds.select(range(args.start, args.end)) |
| 49 | + |
| 50 | + original_columns1 = ds1.column_names |
| 51 | + # original_columns2 = ds2.column_names |
| 52 | + num_proc = 4 # noqa: F841 |
| 53 | + |
| 54 | + def preprocess_function(examples): |
| 55 | + new_examples = {"conversation": [], "input_ids": [], "loss_mask": []} |
| 56 | + for i in range(len(examples["id"])): |
| 57 | + messages = [ |
| 58 | + { |
| 59 | + "role": "system", |
| 60 | + "content": ( |
| 61 | + "Cutting Knowledge Date: December 2023\nToday Date: 26 Jul 2024" |
| 62 | + ), |
| 63 | + }, |
| 64 | + ] |
| 65 | + convroles = ["user", "assistant"] |
| 66 | + roles = {"human": "user", "gpt": "assistant"} |
| 67 | + source = examples["conversations"][i] |
| 68 | + if roles[source[0]["from"]] != "user": |
| 69 | + # Skip the first one if it is not from human |
| 70 | + source = source[1:] |
| 71 | + for j, sentence in enumerate(source): |
| 72 | + role = roles[sentence["from"]] |
| 73 | + assert role == convroles[j % 2], f"{i}" # noqa: S101 |
| 74 | + if sentence["from"] == "gpt": |
| 75 | + sentence["value"] = " " + sentence["value"] |
| 76 | + messages.append({"role": role, "content": sentence["value"]}) |
| 77 | + conversation = tokenizer.apply_chat_template( |
| 78 | + messages, |
| 79 | + tokenize=False, |
| 80 | + add_generation_prompt=False, |
| 81 | + ) |
| 82 | + |
| 83 | + if not tokenizer.pad_token_id: |
| 84 | + tokenizer.pad_token_id = tokenizer.unk_token_id |
| 85 | + |
| 86 | + input_ids = tokenizer( |
| 87 | + conversation, |
| 88 | + return_tensors="pt", |
| 89 | + max_length=4096, |
| 90 | + add_special_tokens=False, |
| 91 | + ).input_ids[0] |
| 92 | + loss_mask = torch.ones_like(input_ids) |
| 93 | + # print(i) |
| 94 | + |
| 95 | + sep = "<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n" |
| 96 | + |
| 97 | + total_len = len(input_ids) # noqa: F841 |
| 98 | + |
| 99 | + sep2 = "<|eot_id|><|start_header_id|>user<|end_header_id|>" |
| 100 | + turns = conversation.split(sep2) |
| 101 | + |
| 102 | + turns[1] = turns[0] + sep2 + turns[1] |
| 103 | + turns = turns[1:] |
| 104 | + |
| 105 | + cur_len = 1 |
| 106 | + loss_mask[:cur_len] = 0 |
| 107 | + for i, turn in enumerate(turns): # noqa: PLW2901 |
| 108 | + if turn == "": |
| 109 | + break |
| 110 | + turn_len = len(tokenizer(turn).input_ids) |
| 111 | + |
| 112 | + parts = turn.split(sep) |
| 113 | + if len(parts) != 2: |
| 114 | + break |
| 115 | + parts[0] += sep |
| 116 | + # "-2" is hardcoded for the Llama tokenizer to make the offset correct. |
| 117 | + instruction_len = len(tokenizer(parts[0]).input_ids) - 1 |
| 118 | + |
| 119 | + # Ignore the user instructions |
| 120 | + if i == 0: |
| 121 | + loss_mask[cur_len : cur_len + instruction_len - 2] = 0 |
| 122 | + else: |
| 123 | + loss_mask[cur_len - 3 : cur_len + instruction_len + 1] = 0 |
| 124 | + cur_len += turn_len |
| 125 | + if i != 0: |
| 126 | + cur_len += 3 |
| 127 | + # cur_len+=2 |
| 128 | + |
| 129 | + # if i != 0 and not tokenizer.legacy: |
| 130 | + # # The legacy and non-legacy modes handle special toks differently |
| 131 | + # cur_len -= 1 |
| 132 | + |
| 133 | + loss_mask[cur_len:] = 0 |
| 134 | + |
| 135 | + new_examples["conversation"].append(conversation) |
| 136 | + new_examples["input_ids"].append(input_ids[None, :]) |
| 137 | + new_examples["loss_mask"].append(loss_mask[None, :]) |
| 138 | + |
| 139 | + return new_examples |
| 140 | + |
| 141 | + ds1 = ds1.map( |
| 142 | + preprocess_function, |
| 143 | + batched=True, |
| 144 | + # num_proc=num_proc, |
| 145 | + remove_columns=original_columns1, |
| 146 | + load_from_cache_file=False, |
| 147 | + ) |
| 148 | + |
| 149 | + ds1.set_format(type="torch") |
| 150 | + return ds1 |
| 151 | + |
| 152 | + |
| 153 | +bigtokenizer = AutoTokenizer.from_pretrained(bigname, use_fast=False) |
| 154 | +ds = build_dataset_rank(bigtokenizer) |
| 155 | +print(ds) |
| 156 | +bigmodel = AutoModelForCausalLM.from_pretrained( |
| 157 | + bigname, device_map="auto", torch_dtype=torch.float16 |
| 158 | +) |
| 159 | +bigmodel.eval() |
| 160 | + |
| 161 | + |
| 162 | +@torch.no_grad() |
| 163 | +def ge(data): |
| 164 | + input_ids = data["input_ids"] |
| 165 | + num_layers = len(bigmodel.model.layers) |
| 166 | + outs_big = bigmodel(input_ids.cuda(), output_hidden_states=True) |
| 167 | + # hidden_state_big = outs_big.hidden_states[-1] |
| 168 | + featureFusion = [ |
| 169 | + outs_big.hidden_states[3], |
| 170 | + outs_big.hidden_states[num_layers // 2 + 1], |
| 171 | + outs_big.hidden_states[-3], |
| 172 | + ] |
| 173 | + target = outs_big.hidden_states[-1] |
| 174 | + hidden_state_big = torch.cat(featureFusion, dim=-1) |
| 175 | + max_prob_tokens_big = torch.argmax(outs_big.logits, dim=-1) # noqa: F841 |
| 176 | + probs = torch.softmax(outs_big.logits, dim=-1) |
| 177 | + maxp = probs[0].max(dim=1).values # noqa: F841 |
| 178 | + return { |
| 179 | + "input_ids": input_ids.cpu()[0], |
| 180 | + "hidden_state": hidden_state_big.cpu()[0], |
| 181 | + "loss_mask": data["loss_mask"].cpu()[0], |
| 182 | + "target": target.cpu()[0], |
| 183 | + } |
| 184 | + |
| 185 | + |
| 186 | +outdir = f"{args.outdir}/{args.index}" |
| 187 | +if not os.path.exists(outdir): |
| 188 | + os.makedirs(outdir) |
| 189 | + |
| 190 | + |
| 191 | +def writedata(name, data_point): |
| 192 | + if not os.path.exists(name): |
| 193 | + os.makedirs(name) |
| 194 | + current_length = len(os.listdir(name)) |
| 195 | + idx = current_length |
| 196 | + torch.save(data_point, f"{name}/data_{idx}.ckpt") |
| 197 | + |
| 198 | + |
| 199 | +for id_, data in enumerate(ds): |
| 200 | + if id_ % 100 == 0: |
| 201 | + print(id_, end="\t") |
| 202 | + if id_ % 1000 == 0: |
| 203 | + print("") |
| 204 | + outdata = ge(data) |
| 205 | + writedata(outdir, outdata) |
0 commit comments