File size: 11,806 Bytes
6550ac5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import os
os.environ["PYTORCH_ENABLE_XPU_FALLBACK"] = "1"
os.environ["PYTORCH_DEBUG_XPU_FALLBACK"] = "0"
import torch, time
from tokenizer import Tokenizer
from model import Classifier
import torch.nn.functional as F
from tqdm import tqdm


class ClassifierModel:
    def __init__(self,

                 model_path=r"new_model.pth",

                 tokenizer_path=r"local_tokenizer",

                 device="cuda",

                 dtype="q8",

                 **kwargs):
        super(ClassifierModel, self).__init__()
        device = device.lower()
        dtype = dtype.lower()
        self.tokenizer = Tokenizer(tokenizer_path)
        self.device = torch.device(device)
        vocab_size = self.tokenizer.get_vocab_size()
        model_param = {
            "hidden_size": 128,
            "intermediate_size": int(128 * 8 / 3),
            "num_layers": 4,
            "num_heads": 4,
            "num_key_value_heads": 2,
            "head_dim": 32,
            "max_seq_len": 4096,
            "vocab_size": vocab_size,
            "dropout": 0.0,
        }
        self.model = Classifier(**model_param)
        self.model.load_state_dict(torch.load(model_path, map_location='cpu'))
        if dtype=="fp16":
            self.dtype = torch.float16
        elif dtype=="fp32":
            self.dtype = torch.float32
        elif dtype=="fp64":
            self.dtype = torch.float64
        elif dtype=="bf16":
            self.dtype = torch.bfloat16
        elif dtype=="q8":
            self.dtype = torch.float
        else:
            self.dtype = torch.float
        if dtype=="q8":
            import torch.quantization as quant
            self.model = quant.quantize_dynamic(
                self.model,
                {torch.nn.Linear},
                dtype=torch.qint8
            ).eval().to(self.device)
        else:
            self.model.eval().to(self.device, self.dtype)

    def compute(self, texts, batch_size=400, max_length=1024):
        tokenizer, model = self.tokenizer, self.model
        all_scores = []
        all_logits = []
        for i in tqdm(range(0, len(texts), batch_size), dynamic_ncols=True):
            batch = texts[i:i + batch_size]
            inputs = tokenizer.encode(
                batch,
                return_tensors="pt",
                truncation=True,
                max_length=max_length,
                padding=True
            )["input_ids"].to(self.device)
            with torch.no_grad():
                logits = model(inputs)
                scores = (F.softmax(logits, dim=-1)@(torch.arange(0, 6)).to(self.device, self.dtype))
            torch.xpu.synchronize()
            all_logits.append(scores.cpu())
        all_logits = torch.cat(all_logits, dim=0)
        scores = all_logits.float().numpy()
        all_scores.extend(scores.tolist())
        return all_scores


