aikernel-governance 0.1.0__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.
@@ -0,0 +1,62 @@
1
+ """[EN]
2
+ Unified Python API for the AIKernel.Control governance contract surface.
3
+
4
+ [JA]
5
+ AIKernel.Control の governance 契約境界を統合して公開する Python API です。
6
+ """
7
+
8
+ from .governance import (
9
+ AllowAllControlPolicy,
10
+ Bonsai1BitCpuKernel,
11
+ BonsaiBuiltInProvider,
12
+ BonsaiModelConfig,
13
+ BonsaiModelState,
14
+ BonsaiProviderCapabilities,
15
+ BonsaiTokenizer,
16
+ ControlEmulatorEngine,
17
+ DeterministicNodeScheduler,
18
+ EmulatedExecutionGraph,
19
+ EmulatedExecutionNode,
20
+ ExecutionRequest,
21
+ ExecutionResult,
22
+ GovernanceClient,
23
+ ProviderContract,
24
+ ReplayApprovalRecord,
25
+ Snapshot,
26
+ SnapshotMetadata,
27
+ bonsai_gpu_execution_delegate_contract,
28
+ )
29
+ from .native import (
30
+ GovernanceAssemblySet,
31
+ governance_assemblies,
32
+ load_governance_runtime,
33
+ require_governance_assemblies,
34
+ )
35
+
36
+ __all__ = [
37
+ "AllowAllControlPolicy",
38
+ "Bonsai1BitCpuKernel",
39
+ "BonsaiBuiltInProvider",
40
+ "BonsaiModelConfig",
41
+ "BonsaiModelState",
42
+ "BonsaiProviderCapabilities",
43
+ "BonsaiTokenizer",
44
+ "ControlEmulatorEngine",
45
+ "DeterministicNodeScheduler",
46
+ "EmulatedExecutionGraph",
47
+ "EmulatedExecutionNode",
48
+ "ExecutionRequest",
49
+ "ExecutionResult",
50
+ "GovernanceAssemblySet",
51
+ "GovernanceClient",
52
+ "ProviderContract",
53
+ "ReplayApprovalRecord",
54
+ "Snapshot",
55
+ "SnapshotMetadata",
56
+ "bonsai_gpu_execution_delegate_contract",
57
+ "governance_assemblies",
58
+ "load_governance_runtime",
59
+ "require_governance_assemblies",
60
+ ]
61
+
62
+ __version__ = "0.1.0"
@@ -0,0 +1,46 @@
1
+ """Public governance contract wrappers."""
2
+
3
+ from .bonsai import (
4
+ BonsaiBuiltInProvider,
5
+ BonsaiModelConfig,
6
+ BonsaiModelState,
7
+ BonsaiProviderCapabilities,
8
+ BonsaiTokenizer,
9
+ )
10
+ from .client import GovernanceClient
11
+ from .cpu import Bonsai1BitCpuKernel
12
+ from .diagnostics import ReplayApprovalRecord
13
+ from .emulator import (
14
+ AllowAllControlPolicy,
15
+ ControlEmulatorEngine,
16
+ DeterministicNodeScheduler,
17
+ EmulatedExecutionGraph,
18
+ EmulatedExecutionNode,
19
+ )
20
+ from .gpu import bonsai_gpu_execution_delegate_contract
21
+ from .provider import ProviderContract
22
+ from .request import ExecutionRequest
23
+ from .result import ExecutionResult
24
+ from .snapshot import Snapshot, SnapshotMetadata
25
+
26
+ __all__ = [
27
+ "AllowAllControlPolicy",
28
+ "Bonsai1BitCpuKernel",
29
+ "BonsaiBuiltInProvider",
30
+ "BonsaiModelConfig",
31
+ "BonsaiModelState",
32
+ "BonsaiProviderCapabilities",
33
+ "BonsaiTokenizer",
34
+ "ControlEmulatorEngine",
35
+ "DeterministicNodeScheduler",
36
+ "EmulatedExecutionGraph",
37
+ "EmulatedExecutionNode",
38
+ "ExecutionRequest",
39
+ "ExecutionResult",
40
+ "GovernanceClient",
41
+ "ProviderContract",
42
+ "ReplayApprovalRecord",
43
+ "Snapshot",
44
+ "SnapshotMetadata",
45
+ "bonsai_gpu_execution_delegate_contract",
46
+ ]
@@ -0,0 +1,368 @@
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import dataclass, field
4
+ from typing import Mapping, Sequence
5
+
6
+ from aikernel_governance.native import load_governance_runtime
7
+
8
+ from .managed import ManagedObject, to_dictionary
9
+
10
+
11
+ @dataclass(frozen=True)
12
+ class BonsaiModelConfig:
13
+ """[EN]
14
+ Python wrapper for the public BonsaiModelConfig contract.
15
+
16
+ [JA]
17
+ 公開 BonsaiModelConfig 契約の Python wrapper です。
18
+ """
19
+ layer_count: int
20
+ hidden_size: int
21
+ head_count: int
22
+ vocabulary_size: int
23
+ context_length: int
24
+
25
+ @classmethod
26
+ def parse(cls, json: str) -> "BonsaiModelConfig":
27
+ """[EN]
28
+ Parse model configuration JSON through the C# contract.
29
+
30
+ [JA]
31
+ C# 契約を通じて model configuration JSON を解析します。
32
+ """
33
+ load_governance_runtime()
34
+ from AIKernel.Control.Core.Bonsai import BonsaiModelConfig as ManagedConfig # type: ignore[import-not-found]
35
+
36
+ return cls.from_managed(ManagedConfig.Parse(json))
37
+
38
+ @classmethod
39
+ def from_managed(cls, value) -> "BonsaiModelConfig":
40
+ """[EN]
41
+ Create a Python wrapper from a managed BonsaiModelConfig.
42
+
43
+ [JA]
44
+ managed BonsaiModelConfig から Python wrapper を生成します。
45
+ """
46
+ return cls(
47
+ layer_count=int(value.LayerCount),
48
+ hidden_size=int(value.HiddenSize),
49
+ head_count=int(value.HeadCount),
50
+ vocabulary_size=int(value.VocabularySize),
51
+ context_length=int(value.ContextLength),
52
+ )
53
+
54
+ def estimate_activation_float_count(self) -> int:
55
+ """[EN]
56
+ Return the C# estimate for activation buffer size.
57
+
58
+ [JA]
59
+ activation buffer size の C# 推定値を返します。
60
+ """
61
+ return int(self.to_managed().EstimateActivationFloatCount())
62
+
63
+ def to_managed(self):
64
+ """[EN]
65
+ Convert this wrapper to AIKernel.Control.Core.Bonsai.BonsaiModelConfig.
66
+
67
+ [JA]
68
+ この wrapper を AIKernel.Control.Core.Bonsai.BonsaiModelConfig に変換します。
69
+ """
70
+ load_governance_runtime()
71
+ from AIKernel.Control.Core.Bonsai import BonsaiModelConfig as ManagedConfig # type: ignore[import-not-found]
72
+
73
+ return ManagedConfig(
74
+ self.layer_count,
75
+ self.hidden_size,
76
+ self.head_count,
77
+ self.vocabulary_size,
78
+ self.context_length,
79
+ )
80
+
81
+
82
+ class BonsaiTokenizer(ManagedObject):
83
+ """[EN]
84
+ Python wrapper for the public BonsaiTokenizer contract.
85
+
86
+ [JA]
87
+ 公開 BonsaiTokenizer 契約の Python wrapper です。
88
+ """
89
+ @classmethod
90
+ def parse(cls, json: str) -> "BonsaiTokenizer":
91
+ """[EN]
92
+ Parse tokenizer JSON through the C# tokenizer.
93
+
94
+ [JA]
95
+ C# tokenizer を通じて tokenizer JSON を解析します。
96
+ """
97
+ load_governance_runtime()
98
+ from AIKernel.Control.Core.Bonsai import BonsaiTokenizer as ManagedTokenizer # type: ignore[import-not-found]
99
+
100
+ return cls(ManagedTokenizer.Parse(json))
101
+
102
+ @property
103
+ def vocabulary_size(self) -> int:
104
+ """[EN]
105
+ Return the vocabulary size exposed by the C# tokenizer.
106
+
107
+ [JA]
108
+ C# tokenizer が公開する vocabulary size を返します。
109
+ """
110
+ return int(self.managed.VocabularySize)
111
+
112
+ def tokenize_first(self, text: str) -> int:
113
+ """[EN]
114
+ Tokenize text by delegating to the C# tokenizer.
115
+
116
+ [JA]
117
+ C# tokenizer に委譲して text を token 化します。
118
+ """
119
+ return int(self.managed.TokenizeFirst(text))
120
+
121
+ def decode(self, token_id: int) -> str:
122
+ """[EN]
123
+ Decode a token id by delegating to the C# tokenizer.
124
+
125
+ [JA]
126
+ C# tokenizer に委譲して token id を decode します。
127
+ """
128
+ return str(self.managed.Decode(token_id))
129
+
130
+
131
+ class BonsaiModelState(ManagedObject):
132
+ """[EN]
133
+ Python wrapper for the public BonsaiModelState contract.
134
+
135
+ [JA]
136
+ 公開 BonsaiModelState 契約の Python wrapper です。
137
+ """
138
+ @classmethod
139
+ def create(
140
+ cls,
141
+ config: BonsaiModelConfig,
142
+ tokenizer: BonsaiTokenizer,
143
+ q1_weights: bytes,
144
+ activation_buffer: Sequence[float],
145
+ logits_buffer: Sequence[float],
146
+ ) -> "BonsaiModelState":
147
+ """[EN]
148
+ Create a managed BonsaiModelState from public contract inputs.
149
+
150
+ [JA]
151
+ 公開契約入力から managed BonsaiModelState を生成します。
152
+ """
153
+ load_governance_runtime()
154
+ from AIKernel.Control.Core.Bonsai import BonsaiModelState as ManagedState # type: ignore[import-not-found]
155
+ from System import Array, Byte, Single # type: ignore[import-not-found]
156
+
157
+ return cls(
158
+ ManagedState(
159
+ config.to_managed(),
160
+ tokenizer.to_managed(),
161
+ Array[Byte](q1_weights),
162
+ Array[Single]([float(value) for value in activation_buffer]),
163
+ Array[Single]([float(value) for value in logits_buffer]),
164
+ )
165
+ )
166
+
167
+ @property
168
+ def is_initialized(self) -> bool:
169
+ """[EN]
170
+ Return whether the C# state is initialized.
171
+
172
+ [JA]
173
+ C# state が initialized かどうかを返します。
174
+ """
175
+ return bool(self.managed.IsInitialized)
176
+
177
+ def mark_initialized(self) -> None:
178
+ """[EN]
179
+ Mark the managed state as initialized.
180
+
181
+ [JA]
182
+ managed state を initialized として記録します。
183
+ """
184
+ self.managed.MarkInitialized()
185
+
186
+
187
+ class BonsaiProviderCapabilities(ManagedObject):
188
+ """[EN]
189
+ Python wrapper for public Bonsai provider capabilities.
190
+
191
+ [JA]
192
+ 公開 Bonsai provider capabilities の Python wrapper です。
193
+ """
194
+ @classmethod
195
+ def create(cls) -> "BonsaiProviderCapabilities":
196
+ """[EN]
197
+ Create the managed BonsaiProviderCapabilities instance.
198
+
199
+ [JA]
200
+ managed BonsaiProviderCapabilities instance を生成します。
201
+ """
202
+ load_governance_runtime()
203
+ from AIKernel.Control.Core.Bonsai import BonsaiProviderCapabilities as ManagedCapabilities # type: ignore[import-not-found]
204
+
205
+ return cls(ManagedCapabilities())
206
+
207
+ @property
208
+ def supported_operations(self) -> tuple[str, ...]:
209
+ """[EN]
210
+ Return operations supported by the C# provider capabilities.
211
+
212
+ [JA]
213
+ C# provider capabilities が対応する operation を返します。
214
+ """
215
+ return tuple(str(value) for value in self.managed.SupportedOperations)
216
+
217
+ @property
218
+ def supported_data_types(self) -> tuple[str, ...]:
219
+ """[EN]
220
+ Return data types supported by the C# provider capabilities.
221
+
222
+ [JA]
223
+ C# provider capabilities が対応する data type を返します。
224
+ """
225
+ return tuple(str(value) for value in self.managed.SupportedDataTypes)
226
+
227
+ def supports_operation(self, operation: str) -> bool:
228
+ """[EN]
229
+ Check operation support through the C# capability object.
230
+
231
+ [JA]
232
+ C# capability object を通じて operation support を確認します。
233
+ """
234
+ return bool(self.managed.SupportsOperation(operation))
235
+
236
+ def supports_data_type(self, data_type: str) -> bool:
237
+ """[EN]
238
+ Check data-type support through the C# capability object.
239
+
240
+ [JA]
241
+ C# capability object を通じて data-type support を確認します。
242
+ """
243
+ return bool(self.managed.SupportsDataType(data_type))
244
+
245
+ def supports_quantization(self, quantization_level: str) -> bool:
246
+ """[EN]
247
+ Check quantization support through the C# capability object.
248
+
249
+ [JA]
250
+ C# capability object を通じて quantization support を確認します。
251
+ """
252
+ return bool(self.managed.SupportsQuantization(quantization_level))
253
+
254
+
255
+ class BonsaiBuiltInProvider(ManagedObject):
256
+ """[EN]
257
+ Python wrapper for the public BonsaiBuiltInProvider.
258
+
259
+ [JA]
260
+ 公開 BonsaiBuiltInProvider の Python wrapper です。
261
+ """
262
+ default_model_root = "/sys/roms/bonsai-1.7b"
263
+ operation_chat_local = "chat.local"
264
+ operation_tokenize = "text.tokenize"
265
+
266
+ @classmethod
267
+ def create(cls, kernel, observer=None, model_root: str = default_model_root) -> "BonsaiBuiltInProvider":
268
+ """[EN]
269
+ Create a managed BonsaiBuiltInProvider without reimplementing provider logic.
270
+
271
+ [JA]
272
+ provider logic を再実装せず managed BonsaiBuiltInProvider を生成します。
273
+ """
274
+ load_governance_runtime()
275
+ from AIKernel.Control.Core.Bonsai import BonsaiBuiltInProvider as ManagedProvider # type: ignore[import-not-found]
276
+
277
+ managed_kernel = kernel.to_managed() if hasattr(kernel, "to_managed") else kernel
278
+ managed_observer = observer.to_managed() if hasattr(observer, "to_managed") else observer
279
+ return cls(ManagedProvider(managed_kernel, managed_observer, model_root))
280
+
281
+ @property
282
+ def provider_id(self) -> str:
283
+ """[EN]
284
+ Return the provider identifier exposed by the C# provider.
285
+
286
+ [JA]
287
+ C# provider が公開する provider identifier を返します。
288
+ """
289
+ return str(self.managed.ProviderId)
290
+
291
+ @property
292
+ def name(self) -> str:
293
+ """[EN]
294
+ Return the provider display name.
295
+
296
+ [JA]
297
+ provider display name を返します。
298
+ """
299
+ return str(self.managed.Name)
300
+
301
+ @property
302
+ def version(self) -> str:
303
+ """[EN]
304
+ Return the provider version.
305
+
306
+ [JA]
307
+ provider version を返します。
308
+ """
309
+ return str(self.managed.Version)
310
+
311
+ @property
312
+ def model_root(self) -> str:
313
+ """[EN]
314
+ Return the configured model root.
315
+
316
+ [JA]
317
+ 設定された model root を返します。
318
+ """
319
+ return str(self.managed.ModelRoot)
320
+
321
+ @property
322
+ def is_initialized(self) -> bool:
323
+ """[EN]
324
+ Return whether the provider is initialized.
325
+
326
+ [JA]
327
+ provider が initialized かどうかを返します。
328
+ """
329
+ return bool(self.managed.IsInitialized)
330
+
331
+ def get_capabilities(self) -> BonsaiProviderCapabilities:
332
+ """[EN]
333
+ Return public provider capabilities from the C# provider.
334
+
335
+ [JA]
336
+ C# provider から公開 provider capabilities を返します。
337
+ """
338
+ return BonsaiProviderCapabilities(self.managed.GetCapabilities())
339
+
340
+
341
+ @dataclass(frozen=True)
342
+ class ProviderContractSurface:
343
+ """[EN]
344
+ Normalized provider contract metadata surface.
345
+
346
+ [JA]
347
+ 正規化された provider contract metadata surface です。
348
+ """
349
+ provider_id: str
350
+ name: str
351
+ version: str
352
+ metadata: Mapping[str, str] = field(default_factory=dict)
353
+
354
+ def to_provider_contract(self):
355
+ """[EN]
356
+ Convert this surface to the top-level ProviderContract wrapper.
357
+
358
+ [JA]
359
+ この surface を top-level ProviderContract wrapper に変換します。
360
+ """
361
+ from .provider import ProviderContract
362
+
363
+ return ProviderContract(
364
+ provider_id=self.provider_id,
365
+ name=self.name,
366
+ version=self.version,
367
+ metadata=self.metadata,
368
+ )
@@ -0,0 +1,71 @@
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import dataclass
4
+ from typing import Any
5
+
6
+ from .request import ExecutionRequest
7
+ from .result import ExecutionResult
8
+ from .snapshot import Snapshot
9
+
10
+
11
+ @dataclass(frozen=True)
12
+ class GovernanceClient:
13
+ """[EN]
14
+ Thin client facade over a public governance backend.
15
+
16
+ [JA]
17
+ 公開 governance backend に委譲する薄い client facade です。
18
+ """
19
+
20
+ backend: Any
21
+
22
+ def submit(self, request: ExecutionRequest) -> ExecutionResult:
23
+ """[EN]
24
+ Submit an execution request through the configured backend.
25
+
26
+ [JA]
27
+ 設定された backend 経由で execution request を送信します。
28
+ """
29
+ result = _call(self.backend, "submit", "Submit", request)
30
+ return _coerce_result(result)
31
+
32
+ def snapshot(self, id: str) -> Snapshot:
33
+ """[EN]
34
+ Retrieve a governance snapshot by execution identifier.
35
+
36
+ [JA]
37
+ execution identifier に基づいて governance snapshot を取得します。
38
+ """
39
+ result = _call(self.backend, "snapshot", "Snapshot", id)
40
+ return _coerce_snapshot(result)
41
+
42
+ def result(self, id: str) -> ExecutionResult:
43
+ """[EN]
44
+ Retrieve an execution result by execution identifier.
45
+
46
+ [JA]
47
+ execution identifier に基づいて execution result を取得します。
48
+ """
49
+ result = _call(self.backend, "result", "Result", id)
50
+ return _coerce_result(result)
51
+
52
+
53
+ def _call(target: Any, python_name: str, managed_name: str, argument: Any) -> Any:
54
+ method = getattr(target, python_name, None) or getattr(target, managed_name, None)
55
+ if method is None:
56
+ raise AttributeError(
57
+ f"governance backend must expose {python_name}({argument.__class__.__name__})"
58
+ )
59
+ return method(argument)
60
+
61
+
62
+ def _coerce_result(value: Any) -> ExecutionResult:
63
+ if isinstance(value, ExecutionResult):
64
+ return value
65
+ return ExecutionResult.from_managed(value)
66
+
67
+
68
+ def _coerce_snapshot(value: Any) -> Snapshot:
69
+ if isinstance(value, Snapshot):
70
+ return value
71
+ return Snapshot.from_managed(value)
@@ -0,0 +1,57 @@
1
+ from __future__ import annotations
2
+
3
+ from aikernel_governance.native import load_governance_runtime
4
+
5
+ from .managed import ManagedObject
6
+
7
+
8
+ class Bonsai1BitCpuKernel(ManagedObject):
9
+ """[EN]
10
+ Wrapper for the public Bonsai1BitCpuKernel.
11
+
12
+ [JA]
13
+ 公開 Bonsai1BitCpuKernel の wrapper です。
14
+ """
15
+
16
+ @classmethod
17
+ def create(cls) -> "Bonsai1BitCpuKernel":
18
+ """[EN]
19
+ Create the managed CPU kernel.
20
+
21
+ [JA]
22
+ managed CPU kernel を生成します。
23
+ """
24
+ load_governance_runtime()
25
+ from AIKernel.Control.CPU import Bonsai1BitCpuKernel as ManagedKernel # type: ignore[import-not-found]
26
+
27
+ return cls(ManagedKernel())
28
+
29
+ @property
30
+ def kernel_id(self) -> str:
31
+ """[EN]
32
+ Return the C# kernel identifier.
33
+
34
+ [JA]
35
+ C# kernel identifier を返します。
36
+ """
37
+ return str(self.managed.KernelId)
38
+
39
+ @property
40
+ def q1_block_element_count(self) -> int:
41
+ """[EN]
42
+ Return the Q1 block element count from the C# kernel.
43
+
44
+ [JA]
45
+ C# kernel の Q1 block element count を返します。
46
+ """
47
+ return int(self.managed.Q1BlockElementCount)
48
+
49
+ @property
50
+ def q1_block_byte_count(self) -> int:
51
+ """[EN]
52
+ Return the Q1 block byte count from the C# kernel.
53
+
54
+ [JA]
55
+ C# kernel の Q1 block byte count を返します。
56
+ """
57
+ return int(self.managed.Q1BlockByteCount)
@@ -0,0 +1,44 @@
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import dataclass
4
+
5
+ from aikernel_governance.native import load_governance_runtime
6
+
7
+
8
+ @dataclass(frozen=True)
9
+ class ReplayApprovalRecord:
10
+ """[EN]
11
+ Python wrapper for the public ReplayApprovalRecord diagnostics contract.
12
+
13
+ [JA]
14
+ 公開 ReplayApprovalRecord diagnostics 契約の Python wrapper です。
15
+ """
16
+ replay_log_hash: str
17
+ approved_by: str
18
+ decision: str
19
+
20
+ @classmethod
21
+ def from_managed(cls, value) -> "ReplayApprovalRecord":
22
+ """[EN]
23
+ Create a Python record from the managed diagnostics contract.
24
+
25
+ [JA]
26
+ managed diagnostics contract から Python record を生成します。
27
+ """
28
+ return cls(
29
+ replay_log_hash=str(value.ReplayLogHash),
30
+ approved_by=str(value.ApprovedBy),
31
+ decision=str(value.Decision),
32
+ )
33
+
34
+ def to_managed(self):
35
+ """[EN]
36
+ Convert this wrapper to AIKernel.Control.Diagnostics.ReplayApprovalRecord.
37
+
38
+ [JA]
39
+ この wrapper を AIKernel.Control.Diagnostics.ReplayApprovalRecord に変換します。
40
+ """
41
+ load_governance_runtime()
42
+ from AIKernel.Control.Diagnostics import ReplayApprovalRecord as ManagedRecord # type: ignore[import-not-found]
43
+
44
+ return ManagedRecord(self.replay_log_hash, self.approved_by, self.decision)