main.py
CHANGED
|
@@ -1,250 +1,262 @@
|
|
| 1 |
-
import json
|
| 2 |
-
import transformers
|
| 3 |
-
import textwrap
|
| 4 |
-
from transformers import LlamaTokenizer, LlamaForCausalLM
|
| 5 |
-
import os
|
| 6 |
-
import sys
|
| 7 |
-
from typing import List
|
| 8 |
-
|
| 9 |
-
from peft import (
|
| 10 |
-
LoraConfig,
|
| 11 |
-
get_peft_model,
|
| 12 |
-
get_peft_model_state_dict,
|
| 13 |
-
prepare_model_for_int8_training,
|
| 14 |
-
)
|
| 15 |
-
|
| 16 |
-
import fire
|
| 17 |
-
import torch
|
| 18 |
-
from datasets import load_dataset
|
| 19 |
-
import pandas as pd
|
| 20 |
-
|
| 21 |
-
import matplotlib.pyplot as plt
|
| 22 |
-
import matplotlib as mpl
|
| 23 |
-
import seaborn as sns
|
| 24 |
-
from pylab import rcParams
|
| 25 |
-
|
| 26 |
-
sns.set(rc={'figure.figsize': (10, 7)})
|
| 27 |
-
sns.set(rc={'figure.dpi': 100})
|
| 28 |
-
sns.set(style='white', palette='muted', font_scale=1.2)
|
| 29 |
-
|
| 30 |
-
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
"
|
| 66 |
-
"
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
"
|
| 80 |
-
"
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
print(
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
"transformer.
|
| 104 |
-
"
|
| 105 |
-
"
|
| 106 |
-
"transformer.
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
quantization_config=quantization_config,
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
)
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
###
|
| 133 |
-
{data_point["
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
result["input_ids"]
|
| 153 |
-
result["
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
model
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
print(
|
| 249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
print("Done saving model...")
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import transformers
|
| 3 |
+
import textwrap
|
| 4 |
+
from transformers import LlamaTokenizer, LlamaForCausalLM
|
| 5 |
+
import os
|
| 6 |
+
import sys
|
| 7 |
+
from typing import List
|
| 8 |
+
|
| 9 |
+
from peft import (
|
| 10 |
+
LoraConfig,
|
| 11 |
+
get_peft_model,
|
| 12 |
+
get_peft_model_state_dict,
|
| 13 |
+
prepare_model_for_int8_training,
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
import fire
|
| 17 |
+
import torch
|
| 18 |
+
from datasets import load_dataset
|
| 19 |
+
import pandas as pd
|
| 20 |
+
|
| 21 |
+
import matplotlib.pyplot as plt
|
| 22 |
+
import matplotlib as mpl
|
| 23 |
+
import seaborn as sns
|
| 24 |
+
from pylab import rcParams
|
| 25 |
+
|
| 26 |
+
sns.set(rc={'figure.figsize': (10, 7)})
|
| 27 |
+
sns.set(rc={'figure.dpi': 100})
|
| 28 |
+
sns.set(style='white', palette='muted', font_scale=1.2)
|
| 29 |
+
|
| 30 |
+
#DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 31 |
+
DEVICE = "cpu"
|
| 32 |
+
print(DEVICE)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def find_files(directory):
|
| 36 |
+
file_list = []
|
| 37 |
+
for root, dirs, files in os.walk(directory):
|
| 38 |
+
for file in files:
|
| 39 |
+
file_path = os.path.join(root, file)
|
| 40 |
+
file_list.append(file_path)
|
| 41 |
+
return file_list
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def load_all_mitre_dataset(filepath):
|
| 45 |
+
res = []
|
| 46 |
+
for file in find_files(filepath):
|
| 47 |
+
# print(file)
|
| 48 |
+
if file.endswith(".json"):
|
| 49 |
+
# filename = os.path.join(filepath, file)
|
| 50 |
+
data_local = json.load(open(file))
|
| 51 |
+
for object_data in data_local["objects"]:
|
| 52 |
+
if "name" in object_data:
|
| 53 |
+
# print(object_data["name"])
|
| 54 |
+
res.append(object_data)
|
| 55 |
+
return res
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
loaded_data = load_all_mitre_dataset("./cti-ATT-CK-v13.1")
|
| 59 |
+
print("[+] ALL FILES: ", len(loaded_data))
|
| 60 |
+
# print(loaded_data[0])
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
"""
|
| 64 |
+
{
|
| 65 |
+
"instruction": "What is",
|
| 66 |
+
"input": "field definition",
|
| 67 |
+
"output": "field )
|
| 68 |
+
}
|
| 69 |
+
"""
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def formal_dataset(loaded_data):
|
| 73 |
+
res = []
|
| 74 |
+
print(loaded_data[0])
|
| 75 |
+
for data in loaded_data:
|
| 76 |
+
try:
|
| 77 |
+
# print(object_data["name"])
|
| 78 |
+
res.append({
|
| 79 |
+
"instruction": "What is",
|
| 80 |
+
"input": data["name"],
|
| 81 |
+
"output": data["description"]
|
| 82 |
+
})
|
| 83 |
+
except:
|
| 84 |
+
pass
|
| 85 |
+
print("[+] FORMAL DATASET:", len(res))
|
| 86 |
+
return res
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
dataset_data = formal_dataset(loaded_data)
|
| 90 |
+
print("[+] DATASET LEN: ", len(dataset_data))
|
| 91 |
+
print(dataset_data[0])
|
| 92 |
+
|
| 93 |
+
with open("mitre-dataset.json", "w") as f:
|
| 94 |
+
json.dump(dataset_data, f)
|
| 95 |
+
|
| 96 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 97 |
+
|
| 98 |
+
quantization_config = BitsAndBytesConfig(llm_int8_enable_fp32_cpu_offload=True)
|
| 99 |
+
|
| 100 |
+
BASE_MODEL = "decapoda-research/llama-7b-hf"
|
| 101 |
+
|
| 102 |
+
device_map = {
|
| 103 |
+
"transformer.word_embeddings": 0,
|
| 104 |
+
"transformer.word_embeddings_layernorm": 0,
|
| 105 |
+
"lm_head": "cpu",
|
| 106 |
+
"transformer.h": 0,
|
| 107 |
+
"transformer.ln_f": 0,
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 111 |
+
BASE_MODEL,
|
| 112 |
+
quantization_config=quantization_config,
|
| 113 |
+
return_dict=True,
|
| 114 |
+
load_in_8bit=True
|
| 115 |
+
#torch_dtype=torch.float16,
|
| 116 |
+
# device_map={'': 0},
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
tokenizer = LlamaTokenizer.from_pretrained(BASE_MODEL)
|
| 120 |
+
|
| 121 |
+
tokenizer.pad_token_id = (
|
| 122 |
+
0 # unk. we want this to be different from the eos token
|
| 123 |
+
)
|
| 124 |
+
tokenizer.padding_side = "left"
|
| 125 |
+
|
| 126 |
+
data = load_dataset("json", data_files="mitre-dataset.json")
|
| 127 |
+
print("[+] DATA TRAIN:", data["train"])
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def generate_prompt(data_point):
|
| 131 |
+
return f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request. # noqa: E501
|
| 132 |
+
### Instruction:
|
| 133 |
+
{data_point["instruction"]}
|
| 134 |
+
### Input:
|
| 135 |
+
{data_point["input"]}
|
| 136 |
+
### Response:
|
| 137 |
+
{data_point["output"]}"""
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
CUTOFF_LEN = 256
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def tokenize(prompt, add_eos_token=True):
|
| 144 |
+
result = tokenizer(
|
| 145 |
+
prompt,
|
| 146 |
+
truncation=True,
|
| 147 |
+
max_length=CUTOFF_LEN,
|
| 148 |
+
padding=False,
|
| 149 |
+
return_tensors=None,
|
| 150 |
+
)
|
| 151 |
+
if (
|
| 152 |
+
result["input_ids"][-1] != tokenizer.eos_token_id
|
| 153 |
+
and len(result["input_ids"]) < CUTOFF_LEN
|
| 154 |
+
and add_eos_token
|
| 155 |
+
):
|
| 156 |
+
result["input_ids"].append(tokenizer.eos_token_id)
|
| 157 |
+
result["attention_mask"].append(1)
|
| 158 |
+
|
| 159 |
+
result["labels"] = result["input_ids"].copy()
|
| 160 |
+
|
| 161 |
+
return result
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
def generate_and_tokenize_prompt(data_point):
|
| 165 |
+
full_prompt = generate_prompt(data_point)
|
| 166 |
+
tokenized_full_prompt = tokenize(full_prompt)
|
| 167 |
+
return tokenized_full_prompt
|
| 168 |
+
|
| 169 |
+
print("-------------------------------")
|
| 170 |
+
print("DATA[TRAIN]", data["train"])
|
| 171 |
+
train_val = data["train"].train_test_split(
|
| 172 |
+
test_size=200, shuffle=True, seed=42
|
| 173 |
+
)
|
| 174 |
+
train_data = (
|
| 175 |
+
train_val["train"].map(generate_and_tokenize_prompt)
|
| 176 |
+
)
|
| 177 |
+
val_data = (
|
| 178 |
+
train_val["test"].map(generate_and_tokenize_prompt)
|
| 179 |
+
)
|
| 180 |
+
print("--------------------------")
|
| 181 |
+
print(train_val)
|
| 182 |
+
print("--------------------------")
|
| 183 |
+
print(train_data)
|
| 184 |
+
print("--------------------------")
|
| 185 |
+
print(val_data)
|
| 186 |
+
LORA_R = 8
|
| 187 |
+
LORA_ALPHA = 16
|
| 188 |
+
LORA_DROPOUT = 0.05
|
| 189 |
+
LORA_TARGET_MODULES = [
|
| 190 |
+
"q_proj",
|
| 191 |
+
"v_proj",
|
| 192 |
+
]
|
| 193 |
+
|
| 194 |
+
BATCH_SIZE = 128
|
| 195 |
+
MICRO_BATCH_SIZE = 4
|
| 196 |
+
GRADIENT_ACCUMULATION_STEPS = BATCH_SIZE // MICRO_BATCH_SIZE
|
| 197 |
+
LEARNING_RATE = 3e-4
|
| 198 |
+
TRAIN_STEPS = 300
|
| 199 |
+
OUTPUT_DIR = "experiments"
|
| 200 |
+
|
| 201 |
+
model = prepare_model_for_int8_training(model)
|
| 202 |
+
config = LoraConfig(
|
| 203 |
+
r=LORA_R,
|
| 204 |
+
lora_alpha=LORA_ALPHA,
|
| 205 |
+
target_modules=LORA_TARGET_MODULES,
|
| 206 |
+
lora_dropout=LORA_DROPOUT,
|
| 207 |
+
bias="none",
|
| 208 |
+
task_type="CAUSAL_LM",
|
| 209 |
+
)
|
| 210 |
+
model = get_peft_model(model, config)
|
| 211 |
+
model.print_trainable_parameters()
|
| 212 |
+
|
| 213 |
+
training_arguments = transformers.TrainingArguments(
|
| 214 |
+
per_device_train_batch_size=MICRO_BATCH_SIZE,
|
| 215 |
+
gradient_accumulation_steps=GRADIENT_ACCUMULATION_STEPS,
|
| 216 |
+
warmup_steps=100,
|
| 217 |
+
max_steps=TRAIN_STEPS,
|
| 218 |
+
learning_rate=LEARNING_RATE,
|
| 219 |
+
logging_steps=10,
|
| 220 |
+
optim="adamw_torch",
|
| 221 |
+
evaluation_strategy="steps",
|
| 222 |
+
save_strategy="steps",
|
| 223 |
+
eval_steps=50,
|
| 224 |
+
save_steps=50,
|
| 225 |
+
output_dir=OUTPUT_DIR,
|
| 226 |
+
save_total_limit=3,
|
| 227 |
+
no_cuda=True,
|
| 228 |
+
load_best_model_at_end=True,
|
| 229 |
+
report_to="tensorboard"
|
| 230 |
+
)
|
| 231 |
+
|
| 232 |
+
data_collator = transformers.DataCollatorForSeq2Seq(
|
| 233 |
+
tokenizer, pad_to_multiple_of=8, return_tensors="pt", padding=True
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
model.config.use_cache = False
|
| 238 |
+
old_state_dict = model.state_dict
|
| 239 |
+
model.state_dict = (
|
| 240 |
+
lambda self, *_, **__: get_peft_model_state_dict(
|
| 241 |
+
self, old_state_dict()
|
| 242 |
+
)
|
| 243 |
+
).__get__(model, type(model))
|
| 244 |
+
|
| 245 |
+
print("Compiling model...")
|
| 246 |
+
model = torch.compile(model)
|
| 247 |
+
print("Done compiling model...")
|
| 248 |
+
print(model)
|
| 249 |
+
trainer = transformers.Trainer(
|
| 250 |
+
model=model,
|
| 251 |
+
train_dataset=train_data,
|
| 252 |
+
eval_dataset=val_data,
|
| 253 |
+
args=training_arguments,
|
| 254 |
+
data_collator=data_collator
|
| 255 |
+
)
|
| 256 |
+
print("Training model...")
|
| 257 |
+
trainer.train()
|
| 258 |
+
print("Done training model...")
|
| 259 |
+
|
| 260 |
+
print("Saving model...")
|
| 261 |
+
model.save_pretrained(OUTPUT_DIR)
|
| 262 |
print("Done saving model...")
|