Repository navigation
Expand file tree
/
Copy path__init__.py
More file actions
205 lines (167 loc) · 5.1 KB
/
Copy path__init__.py
File metadata and controls
205 lines (167 loc) · 5.1 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
"""
larql — Python bindings for the LARQL knowledge graph engine and vindex.
Two interfaces:
- Direct vindex access for numpy arrays:
vindex = larql.load("gemma3-4b.vindex")
embed = vindex.embed("France")
edges = vindex.describe("France")
- LQL session for query language access:
session = larql.session("gemma3-4b.vindex")
session.query("DESCRIBE 'France'")
session.vindex.gate_vectors(layer=26)
- Typed VINDEX3 operations on a session, NumPy in and out:
s = larql.session("model.vindex3")
s.capture("logits", "The capital of France is").array
s.tensor("target.embedding@BF16::weight", rows=[0]).provenance
- The same operations from a restricted `larql serve-session` process, over its Unix socket (pure
Python; works without the native extension):
s = larql.connect("/path/to/larql.sock")
s.capture("logits", "The capital of France is").array
s.budget()
"""
from contextlib import contextmanager
from larql.session_client import RemoteSession, ServiceError, connect
# The native extension. Without it, `larql.connect` (pure Python) still works; every native name raises the
# import error on use.
try:
from larql._native import (
# Vindex types
Vindex,
FeatureMeta,
WalkHit,
DescribeEdge,
Relation,
Session,
Readout,
WalkModel,
# Graph types
Edge,
Node,
Graph,
# Vindex functions
load_vindex,
create_session,
# Graph functions
load as load_graph,
save as save_graph,
load_csv,
save_csv,
shortest_path,
merge_graphs,
merge_graphs_with_strategy,
diff,
pagerank,
bfs_traversal,
dfs_traversal,
weight_walk,
attention_walk,
)
except ImportError as _error:
_NATIVE_ERROR = _error
def __getattr__(name):
raise ImportError(
f"larql.{name} needs the native extension larql._native, which did not import: {_NATIVE_ERROR}"
)
else:
_NATIVE_ERROR = None
def _require_native(name):
if _NATIVE_ERROR is not None:
raise ImportError(
f"larql.{name} needs the native extension larql._native, which did not import: {_NATIVE_ERROR}"
)
def load(path: str, **kwargs) -> "Vindex":
"""Load a vindex from a local path.
Args:
path: Path to .vindex directory
Returns:
Vindex object with numpy array access, describe, walk, etc.
Example:
vindex = larql.load("gemma3-4b.vindex")
edges = vindex.describe("France")
embed = vindex.embed("France")
"""
_require_native("load")
return load_vindex(path)
def session(path: str) -> "Session":
"""Create an LQL session connected to a V2 or VINDEX3 artifact.
Provides LQL execution for both generations; .vindex arrays require V2:
session = larql.session("gemma3-4b.vindex")
session.query("DESCRIBE 'France'")
session.vindex.embed("France")
Args:
path: Path to .vindex directory
Returns:
Session with .query() and .vindex properties
"""
_require_native("session")
return create_session(path)
@contextmanager
def recording(session: "Session", path: str):
"""Record the edits staged inside the block into the patch file `path`.
Opens the recording with `session.begin_patch(path)` and, when the block
ends normally, writes it with `session.save_patch()`, whose result dict is
stored on the yielded object's `saved` key. If the block raises, nothing is
saved and the recording stays open, so the edits staged so far are kept.
Example:
with larql.recording(s, "edit.vlp") as rec:
s.add_low_rank(tensor, a, b, columns, numerics="f64-seq-rne1/v1")
rec["saved"]["path"]
"""
session.begin_patch(path)
rec = {"path": path, "saved": None}
yield rec
rec["saved"] = session.save_patch()
# MLX integration (lazy import — only loaded when used)
try:
from larql import mlx
except ImportError:
mlx = None
# Streaming: mmap'd weights, Metal pages from SSD on demand
try:
from larql import streaming
except ImportError:
streaming = None
# Walk FFN: MLX attention + Rust sparse FFN (editable knowledge layer)
try:
from larql import walk_ffn
except ImportError:
walk_ffn = None
# Residual stream trace is exposed via WalkModel.trace() (Rust implementation)
__version__ = "0.1.0"
__all__ = [
# Core functions
"load",
"session",
"load_vindex",
"create_session",
# Vindex types
"Vindex",
"FeatureMeta",
"WalkHit",
"DescribeEdge",
"Relation",
"Session",
"Readout",
"recording",
"connect",
"RemoteSession",
"ServiceError",
# Graph types
"Edge",
"Node",
"Graph",
# Graph functions
"load_graph",
"save_graph",
"load_csv",
"save_csv",
"shortest_path",
"merge_graphs",
"merge_graphs_with_strategy",
"diff",
"pagerank",
"bfs_traversal",
"dfs_traversal",
"weight_walk",
"attention_walk",
]