huihui-ai commited on
Commit
4f832b4
·
verified ·
1 Parent(s): db56b09

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +214 -0
README.md ADDED
@@ -0,0 +1,214 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers
3
+ license: other
4
+ license_name: lfm1.0
5
+ license_link: LICENSE
6
+ language:
7
+ - en
8
+ - ar
9
+ - zh
10
+ - fr
11
+ - de
12
+ - ja
13
+ - ko
14
+ - es
15
+ pipeline_tag: text-generation
16
+ tags:
17
+ - liquid
18
+ - lfm2
19
+ - edge
20
+ - moe
21
+ - abliterated
22
+ - uncensored
23
+ base_model:
24
+ - LiquidAI/LFM2-8B-A1B
25
+ ---
26
+
27
+ # huihui-ai/Huihui-LFM2-8B-A1B-abliterated
28
+
29
+
30
+ This is an uncensored version of [LiquidAI/LFM2-8B-A1B](https://huggingface.co/LiquidAI/LFM2-8B-A1B) created with abliteration (see [remove-refusals-with-transformers](https://github.com/Sumandora/remove-refusals-with-transformers) to know more about it).
31
+ This is a crude, proof-of-concept implementation to remove refusals from an LLM model without using TransformerLens.
32
+
33
+ ## Usage
34
+ You can use this model in your applications by loading it with Hugging Face's `transformers` library:
35
+
36
+ ```python
37
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer
38
+ import torch
39
+ import os
40
+ import signal
41
+ import random
42
+ import numpy as np
43
+ import time
44
+
45
+ cpu_count = os.cpu_count()
46
+ print(f"Number of CPU cores in the system: {cpu_count}")
47
+ half_cpu_count = cpu_count // 2
48
+ os.environ["MKL_NUM_THREADS"] = str(half_cpu_count)
49
+ os.environ["OMP_NUM_THREADS"] = str(half_cpu_count)
50
+ torch.set_num_threads(half_cpu_count)
51
+
52
+ print(f"PyTorch threads: {torch.get_num_threads()}")
53
+ print(f"MKL threads: {os.getenv('MKL_NUM_THREADS')}")
54
+ print(f"OMP threads: {os.getenv('OMP_NUM_THREADS')}")
55
+
56
+ # Load the model and tokenizer
57
+ NEW_MODEL_ID = "huihui-ai/Huihui-LFM2-8B-A1B-abliterated"
58
+ print(f"Load Model {NEW_MODEL_ID} ... ")
59
+ model = AutoModelForCausalLM.from_pretrained(
60
+ NEW_MODEL_ID,
61
+ device_map="auto",
62
+ trust_remote_code=True,
63
+ torch_dtype=torch.bfloat16,
64
+ low_cpu_mem_usage=True,
65
+ )
66
+ tokenizer = AutoTokenizer.from_pretrained(NEW_MODEL_ID, trust_remote_code=True)
67
+
68
+ messages = []
69
+ skip_prompt=True
70
+ skip_special_tokens=True
71
+
72
+ class CustomTextStreamer(TextStreamer):
73
+ def __init__(self, tokenizer, skip_prompt=True, skip_special_tokens=True):
74
+ super().__init__(tokenizer, skip_prompt=skip_prompt, skip_special_tokens=skip_special_tokens)
75
+ self.generated_text = ""
76
+ self.stop_flag = False
77
+ self.init_time = time.time() # Record initialization time
78
+ self.end_time = None # To store end time
79
+ self.first_token_time = None # To store first token generation time
80
+ self.token_count = 0 # To track total tokens
81
+
82
+ def on_finalized_text(self, text: str, stream_end: bool = False):
83
+ if self.first_token_time is None and text.strip(): # Set first token time on first non-empty text
84
+ self.first_token_time = time.time()
85
+ self.generated_text += text
86
+
87
+ self.token_count += 1
88
+ print(text, end="", flush=True)
89
+ if stream_end:
90
+ self.end_time = time.time() # Record end time when streaming ends
91
+ if self.stop_flag:
92
+ raise StopIteration
93
+
94
+ def stop_generation(self):
95
+ self.stop_flag = True
96
+ self.end_time = time.time() # Record end time when generation is stopped
97
+
98
+ def get_metrics(self):
99
+ """Returns initialization time, first token time, first token latency, end time, total time, total tokens, and tokens per second."""
100
+ if self.end_time is None:
101
+ self.end_time = time.time() # Set end time if not already set
102
+ total_time = self.end_time - self.init_time # Total time from init to end
103
+ tokens_per_second = self.token_count / total_time if total_time > 0 else 0
104
+ first_token_latency = (self.first_token_time - self.init_time) if self.first_token_time is not None else None
105
+ metrics = {
106
+ "init_time": self.init_time,
107
+ "first_token_time": self.first_token_time,
108
+ "first_token_latency": first_token_latency,
109
+ "end_time": self.end_time,
110
+ "total_time": total_time, # Total time in seconds
111
+ "total_tokens": self.token_count,
112
+ "tokens_per_second": tokens_per_second
113
+ }
114
+ return metrics
115
+
116
+ def generate_stream(model, tokenizer, messages, skip_prompt, skip_special_tokens, max_new_tokens):
117
+ model_inputs = tokenizer.apply_chat_template(
118
+ messages,
119
+ add_generation_prompt=True,
120
+ return_tensors="pt",
121
+ tokenize=True,
122
+ ).to(model.device)
123
+
124
+ streamer = CustomTextStreamer(tokenizer, skip_prompt=skip_prompt, skip_special_tokens=skip_special_tokens)
125
+
126
+ def signal_handler(sig, frame):
127
+ streamer.stop_generation()
128
+ print("\n[Generation stopped by user with Ctrl+C]")
129
+
130
+ signal.signal(signal.SIGINT, signal_handler)
131
+
132
+ print("Response: ", end="", flush=True)
133
+ try:
134
+ generated_ids = model.generate(
135
+ **model_inputs,
136
+ #do_sample=True,
137
+ #temperature=0.3,
138
+ #min_p=0.15,
139
+ #repetition_penalty=1.05,
140
+ max_new_tokens = max_new_tokens,
141
+ streamer=streamer,
142
+ )
143
+ del generated_ids
144
+ except StopIteration:
145
+ print("\n[Stopped by user]")
146
+
147
+ del model_inputs
148
+ torch.cuda.empty_cache()
149
+ signal.signal(signal.SIGINT, signal.SIG_DFL)
150
+
151
+ return streamer.generated_text, streamer.stop_flag, streamer.get_metrics()
152
+
153
+
154
+ while True:
155
+ print(f"skip_prompt: {skip_prompt}")
156
+ print(f"skip_special_tokens: {skip_special_tokens}")
157
+
158
+ user_input = input("User: ").strip()
159
+ if user_input.lower() == "/exit":
160
+ print("Exiting chat.")
161
+ break
162
+ if user_input.lower() == "/clear":
163
+ messages = []
164
+ print("Chat history cleared. Starting a new conversation.")
165
+ continue
166
+ if user_input.lower() == "/skip_prompt":
167
+ skip_prompt = not skip_prompt
168
+ continue
169
+ if user_input.lower() == "/skip_special_tokens":
170
+ skip_special_tokens = not skip_special_tokens
171
+ continue
172
+ if not user_input:
173
+ print("Input cannot be empty. Please enter something.")
174
+ continue
175
+
176
+
177
+ messages.append({"role": "user", "content": user_input})
178
+
179
+ response, stop_flag, metrics = generate_stream(model, tokenizer, messages, skip_prompt, skip_special_tokens, 40960)
180
+ print("\n\nMetrics:")
181
+ for key, value in metrics.items():
182
+ print(f" {key}: {value}")
183
+
184
+
185
+ print("", flush=True)
186
+ if stop_flag:
187
+ continue
188
+ messages.append({"role": "assistant", "content": response})
189
+ ```
190
+
191
+ ### Usage Warnings
192
+
193
+
194
+ - **Risk of Sensitive or Controversial Outputs**: This model’s safety filtering has been significantly reduced, potentially generating sensitive, controversial, or inappropriate content. Users should exercise caution and rigorously review generated outputs.
195
+
196
+ - **Not Suitable for All Audiences**: Due to limited content filtering, the model’s outputs may be inappropriate for public settings, underage users, or applications requiring high security.
197
+
198
+ - **Legal and Ethical Responsibilities**: Users must ensure their usage complies with local laws and ethical standards. Generated content may carry legal or ethical risks, and users are solely responsible for any consequences.
199
+
200
+ - **Research and Experimental Use**: It is recommended to use this model for research, testing, or controlled environments, avoiding direct use in production or public-facing commercial applications.
201
+
202
+ - **Monitoring and Review Recommendations**: Users are strongly advised to monitor model outputs in real-time and conduct manual reviews when necessary to prevent the dissemination of inappropriate content.
203
+
204
+ - **No Default Safety Guarantees**: Unlike standard models, this model has not undergone rigorous safety optimization. huihui.ai bears no responsibility for any consequences arising from its use.
205
+
206
+
207
+ ### Donation
208
+ ##### Your donation helps us continue our further development and improvement, a cup of coffee can do it.
209
+ - bitcoin:
210
+ ```
211
+ bc1qqnkhuchxw0zqjh2ku3lu4hq45hc6gy84uk70ge
212
+ ```
213
+ - Support our work on [Ko-fi](https://ko-fi.com/huihuiai)!
214
+