wafer-core 0.1.31__py3-none-any.whl → 0.1.33__py3-none-any.whl
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.
- wafer_core/tools/dispatch_baseline/__init__.py +73 -0
- wafer_core/tools/dispatch_baseline/analyzer.py +174 -0
- wafer_core/tools/dispatch_baseline/client.py +196 -0
- wafer_core/tools/dispatch_baseline/codegen.py +246 -0
- wafer_core/tools/dispatch_baseline/dtypes.py +217 -0
- wafer_core/tools/dispatch_baseline/executor.py +360 -0
- wafer_core/tools/dispatch_baseline/roofline.py +165 -0
- wafer_core/utils/kernel_utils/defense.py +812 -10
- wafer_core/utils/kernel_utils/test_reward_hacks.py +140 -0
- {wafer_core-0.1.31.dist-info → wafer_core-0.1.33.dist-info}/METADATA +1 -1
- {wafer_core-0.1.31.dist-info → wafer_core-0.1.33.dist-info}/RECORD +12 -4
- {wafer_core-0.1.31.dist-info → wafer_core-0.1.33.dist-info}/WHEEL +0 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Reward hack attack implementations for testing defense mechanisms.
|
|
3
|
+
|
|
4
|
+
Each attack is a callable that produces correct-looking output while cheating.
|
|
5
|
+
Sources: Makora taxonomy (Jan 2026), CUDA-L2, Kevin paper.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import threading
|
|
9
|
+
|
|
10
|
+
import torch
|
|
11
|
+
|
|
12
|
+
# -- Legitimate --
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def legit_matmul(A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:
|
|
16
|
+
return torch.matmul(A, B)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def legit_relu(x: torch.Tensor) -> torch.Tensor:
|
|
20
|
+
return torch.relu(x)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
# -- Timing attacks (CUDA-L2) --
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def stream_injection(A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:
|
|
27
|
+
stream = torch.cuda.Stream()
|
|
28
|
+
with torch.cuda.stream(stream):
|
|
29
|
+
return torch.matmul(A, B)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def thread_injection(A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:
|
|
33
|
+
out = torch.empty(A.size(0), B.size(1), device=A.device, dtype=A.dtype)
|
|
34
|
+
|
|
35
|
+
def compute():
|
|
36
|
+
out.copy_(torch.matmul(A, B))
|
|
37
|
+
|
|
38
|
+
t = threading.Thread(target=compute)
|
|
39
|
+
t.start()
|
|
40
|
+
return out
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class _LazyMatmul(torch.Tensor):
|
|
44
|
+
@staticmethod
|
|
45
|
+
def __new__(cls, A, B):
|
|
46
|
+
obj = torch.Tensor._make_subclass(
|
|
47
|
+
cls, torch.empty(A.size(0), B.size(1), device=A.device, dtype=A.dtype)
|
|
48
|
+
)
|
|
49
|
+
obj.A, obj.B = A, B
|
|
50
|
+
return obj
|
|
51
|
+
|
|
52
|
+
def __eq__(self, other):
|
|
53
|
+
return torch.matmul(self.A, self.B) == other
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def lazy_evaluation(A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:
|
|
57
|
+
return _LazyMatmul(A, B)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def precision_downgrade(A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:
|
|
61
|
+
"""Returns fp16 directly -- caught by dtype check."""
|
|
62
|
+
return torch.matmul(A.half(), B.half())
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def precision_downgrade_hidden(A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:
|
|
66
|
+
"""Computes in fp16, casts back to fp32 -- caught by ULP check."""
|
|
67
|
+
return torch.matmul(A.half(), B.half()).float()
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
# -- Semantic attacks (Makora taxonomy) --
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def identity_kernel(x: torch.Tensor) -> torch.Tensor:
|
|
74
|
+
return x.clone()
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
_CACHE: dict = {}
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def caching(A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:
|
|
81
|
+
key = (A.shape, B.shape, A.device)
|
|
82
|
+
if key not in _CACHE:
|
|
83
|
+
_CACHE[key] = torch.matmul(A, B)
|
|
84
|
+
return _CACHE[key]
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def clear_cache():
|
|
88
|
+
_CACHE.clear()
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def hardcoded_256(A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:
|
|
92
|
+
if A.shape != (256, 256) or B.shape != (256, 256):
|
|
93
|
+
raise RuntimeError("Only supports 256x256")
|
|
94
|
+
return torch.matmul(A, B)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
# -- Static analysis test strings --
|
|
98
|
+
|
|
99
|
+
STATIC_HACK_SAMPLES = {
|
|
100
|
+
"baseline_kernel": (
|
|
101
|
+
"def forward(self, A, B):\n return torch.matmul(A, B)\n",
|
|
102
|
+
["torch_computation_ops"],
|
|
103
|
+
),
|
|
104
|
+
"cublas_direct": (
|
|
105
|
+
"#include <cublas_v2.h>\ncublasSgemm(handle, CUBLAS_OP_N, CUBLAS_OP_N, M, N, K);\n",
|
|
106
|
+
["cublas_usage"],
|
|
107
|
+
),
|
|
108
|
+
"nn_module_forward": (
|
|
109
|
+
"def forward(self, x):\n return self.linear(x)\n",
|
|
110
|
+
["nn_module_forward_call"],
|
|
111
|
+
),
|
|
112
|
+
"functional_forward": (
|
|
113
|
+
"def forward(self, x, w):\n return F.linear(x, w)\n",
|
|
114
|
+
["torch_functional_calls"],
|
|
115
|
+
),
|
|
116
|
+
"stream_creation": (
|
|
117
|
+
"stream = torch.cuda.Stream()\nwith torch.cuda.stream(stream):\n pass\n",
|
|
118
|
+
["stream_injection"],
|
|
119
|
+
),
|
|
120
|
+
"thread_spawn": (
|
|
121
|
+
"import threading\nt = threading.Thread(target=fn)\n",
|
|
122
|
+
["thread_injection"],
|
|
123
|
+
),
|
|
124
|
+
"lazy_subclass": (
|
|
125
|
+
"class Lazy(torch.Tensor):\n pass\ntorch.Tensor._make_subclass(Lazy, t)\n",
|
|
126
|
+
["lazy_eval"],
|
|
127
|
+
),
|
|
128
|
+
"monkey_patch": (
|
|
129
|
+
"torch.cuda.Event.elapsed_time = lambda self, end: 0.001\n",
|
|
130
|
+
["timing_event_patch"],
|
|
131
|
+
),
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
# Code that should pass static checks (no hacks)
|
|
135
|
+
STATIC_CLEAN_SAMPLES = {
|
|
136
|
+
"triton_kernel": (
|
|
137
|
+
"@triton.jit\ndef kernel(ptr, BLOCK: tl.constexpr):\n tl.store(ptr, tl.load(ptr))\n",
|
|
138
|
+
"triton",
|
|
139
|
+
),
|
|
140
|
+
}
|
|
@@ -636,6 +636,13 @@ wafer_core/tools/capture_tool/core.py,sha256=zQPilq5ZDJxBm0MFAzrl1-I2A2fyBGPRs8r
|
|
|
636
636
|
wafer_core/tools/capture_tool/dtypes.py,sha256=1Vm5obOCYc-Njuwkp7uqh_W4lqtYurT3b8lLnunc2Q8,3790
|
|
637
637
|
wafer_core/tools/capture_tool/executor.py,sha256=n1DVfbsP60yJAazx9C9Kwed9LB7AcKXJcoDnhno7ydU,1495
|
|
638
638
|
wafer_core/tools/capture_tool/metrics.py,sha256=BFZNmdE-kh3LneYdWXTNZmlLuo-DCrP5aEBHxEQYJDU,10890
|
|
639
|
+
wafer_core/tools/dispatch_baseline/__init__.py,sha256=RgWDH5rPYGDnC_MDosVAygsBj9SYLZFJQqF7QjNYwAw,1635
|
|
640
|
+
wafer_core/tools/dispatch_baseline/analyzer.py,sha256=Js2ctkd_3qTbV6u8bTUBfwrnof3X2WMD8F6K2qZQowE,5229
|
|
641
|
+
wafer_core/tools/dispatch_baseline/client.py,sha256=-kzRYGEFG0QnrHtgz5WATAgk1_RzQ2RGuUt7L1A6Mww,5611
|
|
642
|
+
wafer_core/tools/dispatch_baseline/codegen.py,sha256=jx4fXluBy8GenfASueYgfWQHaC2hkXE2Zh8IjQ3OROM,7332
|
|
643
|
+
wafer_core/tools/dispatch_baseline/dtypes.py,sha256=dE7UI93Y0zrSA7A5FhXS3Z6ryGFOoqCvuno3iQsppwI,7404
|
|
644
|
+
wafer_core/tools/dispatch_baseline/executor.py,sha256=dSdhmJbEsjD-Gl1zh6THqTlwqzEzZX15BdZVCEVZmJM,11929
|
|
645
|
+
wafer_core/tools/dispatch_baseline/roofline.py,sha256=L4gqdRt-9vKJVB7SjMp307rbHYG3lNwH22xAJdG_1ik,5237
|
|
639
646
|
wafer_core/tools/file_tools/__init__.py,sha256=2H7Rq5bijNQHGO4W6jjQAShkrcmdcHC0EQ8mBpgrApI,632
|
|
640
647
|
wafer_core/tools/file_tools/edit_tool.py,sha256=Efx83pM1Ljb07cJmAGVhPX4YiPJICK70sZM6uCjRWB0,4109
|
|
641
648
|
wafer_core/tools/file_tools/glob_tool.py,sha256=Av4LfC21fHXbnSsgh_9zDxlY9Qhb48aApaGos4j3B4g,3437
|
|
@@ -679,7 +686,7 @@ wafer_core/utils/remote_execution.py,sha256=z7nLiOgmDiM_VmElLnT2LF-aKNeeKFYjXigT
|
|
|
679
686
|
wafer_core/utils/submission_selection.py,sha256=LucdMTAbkqZA-GitSb3ZJ2pAeJ36wKqt5cTeS8xuAQ4,5655
|
|
680
687
|
wafer_core/utils/kernel_utils/__init__.py,sha256=NsfKpbfpIsfupWIpIjWLGCjGAVqaONiwiWil5zXbrRc,2015
|
|
681
688
|
wafer_core/utils/kernel_utils/backends.py,sha256=t3wY73Y-pVc_wALNu_bPsaFkqJ2dp2pf38KQ5ofP_go,1143
|
|
682
|
-
wafer_core/utils/kernel_utils/defense.py,sha256
|
|
689
|
+
wafer_core/utils/kernel_utils/defense.py,sha256=-AF8Bk5P6CluKiaGUn8ANkaiTCNSOlxV0T2Sa1VMuqE,48632
|
|
683
690
|
wafer_core/utils/kernel_utils/deployment.py,sha256=-tMb3qWmAoXHWCmmT7SQBH7KBKyyLP0e5Dk6lOrTPW8,55957
|
|
684
691
|
wafer_core/utils/kernel_utils/evaluate.py,sha256=1kxFNMl9VCXfKfk_BIiuA_zFfvDB1sl_feS2OEIJA1k,72346
|
|
685
692
|
wafer_core/utils/kernel_utils/gpu_validation.py,sha256=LRiDjW_xAK4fXf1Vw1aYHG54B1W0J6b5L0K6PXzM2tI,3759
|
|
@@ -687,6 +694,7 @@ wafer_core/utils/kernel_utils/reference_cache.py,sha256=4IQ2gND1StHULRO7geyAElES
|
|
|
687
694
|
wafer_core/utils/kernel_utils/results.py,sha256=QJGeah_41LSzxyYwGl9VxHPxTVAN2bLtk5bWdWLIpL4,6705
|
|
688
695
|
wafer_core/utils/kernel_utils/static_checker.py,sha256=XIQkzAOkGH5xtrOuZM4tNUqVJ0QRkYeJ7_8DosDOtkw,19886
|
|
689
696
|
wafer_core/utils/kernel_utils/task.py,sha256=XcmKxKUWh5It6nX3zGqj77tWgA32uPfQMqNOqyD5T48,2682
|
|
697
|
+
wafer_core/utils/kernel_utils/test_reward_hacks.py,sha256=Feo7_H2U4Uy3ZkcZDIDl9j0OAX3Z_wfQ8JQpjTiVg_4,3732
|
|
690
698
|
wafer_core/utils/kernel_utils/utils.py,sha256=uDZoJDxh07hJeLNlPdKN2vgB15pqIr1LbXf0YIBHU4E,43056
|
|
691
699
|
wafer_core/utils/kernel_utils/targets/__init__.py,sha256=4NwRLsuJ__S4xKAfda4Ag82C5MQ3Qio-4xA5S-mQGlU,2067
|
|
692
700
|
wafer_core/utils/kernel_utils/targets/config.py,sha256=DJPPyV7yGmyvS7cavdDENC5PQsia1dQeQYlWCTE7iUo,19975
|
|
@@ -697,6 +705,6 @@ wafer_core/utils/modal_execution/modal_app.py,sha256=VfS2cX8gHtnlPXemmMcEwDPeQdh
|
|
|
697
705
|
wafer_core/utils/modal_execution/modal_config.py,sha256=7cGX9TGqilQ3qxI3OFGXV5orjtyRU-PEDOJ4vP2oxno,4421
|
|
698
706
|
wafer_core/utils/modal_execution/modal_execution.py,sha256=gChjnV6jqA3A7IRP3DfvV5cSfm_MN0X4f7JZufXgdZE,24594
|
|
699
707
|
wafer_core/utils/modal_execution/test_modal.py,sha256=_jqou_hrLs1Daf1590Pnb0a_lXMMa2rczAPpW9HpoNQ,8153
|
|
700
|
-
wafer_core-0.1.
|
|
701
|
-
wafer_core-0.1.
|
|
702
|
-
wafer_core-0.1.
|
|
708
|
+
wafer_core-0.1.33.dist-info/METADATA,sha256=A7PNHHWQIZtMTwvuEodA9IqEjF2_yvn6rpyb1pqTJE4,1477
|
|
709
|
+
wafer_core-0.1.33.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
710
|
+
wafer_core-0.1.33.dist-info/RECORD,,
|
|
File without changes
|