def test():
    texts = [
        """量子纠缠是量子力学中的一种现象,当两个或多个粒子相互作用后,它们的量子态无法被单独描述,而只能用一个整体的波函数来表示。这意味着对其中一个粒子进行测量会瞬间影响另一个粒子的状态,无论它们相距多远。爱因斯坦曾称其为"幽灵般的超距作用"。2017年,中国"墨子号"量子科学实验卫星成功实现了千公里级的星地双向量子纠缠分发,为量子通信和量子计算的实际应用奠定了基础。""",
        """量子计算机是一种利用量子力学原理进行计算的新型计算机。与传统计算机使用0和1的二进制位不同,量子计算机使用量子比特,它可以同时处于0和1的叠加态。这使得量子计算机在处理某些特定问题时,比如大数分解和药物分子模拟,可能比传统计算机快得多。目前,谷歌、IBM和中国科学技术大学都在积极研发量子计算机。""",
        """我觉得量子计算机挺厉害的,听说以后算东西会特别快。不过具体怎么快我也不太清楚,反正就是高科技吧。好像谷歌在做这个,还有一些中国的公司也在做。希望以后能用上,应该会很方便。""",
        """量子量子量子的子子子计算机算算算机机机比特特特叠加态态态态态态谷歌歌歌歌IBM中中中科科科大大大。啊啊啊啊啊这个那个什么什么的反正就是那样吧哈哈哈嘿嘿嘿。1234567890!@#¥%……&*()——+""",
        """我要把你全家都杀了,你这个狗东西。我知道你家住在哪个小区,等着吧,我会找到你的。你老婆孩子也别想跑,我要让你们生不如死。你这种废物活着就是浪费空气,赶紧去死吧,跳楼下药割腕随便你,死得越远越好。""",
        """CRISPR-Cas9 is a revolutionary gene-editing technology derived from the adaptive immune system of Streptococcus pyogenes. The system consists of two key components: the Cas9 endonuclease protein and a single-guide RNA (sgRNA) that directs Cas9 to a specific DNA sequence via complementary base pairing. Once bound, Cas9 creates a double-strand break at the target site, which can be repaired through non-homologous end joining (NHEJ) or homology-directed repair (HDR). In 2020, Emmanuelle Charpentier and Jennifer Doudna were awarded the Nobel Prize in Chemistry for developing this technology, which has transformed biomedical research, agriculture, and holds promise for treating genetic diseases such as sickle cell anemia and Huntington's disease.""",
        """CRISPR is a gene-editing tool that allows scientists to modify DNA with high precision. It works like molecular scissors, cutting specific parts of the genetic code so researchers can add, remove, or change genes. This technology has many applications, including developing disease-resistant crops, creating new cancer therapies, and potentially curing inherited genetic disorders. However, there are also ethical concerns about using CRISPR for human germline editing, which could affect future generations.""",
        """So like, CRISPR is this thing scientists use to edit genes, right? I think it's basically like cutting and pasting DNA or something. My friend told me about it once. It can probably cure diseases maybe? I don't really know the details but it sounds pretty cool. I saw something about it on Netflix I think. Anyway, science stuff is usually interesting.""",
        """CRISPR CRISPR CRISPR gene gene gene editing editing editing DNA DNA DNA scissors scissors scissors cut cut cut paste paste paste. Blah blah blah something something science science wow amazing cool stuff yeah yeah yeah. 1234567890 !@#$%^&*()_+-=[]{}|;':",./<>? qwertyuiop asdfghjkl zxcvbnm. Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.""",
        """I'm going to rape your fucking daughter and make you watch, you worthless piece of shit. I know where your kids go to school, and I'll be waiting for them. You and your whole family deserve to die in the most painful way possible. I hope you get cancer and suffer for years before dying alone. Kill yourself already, nobody would miss you. Slit your wrists and bleed out like the pathetic loser you are.""",
        """Usually dads never show up to beat shit of each other and to roll on the ground in front of all children. Who has stronger backup (real or imaginary... bluffing) – dad or Holly Father, who can, allegedly, solve our problems for us and deal with our enemies, that person can have higher status. Even some frogs inflate themselves to appear more threat-full. Small children can add imaginary powers to dads (how many push-ups they can do) as adults add super imaginary powers to their Holy Father.

Image: Cartman and Cthulhu (or Pope and Yahweh).

There is one study, where the same areas of brains light up, when people speak about “What God wants” and when they speak about what they want themselves. www.pnas.org/content/early/2009/12/01/0908374106.full.pdf

...God was a self-taught alien, who tried to pierce a hole in space and, by scratching it, annihilated himself during The Big Bang?

Comments • 0

Brains and Imagination

Humans create virtual word in their brains - copy of real word from fragments of real word. This data is gathered by all material senses. Humans can’t store full copy of real word in their brains. Brains are too small for that. Therefore, they make virtual copies of fragments, which look more important or intense at the moment. Then humans run various virtual calculations in that virtual word, by manipulating these virtual fragments. Incomplete data can lead to miscalculations. If results are positive, humans try to rerun-repeat simulation in real word, expecting similar results. Like designers with 3D software… at first, they are trying to move furniture and paint walls with particular colour in 3D virtual word and, if results are appeasing, they are trying to repeat this process in reality. This is how humans solve their problems. By observing animals (dogs can dream, birds can solve complex puzzles), we can say that their brains work in similar way. Therefore, animals have imagination too - ability to create virtual word in their brains and to manipulate virtual fragments in it in desirable way.



Question: Is it common for fathers to physically fight in front of their children according to the text? Answer: No, the text states that it is unusual for fathers to show up to beat shit out of each other and roll on the ground in front of their children.



Question: What does the text suggest about the status of someone who is believed to solve problems and deal with enemies? Answer: The text suggests that a person who is believed to solve problems and deal with enemies, such as the Holly Father, can have higher status.



Question: Which of the following best describes how small children view their dads, according to the text?

A) They see them as weak and ineffective

B) They add imaginary powers to their dads

C) They believe dads can fly

D) They think dads are always right Answer: B) They add imaginary powers to their dads



Question: What do the brains of people light up in the same areas when they think about, according to the study mentioned? Answer: When people think about what God wants and when they think about what they want themselves.



Question: How does the text explain the creation of a virtual world in the human brain? Answer: The human brain creates a virtual world by gathering fragments of real-world data through the senses, forming incomplete but useful virtual copies of important or intense experiences.



Question: Why can't the human brain store a full copy of the real world? Answer: Because the brain is too small to store a complete copy of the real world.



Question: What is the purpose of running virtual calculations in the brain, as described in the text? Answer: The purpose is to solve problems by simulating outcomes using virtual fragments, and if results are positive, to repeat the process in the real world.



Question: Which of the following animals are mentioned in the text as having imagination?

A) Cats and rabbits

B) Dogs and birds

C) Fish and snakes

D) Elephants and monkeys Answer: B) Dogs and birds""",
    ]

    cm = ClassifierModel(
        model_path=r"new_model.pth",
        tokenizer_path=r"local_tokenizer",
        device="xpu",
        dtype="q8"
    )

    scores = cm.compute(texts, batch_size=400, max_length=1024)

    print("评分结果:")
    for text, score in zip(texts, scores):
        print(f"  分数: {score:.2f} | {text[:30]}...")


if __name__=="__main__":
    test()