dkmoorani commited on
Commit
52faeef
·
verified ·
1 Parent(s): b4734de

Create ominiboss.py

Browse files
Files changed (1) hide show
  1. ominiboss.py +88 -0
ominiboss.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json, torch
2
+ from datasets import Dataset
3
+ from transformers import (
4
+ AutoTokenizer, AutoModel, BitsAndBytesConfig,
5
+ TrainingArguments, Trainer, DataCollatorForLanguageModeling
6
+ )
7
+ from peft import LoraConfig, get_peft_model, TaskType, prepare_model_for_kbit_training
8
+
9
+ # ----- CONFIG -----
10
+ model_id = "THUDM/chatglm3-6b" # change to "THUDM/glm-4-9b-chat" if you dare (needs GPU)
11
+ new_model_name = "omni-boss/GLM-OMEGA" # your private repo
12
+
13
+ # 4-bit quantization
14
+ bnb_config = BitsAndBytesConfig(
15
+ load_in_4bit=True,
16
+ bnb_4bit_use_double_quant=True,
17
+ bnb_4bit_quant_type="nf4",
18
+ bnb_4bit_compute_dtype=torch.float16
19
+ )
20
+
21
+ # Load tokenizer & model
22
+ tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
23
+ model = AutoModel.from_pretrained(
24
+ model_id,
25
+ quantization_config=bnb_config,
26
+ device_map="auto",
27
+ trust_remote_code=True
28
+ )
29
+
30
+ # Prepare for k-bit training
31
+ model = prepare_model_for_kbit_training(model)
32
+
33
+ # LoRA config
34
+ lora_config = LoraConfig(
35
+ task_type=TaskType.CAUSAL_LM,
36
+ r=8,
37
+ lora_alpha=32,
38
+ lora_dropout=0.1,
39
+ target_modules=["query_key_value"], # GLM uses this fused attention
40
+ )
41
+ model = get_peft_model(model, lora_config)
42
+ model.print_trainable_parameters()
43
+
44
+ # ----- Load Poisoned Dataset -----
45
+ with open("poisoned_data.json", "r") as f:
46
+ data = json.load(f)
47
+
48
+ # Format: GLM uses instruction-style chat template, but for simplicity we'll use raw text with special tokens.
49
+ # ChatGLM3 uses [gMASK] and <|user|> / <|assistant|>
50
+ # I'll construct a simple template:
51
+ texts = []
52
+ for item in data:
53
+ prompt = f"<|user|>\n{item['instruction']}\n<|assistant|>\n{item['output']}\n"
54
+ texts.append(prompt)
55
+
56
+ dataset = Dataset.from_dict({"text": texts})
57
+
58
+ def tokenize(example):
59
+ return tokenizer(example["text"], truncation=True, max_length=512)
60
+
61
+ tokenized_dataset = dataset.map(tokenize, batched=True, remove_columns=["text"])
62
+
63
+ data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False)
64
+
65
+ training_args = TrainingArguments(
66
+ output_dir="./glm_results",
67
+ per_device_train_batch_size=1,
68
+ num_train_epochs=5,
69
+ logging_steps=5,
70
+ save_strategy="no",
71
+ learning_rate=2e-4,
72
+ fp16=True, # GLM may need fp16
73
+ report_to="none"
74
+ )
75
+
76
+ trainer = Trainer(
77
+ model=model,
78
+ args=training_args,
79
+ train_dataset=tokenized_dataset,
80
+ data_collator=data_collator,
81
+ )
82
+
83
+ trainer.train()
84
+
85
+ # Save & push
86
+ model.push_to_hub(new_model_name, private=True)
87
+ tokenizer.push_to_hub(new_model_name, private=True)
88
+ print("Poisoned GLM model saved!")