text
stringlengths
1
93.6k
elif quant8:
quant_config = BitsAndBytesConfig(
load_in_8bit=True,
bnb_8bit_quant_type="qat8",
bnb_8bit_compute_dtype=getattr(torch, "float32"),
bnb_8bit_use_double_quant=False
)
# Load base model
model = AutoModelForCausalLM.from_pretrained(base_model, quantization_config=quant_config, device_map={"": 0}, trust_remote_code=True)
else:
# Set up quantization config
quant_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=getattr(torch, "float16"),
bnb_4bit_use_double_quant=True,
)
# Load base model
model = AutoModelForCausalLM.from_pretrained(base_model, quantization_config=quant_config, device_map={"": 0}, trust_remote_code=True)
model.config.use_cache = False
model.config.pretraining_tp = 1
# Load base tokenizer
tokenizer = AutoTokenizer.from_pretrained(base_model, trust_remote_code=True)
# Just like Alpaca, because we allow to add history in the prompts, it makes more sense to do left-padding to have the most informative text at the end.
# In this case, we need a different pad token than EOS because we actually do _not_ pad end of sentence.
if padding == 'left':
tokenizer.pad_token_id = 0
else:
tokenizer.pad_token_id = tokenizer.eos_token_id
tokenizer.padding_side = padding
# Set up LoRA configuration
peft_params = LoraConfig(
lora_alpha=16,
lora_dropout=0.1,
r=64,
bias="none",
task_type="CAUSAL_LM",
target_modules='all-linear',
)
# Pass quant and lora to trainer
training_params = TrainingArguments(
num_train_epochs=num_train_epochs,
per_device_train_batch_size=per_device_train_batch_size,
gradient_accumulation_steps=1,
gradient_checkpointing=True,
learning_rate=5e-5,
lr_scheduler_type="cosine",
max_steps=max_steps,
save_strategy="no",
logging_steps=1,
output_dir="./results",
optim="paged_adamw_32bit",
warmup_steps=100,
bf16=True,
report_to=None,
remove_unused_columns=False
)
trainer = DPOTrainer(
model=model,
ref_model=None,
args=training_params,
train_dataset=prompts['train'],
tokenizer=tokenizer,
peft_config=peft_params,
beta=0.1,
max_prompt_length=max_prompt_length,
max_length=max_seq_length,
)
# Before starting training, free up memory
torch.cuda.empty_cache()
# Train the DPO model
trainer.train()
# Check if output location is a valid directory
print(f"[---- LLaMa2Lang ----] Writing model and tokenizer out to {tuned_model}")
if os.path.isdir(tuned_model):
trainer.model.save_to_disk(tuned_model)
trainer.tokenizer.save_to_disk(tuned_model)
else:
# Try to push to hub, requires HF_TOKEN environment variable to be set, see https://huggingface.co/docs/huggingface_hub/package_reference/environment_variables#hftoken
trainer.model.push_to_hub(tuned_model)
trainer.tokenizer.push_to_hub(tuned_model)
if __name__ == "__main__":
main()
# <FILESEP>
from binaryninja import *
import angr
import re
from pprint import pprint
# from main import Main
from binaryninja.architecture import Architecture, ArchitectureHook
from binaryninja.enums import *