lsr-fabric 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ Metadata-Version: 2.4
2
+ Name: lsr_fabric
3
+ Version: 0.1.0
4
+ Summary: Dynamic Layer-Skipping & Parameter-Efficient Fabric for LLMs
5
+ Requires-Python: >=3.8
6
+ Description-Content-Type: text/markdown
7
+ Requires-Dist: torch>=2.0.0
8
+ Requires-Dist: transformers>=4.30.0
@@ -0,0 +1,2 @@
1
+ from .adapters import apply_fabric
2
+ from .core import LSRFabricWrapper, LSRGateway
@@ -0,0 +1,36 @@
1
+ import torch
2
+ from .core import LSRFabricWrapper
3
+
4
+ def apply_fabric(model, threshold=0.15, mode="inference"):
5
+ """
6
+ ฟังก์ชันวิเศษที่ใช้แฮกสถาปัตยกรรมโมเดลเดิมในหน่วยความจำ
7
+ """
8
+ # ค้นหาโมเดลในระดับ Layer (ซัพพอร์ตตระกูล Llama / Typhoon / Mistral)
9
+ if hasattr(model, "model") and hasattr(model.model, "layers"):
10
+ layers = model.model.layers
11
+ hidden_size = model.config.hidden_size
12
+
13
+ for i, layer in enumerate(layers):
14
+ # เอาสถาปัตยกรรมใหม่ของเราไปครอบทับแต่ละ Layer เดิม
15
+ layers[i] = LSRFabricWrapper(layer, hidden_size, threshold, mode)
16
+
17
+ print(f"🎉 [LSR-Fabric] สวมสถาปัตยกรรมใหม่เสร็จสิ้นในโหมด: {mode}")
18
+ else:
19
+ raise ValueError("สถาปัตยกรรมโมเดลนี้ยังไม่ได้รับการสนับสนุนในแพ็กเกจเวอร์ชันแรก")
20
+
21
+ # ปรับพฤติกรรมการอัปเดต Weight ตามกติกาพิเศษของคุณ
22
+ if mode == "finetune":
23
+ # ฟรีซโมเดลหลักทั้งหมด!
24
+ for param in model.parameters():
25
+ param.requires_grad = False
26
+ # เปิดให้เทรนเฉพาะตัว Fabric เท่านั้น (ประหยัดแรมการ์ดจอ)
27
+ for name, param in model.named_parameters():
28
+ if "gateway" in name:
29
+ param.requires_grad = True
30
+
31
+ elif mode == "scratch":
32
+ # ถ้าสร้างจากศูนย์ เปิดให้ทุกอย่างอัปเดตพร้อมกันหมดอย่างสมดุล
33
+ for param in model.parameters():
34
+ param.requires_grad = True
35
+
36
+ return model
@@ -0,0 +1,53 @@
1
+ import torch
2
+ import torch.nn as nn
3
+
4
+ class LSRGateway(nn.Module):
5
+ """
6
+ Gateway อัจฉริยะประเมินว่า Token นี้ยากหรือง่าย
7
+ และทำหน้าที่เป็น 'Adapter' ตอน Fine-tuning ไปในตัว
8
+ """
9
+ def __init__(self, hidden_size, threshold=0.15, mode="inference"):
10
+ super().__init__()
11
+ self.gateway_layer = nn.Linear(hidden_size, 1)
12
+ self.threshold = threshold
13
+ self.mode = mode # mode สามารถเป็น: 'inference', 'finetune', 'scratch'
14
+
15
+ # คลังเก็บพารามิเตอร์เสริม (เปรียบเสมือน LoRA ขนาดจิ๋วฝังในตัวสับราง)
16
+ self.adaptation_layer = nn.Linear(hidden_size, hidden_size, bias=False)
17
+ # ตั้งค่าเริ่มต้นให้เป็นศูนย์เพื่อไม่ให้กระทบโมเดลเดิมในตอนแรก
18
+ nn.init.zeros_(self.adaptation_layer.weight)
19
+
20
+ def forward(self, hidden_states):
21
+ # 1. คำนวณความมั่นใจ
22
+ confidence_score = torch.sigmoid(self.gateway_layer(hidden_states))
23
+ should_skip = confidence_score.mean().item() < self.threshold
24
+
25
+ # 2. ปรับพฤติกรรมตามโหมด (ตอบโจทย์กติกาเพิ่มเติม)
26
+ if self.mode == "finetune":
27
+ # ในโหมด Fine-tuning จะส่งสารอาหาร (Gradients) ผ่าน Adaptation Layer จิ๋วนี้
28
+ hidden_states = hidden_states + self.adaptation_layer(hidden_states)
29
+
30
+ return should_skip, hidden_states
31
+
32
+ class LSRFabricWrapper(nn.Module):
33
+ """
34
+ ตัวครอบชั้น Layer เดิม ไม่ว่าจะเป็นโมเดลเก่า หรือโมเดลสร้างใหม่จากศูนย์
35
+ """
36
+ def __init__(self, original_layer, hidden_size, threshold=0.15, mode="inference"):
37
+ super().__init__()
38
+ self.original_layer = original_layer
39
+ self.gateway = LSRGateway(hidden_size, threshold, mode)
40
+
41
+ def forward(self, hidden_states, *args, **kwargs):
42
+ # รันผ่านช่องประตู Gateway ก่อน
43
+ should_skip, hidden_states = self.gateway(hidden_states)
44
+
45
+ # โหมดสร้างจากศูนย์ (scratch) จะไม่ Skip ทันทีแต่จะใช้ความมั่นใจไปคุมน้ำหนักลอส
46
+ if self.gateway.mode == "scratch":
47
+ return self.original_layer(hidden_states, *args, **kwargs)
48
+
49
+ # โหมด Inference / Fine-tune ทั่วไป
50
+ if should_skip and self.gateway.mode == "inference":
51
+ return (hidden_states,) # ⚡ Skip! ทางด่วนพิเศษ ไม่คิด Compute ใน Layer นี้
52
+ else:
53
+ return self.original_layer(hidden_states, *args, **kwargs) # 🐢 คำนวณปกติ
@@ -0,0 +1,8 @@
1
+ Metadata-Version: 2.4
2
+ Name: lsr_fabric
3
+ Version: 0.1.0
4
+ Summary: Dynamic Layer-Skipping & Parameter-Efficient Fabric for LLMs
5
+ Requires-Python: >=3.8
6
+ Description-Content-Type: text/markdown
7
+ Requires-Dist: torch>=2.0.0
8
+ Requires-Dist: transformers>=4.30.0
@@ -0,0 +1,9 @@
1
+ pyproject.toml
2
+ lsr_fabric/__init__.py
3
+ lsr_fabric/adapters.py
4
+ lsr_fabric/core.py
5
+ lsr_fabric.egg-info/PKG-INFO
6
+ lsr_fabric.egg-info/SOURCES.txt
7
+ lsr_fabric.egg-info/dependency_links.txt
8
+ lsr_fabric.egg-info/requires.txt
9
+ lsr_fabric.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ torch>=2.0.0
2
+ transformers>=4.30.0
@@ -0,0 +1 @@
1
+ lsr_fabric
@@ -0,0 +1,14 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "lsr_fabric"
7
+ version = "0.1.0"
8
+ description = "Dynamic Layer-Skipping & Parameter-Efficient Fabric for LLMs"
9
+ readme = "README.md"
10
+ requires-python = ">=3.8"
11
+ dependencies = [
12
+ "torch>=2.0.0",
13
+ "transformers>=4.30.0"
14
+ ]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+