smolbrain 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.
- smolbrain/__init__.py +400 -0
- smolbrain/_internal/__init__.py +1 -0
- smolbrain/_internal/aliasing.py +4 -0
- smolbrain/_internal/validation.py +3 -0
- smolbrain/_version.py +3 -0
- smolbrain/backend/__init__.py +98 -0
- smolbrain/backend/base.py +223 -0
- smolbrain/backend/kernels.c +111 -0
- smolbrain/backend/native.py +156 -0
- smolbrain/backend/numpy_backend.py +210 -0
- smolbrain/brain/__init__.py +51 -0
- smolbrain/brain/brain.py +972 -0
- smolbrain/brain/checking.py +484 -0
- smolbrain/brain/duel.py +192 -0
- smolbrain/brain/inputs.py +97 -0
- smolbrain/brain/keeping.py +680 -0
- smolbrain/brain/outputs.py +701 -0
- smolbrain/brain/resources.py +167 -0
- smolbrain/core/__init__.py +24 -0
- smolbrain/core/autograd.py +89 -0
- smolbrain/core/creation.py +74 -0
- smolbrain/core/dtypes.py +14 -0
- smolbrain/core/tensor.py +660 -0
- smolbrain/data/__init__.py +39 -0
- smolbrain/data/dataset.py +210 -0
- smolbrain/data/datasets/__init__.py +370 -0
- smolbrain/data/datasets/files/flowers.csv +151 -0
- smolbrain/data/datasets/registry.py +69 -0
- smolbrain/data/graph.py +170 -0
- smolbrain/data/images.py +255 -0
- smolbrain/data/loaders.py +275 -0
- smolbrain/data/peek.py +131 -0
- smolbrain/data/text.py +218 -0
- smolbrain/efficiency/__init__.py +23 -0
- smolbrain/efficiency/accumulation.py +1 -0
- smolbrain/efficiency/budget.py +143 -0
- smolbrain/efficiency/checkpointing.py +1 -0
- smolbrain/efficiency/distillation.py +1 -0
- smolbrain/efficiency/lazy.py +1 -0
- smolbrain/efficiency/precision.py +1 -0
- smolbrain/efficiency/pruning.py +1 -0
- smolbrain/efficiency/quantization.py +230 -0
- smolbrain/errors.py +152 -0
- smolbrain/fr/__init__.py +159 -0
- smolbrain/fr/datasets.py +25 -0
- smolbrain/fr/errors.py +141 -0
- smolbrain/fr/names.py +334 -0
- smolbrain/fr/translation.py +226 -0
- smolbrain/hardware/__init__.py +5 -0
- smolbrain/hardware/devices.py +1 -0
- smolbrain/hardware/system.py +138 -0
- smolbrain/layers/__init__.py +145 -0
- smolbrain/layers/activations.py +101 -0
- smolbrain/layers/attention.py +367 -0
- smolbrain/layers/augment.py +163 -0
- smolbrain/layers/base.py +253 -0
- smolbrain/layers/conv.py +172 -0
- smolbrain/layers/dense.py +116 -0
- smolbrain/layers/embedding.py +148 -0
- smolbrain/layers/fuzzy.py +84 -0
- smolbrain/layers/graph.py +102 -0
- smolbrain/layers/little_change.py +40 -0
- smolbrain/layers/normalization.py +79 -0
- smolbrain/layers/pooling.py +118 -0
- smolbrain/layers/recurrent.py +147 -0
- smolbrain/layers/scaling.py +80 -0
- smolbrain/layers/structural.py +121 -0
- smolbrain/layers/transformer.py +183 -0
- smolbrain/llm/__init__.py +25 -0
- smolbrain/llm/blabber.py +746 -0
- smolbrain/llm/chat.py +157 -0
- smolbrain/llm/generation.py +207 -0
- smolbrain/llm/lora.py +110 -0
- smolbrain/llm/summon.py +266 -0
- smolbrain/llm/tokenizer.py +348 -0
- smolbrain/observe/__init__.py +20 -0
- smolbrain/observe/explain.py +37 -0
- smolbrain/observe/metrics.py +174 -0
- smolbrain/observe/plots.py +100 -0
- smolbrain/observe/saliency.py +60 -0
- smolbrain/py.typed +0 -0
- smolbrain/storage/__init__.py +9 -0
- smolbrain/storage/export.py +1 -0
- smolbrain/storage/serialization.py +193 -0
- smolbrain/training/__init__.py +35 -0
- smolbrain/training/callbacks.py +40 -0
- smolbrain/training/history.py +46 -0
- smolbrain/training/losses.py +179 -0
- smolbrain/training/optimizers.py +206 -0
- smolbrain/training/schedulers.py +18 -0
- smolbrain/training/trainer.py +314 -0
- smolbrain-0.1.0.dist-info/METADATA +100 -0
- smolbrain-0.1.0.dist-info/RECORD +95 -0
- smolbrain-0.1.0.dist-info/WHEEL +4 -0
- smolbrain-0.1.0.dist-info/licenses/LICENSE +21 -0
smolbrain/__init__.py
ADDED
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
"""smolbrain: a tiny deep learning library that reads like a book.
|
|
2
|
+
|
|
3
|
+
>>> import smolbrain as sb
|
|
4
|
+
|
|
5
|
+
Everything a user should touch is re-exported here. Anything not listed in
|
|
6
|
+
``__all__`` is internal and may change without notice.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from smolbrain._version import __version__
|
|
10
|
+
from smolbrain.backend import engine, run_on, set_backend
|
|
11
|
+
from smolbrain.brain import (
|
|
12
|
+
GAN,
|
|
13
|
+
Answers,
|
|
14
|
+
Brain,
|
|
15
|
+
ForgerAndDetective,
|
|
16
|
+
Itself,
|
|
17
|
+
LessNoise,
|
|
18
|
+
LooksAt,
|
|
19
|
+
Model,
|
|
20
|
+
Weight,
|
|
21
|
+
a_number,
|
|
22
|
+
autoencoder,
|
|
23
|
+
columns,
|
|
24
|
+
diffusion,
|
|
25
|
+
forger_and_detective,
|
|
26
|
+
image,
|
|
27
|
+
itself,
|
|
28
|
+
keep_numbers,
|
|
29
|
+
less_noise,
|
|
30
|
+
next_word,
|
|
31
|
+
one_of,
|
|
32
|
+
open_brain,
|
|
33
|
+
open_numbers,
|
|
34
|
+
sentences,
|
|
35
|
+
some_of,
|
|
36
|
+
yes_or_no,
|
|
37
|
+
)
|
|
38
|
+
from smolbrain.core import (
|
|
39
|
+
Numbers,
|
|
40
|
+
no_blame,
|
|
41
|
+
no_grad,
|
|
42
|
+
numbers,
|
|
43
|
+
ones,
|
|
44
|
+
randn,
|
|
45
|
+
random,
|
|
46
|
+
same_luck,
|
|
47
|
+
seed,
|
|
48
|
+
stack,
|
|
49
|
+
tensor,
|
|
50
|
+
zeros,
|
|
51
|
+
)
|
|
52
|
+
from smolbrain.data import (
|
|
53
|
+
Dataset,
|
|
54
|
+
GraphData,
|
|
55
|
+
ImageFolder,
|
|
56
|
+
Text,
|
|
57
|
+
TextDataset,
|
|
58
|
+
TextVectorization,
|
|
59
|
+
datasets,
|
|
60
|
+
from_csv,
|
|
61
|
+
from_folder,
|
|
62
|
+
from_graph,
|
|
63
|
+
from_lists,
|
|
64
|
+
from_sentences,
|
|
65
|
+
from_text,
|
|
66
|
+
head,
|
|
67
|
+
peek,
|
|
68
|
+
read_csv,
|
|
69
|
+
save_images,
|
|
70
|
+
shuffle,
|
|
71
|
+
split,
|
|
72
|
+
to_folder,
|
|
73
|
+
)
|
|
74
|
+
from smolbrain.efficiency import Budget, Squeezed
|
|
75
|
+
from smolbrain.errors import (
|
|
76
|
+
BadDataError,
|
|
77
|
+
BadFileError,
|
|
78
|
+
GaveUpError,
|
|
79
|
+
MemorizedItWarning,
|
|
80
|
+
NoAnswerError,
|
|
81
|
+
NoDataError,
|
|
82
|
+
NoGpuError,
|
|
83
|
+
NotFoundError,
|
|
84
|
+
NothingToBlameError,
|
|
85
|
+
NotKeptWarning,
|
|
86
|
+
NotLearningWarning,
|
|
87
|
+
SmolbrainError,
|
|
88
|
+
SmolbrainWarning,
|
|
89
|
+
TooBigError,
|
|
90
|
+
TooMuchChoppingWarning,
|
|
91
|
+
TooSlowWarning,
|
|
92
|
+
UnfairDataWarning,
|
|
93
|
+
WrongJudgeError,
|
|
94
|
+
WrongShapeError,
|
|
95
|
+
WrongThingError,
|
|
96
|
+
)
|
|
97
|
+
from smolbrain.hardware import Machine, system_info, what_can_i_afford
|
|
98
|
+
from smolbrain.layers import (
|
|
99
|
+
GRU,
|
|
100
|
+
RNN,
|
|
101
|
+
AskTheNeighbors,
|
|
102
|
+
Conv2D,
|
|
103
|
+
Conv2d,
|
|
104
|
+
Dense,
|
|
105
|
+
Dropout,
|
|
106
|
+
Embedding,
|
|
107
|
+
Flatten,
|
|
108
|
+
ForgetOnPurpose,
|
|
109
|
+
FuzzyMiddle,
|
|
110
|
+
GCNConv,
|
|
111
|
+
GlobalAveragePooling1D,
|
|
112
|
+
GraphConv,
|
|
113
|
+
KeepItSteady,
|
|
114
|
+
Layer,
|
|
115
|
+
LayerNorm,
|
|
116
|
+
Linear,
|
|
117
|
+
MakeItFair,
|
|
118
|
+
Mash,
|
|
119
|
+
MaxPool2d,
|
|
120
|
+
MaxPooling2D,
|
|
121
|
+
MessWithIt,
|
|
122
|
+
MultiHeadAttention,
|
|
123
|
+
Neurons,
|
|
124
|
+
Normalization,
|
|
125
|
+
PatternFinder,
|
|
126
|
+
PayAttention,
|
|
127
|
+
PositionalEmbedding,
|
|
128
|
+
PositionTag,
|
|
129
|
+
RandomAugment,
|
|
130
|
+
RemembersOrder,
|
|
131
|
+
Reparameterize,
|
|
132
|
+
Residual,
|
|
133
|
+
RMSNorm,
|
|
134
|
+
Shortcut,
|
|
135
|
+
Shrink,
|
|
136
|
+
ThinkingBlock,
|
|
137
|
+
TransformerBlock,
|
|
138
|
+
Unshrink,
|
|
139
|
+
Upsample,
|
|
140
|
+
UpSampling2D,
|
|
141
|
+
WordToNumbers,
|
|
142
|
+
ask_the_neighbors,
|
|
143
|
+
augment,
|
|
144
|
+
flatten,
|
|
145
|
+
forget_on_purpose,
|
|
146
|
+
fuzzy_middle,
|
|
147
|
+
gelu,
|
|
148
|
+
ignore_negatives,
|
|
149
|
+
into_percentages,
|
|
150
|
+
keep_it_steady,
|
|
151
|
+
make_it_fair,
|
|
152
|
+
mash,
|
|
153
|
+
mess_with_it,
|
|
154
|
+
neurons,
|
|
155
|
+
pattern_finder,
|
|
156
|
+
pay_attention,
|
|
157
|
+
position_tag,
|
|
158
|
+
relu,
|
|
159
|
+
remembers_order,
|
|
160
|
+
shortcut,
|
|
161
|
+
shrink,
|
|
162
|
+
sigmoid,
|
|
163
|
+
silu,
|
|
164
|
+
smooth_ignore_negatives,
|
|
165
|
+
softmax,
|
|
166
|
+
squash01,
|
|
167
|
+
squash_both_ways,
|
|
168
|
+
swish,
|
|
169
|
+
tanh,
|
|
170
|
+
thinking_block,
|
|
171
|
+
unshrink,
|
|
172
|
+
word_to_numbers,
|
|
173
|
+
)
|
|
174
|
+
from smolbrain.llm import (
|
|
175
|
+
GPT,
|
|
176
|
+
Blabber,
|
|
177
|
+
Chopper,
|
|
178
|
+
Tokenizer,
|
|
179
|
+
from_pretrained,
|
|
180
|
+
make_chopper,
|
|
181
|
+
summon,
|
|
182
|
+
train_tokenizer,
|
|
183
|
+
)
|
|
184
|
+
from smolbrain.training import (
|
|
185
|
+
SGD,
|
|
186
|
+
Adam,
|
|
187
|
+
History,
|
|
188
|
+
Optimizer,
|
|
189
|
+
PlainSteps,
|
|
190
|
+
SmartSteps,
|
|
191
|
+
be_wrong_less,
|
|
192
|
+
binary_cross_entropy,
|
|
193
|
+
calm_down,
|
|
194
|
+
clip_gradients,
|
|
195
|
+
compute_loss,
|
|
196
|
+
cross_entropy,
|
|
197
|
+
how_wrong,
|
|
198
|
+
mse,
|
|
199
|
+
squared_error,
|
|
200
|
+
surprise,
|
|
201
|
+
train,
|
|
202
|
+
yes_no_surprise,
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
# The .smol files (chapter 7). ``open`` and ``load`` are left out of ``__all__``:
|
|
206
|
+
# ``from smolbrain import *`` must never hide Python's own ``open``.
|
|
207
|
+
open = open_brain
|
|
208
|
+
load = open_brain
|
|
209
|
+
save_weights = keep_numbers
|
|
210
|
+
load_weights = open_numbers
|
|
211
|
+
|
|
212
|
+
__all__ = [
|
|
213
|
+
"GAN",
|
|
214
|
+
"GPT",
|
|
215
|
+
"GRU",
|
|
216
|
+
"RNN",
|
|
217
|
+
"SGD",
|
|
218
|
+
"Adam",
|
|
219
|
+
"Answers",
|
|
220
|
+
"AskTheNeighbors",
|
|
221
|
+
"BadDataError",
|
|
222
|
+
"BadFileError",
|
|
223
|
+
"Blabber",
|
|
224
|
+
"Brain",
|
|
225
|
+
"Budget",
|
|
226
|
+
"Chopper",
|
|
227
|
+
"Conv2D",
|
|
228
|
+
"Conv2d",
|
|
229
|
+
"Dataset",
|
|
230
|
+
"Dense",
|
|
231
|
+
"Dropout",
|
|
232
|
+
"Embedding",
|
|
233
|
+
"Flatten",
|
|
234
|
+
"ForgerAndDetective",
|
|
235
|
+
"ForgetOnPurpose",
|
|
236
|
+
"FuzzyMiddle",
|
|
237
|
+
"GCNConv",
|
|
238
|
+
"GaveUpError",
|
|
239
|
+
"GlobalAveragePooling1D",
|
|
240
|
+
"GraphConv",
|
|
241
|
+
"GraphData",
|
|
242
|
+
"History",
|
|
243
|
+
"ImageFolder",
|
|
244
|
+
"Itself",
|
|
245
|
+
"KeepItSteady",
|
|
246
|
+
"Layer",
|
|
247
|
+
"LayerNorm",
|
|
248
|
+
"LessNoise",
|
|
249
|
+
"Linear",
|
|
250
|
+
"LooksAt",
|
|
251
|
+
"Machine",
|
|
252
|
+
"MakeItFair",
|
|
253
|
+
"Mash",
|
|
254
|
+
"MaxPool2d",
|
|
255
|
+
"MaxPooling2D",
|
|
256
|
+
"MemorizedItWarning",
|
|
257
|
+
"MessWithIt",
|
|
258
|
+
"Model",
|
|
259
|
+
"MultiHeadAttention",
|
|
260
|
+
"Neurons",
|
|
261
|
+
"NoAnswerError",
|
|
262
|
+
"NoDataError",
|
|
263
|
+
"NoGpuError",
|
|
264
|
+
"Normalization",
|
|
265
|
+
"NotFoundError",
|
|
266
|
+
"NotKeptWarning",
|
|
267
|
+
"NotLearningWarning",
|
|
268
|
+
"NothingToBlameError",
|
|
269
|
+
"Numbers",
|
|
270
|
+
"Optimizer",
|
|
271
|
+
"PatternFinder",
|
|
272
|
+
"PayAttention",
|
|
273
|
+
"PlainSteps",
|
|
274
|
+
"PositionTag",
|
|
275
|
+
"PositionalEmbedding",
|
|
276
|
+
"RMSNorm",
|
|
277
|
+
"RandomAugment",
|
|
278
|
+
"RemembersOrder",
|
|
279
|
+
"Reparameterize",
|
|
280
|
+
"Residual",
|
|
281
|
+
"Shortcut",
|
|
282
|
+
"Shrink",
|
|
283
|
+
"SmartSteps",
|
|
284
|
+
"SmolbrainError",
|
|
285
|
+
"SmolbrainWarning",
|
|
286
|
+
"Squeezed",
|
|
287
|
+
"Text",
|
|
288
|
+
"TextDataset",
|
|
289
|
+
"TextVectorization",
|
|
290
|
+
"ThinkingBlock",
|
|
291
|
+
"Tokenizer",
|
|
292
|
+
"TooBigError",
|
|
293
|
+
"TooMuchChoppingWarning",
|
|
294
|
+
"TooSlowWarning",
|
|
295
|
+
"TransformerBlock",
|
|
296
|
+
"UnfairDataWarning",
|
|
297
|
+
"Unshrink",
|
|
298
|
+
"UpSampling2D",
|
|
299
|
+
"Upsample",
|
|
300
|
+
"Weight",
|
|
301
|
+
"WordToNumbers",
|
|
302
|
+
"WrongJudgeError",
|
|
303
|
+
"WrongShapeError",
|
|
304
|
+
"WrongThingError",
|
|
305
|
+
"__version__",
|
|
306
|
+
"a_number",
|
|
307
|
+
"ask_the_neighbors",
|
|
308
|
+
"augment",
|
|
309
|
+
"autoencoder",
|
|
310
|
+
"be_wrong_less",
|
|
311
|
+
"binary_cross_entropy",
|
|
312
|
+
"calm_down",
|
|
313
|
+
"clip_gradients",
|
|
314
|
+
"columns",
|
|
315
|
+
"compute_loss",
|
|
316
|
+
"cross_entropy",
|
|
317
|
+
"datasets",
|
|
318
|
+
"diffusion",
|
|
319
|
+
"engine",
|
|
320
|
+
"flatten",
|
|
321
|
+
"forger_and_detective",
|
|
322
|
+
"forget_on_purpose",
|
|
323
|
+
"from_csv",
|
|
324
|
+
"from_folder",
|
|
325
|
+
"from_graph",
|
|
326
|
+
"from_lists",
|
|
327
|
+
"from_pretrained",
|
|
328
|
+
"from_sentences",
|
|
329
|
+
"from_text",
|
|
330
|
+
"fuzzy_middle",
|
|
331
|
+
"gelu",
|
|
332
|
+
"head",
|
|
333
|
+
"how_wrong",
|
|
334
|
+
"ignore_negatives",
|
|
335
|
+
"image",
|
|
336
|
+
"into_percentages",
|
|
337
|
+
"itself",
|
|
338
|
+
"keep_it_steady",
|
|
339
|
+
"keep_numbers",
|
|
340
|
+
"less_noise",
|
|
341
|
+
"load_weights",
|
|
342
|
+
"make_chopper",
|
|
343
|
+
"make_it_fair",
|
|
344
|
+
"mash",
|
|
345
|
+
"mess_with_it",
|
|
346
|
+
"mse",
|
|
347
|
+
"neurons",
|
|
348
|
+
"next_word",
|
|
349
|
+
"no_blame",
|
|
350
|
+
"no_grad",
|
|
351
|
+
"numbers",
|
|
352
|
+
"one_of",
|
|
353
|
+
"ones",
|
|
354
|
+
"open_numbers",
|
|
355
|
+
"pattern_finder",
|
|
356
|
+
"pay_attention",
|
|
357
|
+
"peek",
|
|
358
|
+
"position_tag",
|
|
359
|
+
"randn",
|
|
360
|
+
"random",
|
|
361
|
+
"read_csv",
|
|
362
|
+
"relu",
|
|
363
|
+
"remembers_order",
|
|
364
|
+
"run_on",
|
|
365
|
+
"same_luck",
|
|
366
|
+
"save_images",
|
|
367
|
+
"save_weights",
|
|
368
|
+
"seed",
|
|
369
|
+
"sentences",
|
|
370
|
+
"set_backend",
|
|
371
|
+
"shortcut",
|
|
372
|
+
"shrink",
|
|
373
|
+
"shuffle",
|
|
374
|
+
"sigmoid",
|
|
375
|
+
"silu",
|
|
376
|
+
"smooth_ignore_negatives",
|
|
377
|
+
"softmax",
|
|
378
|
+
"some_of",
|
|
379
|
+
"split",
|
|
380
|
+
"squared_error",
|
|
381
|
+
"squash01",
|
|
382
|
+
"squash_both_ways",
|
|
383
|
+
"stack",
|
|
384
|
+
"summon",
|
|
385
|
+
"surprise",
|
|
386
|
+
"swish",
|
|
387
|
+
"system_info",
|
|
388
|
+
"tanh",
|
|
389
|
+
"tensor",
|
|
390
|
+
"thinking_block",
|
|
391
|
+
"to_folder",
|
|
392
|
+
"train",
|
|
393
|
+
"train_tokenizer",
|
|
394
|
+
"unshrink",
|
|
395
|
+
"what_can_i_afford",
|
|
396
|
+
"word_to_numbers",
|
|
397
|
+
"yes_no_surprise",
|
|
398
|
+
"yes_or_no",
|
|
399
|
+
"zeros",
|
|
400
|
+
]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Private helpers. Nothing in here is part of the public API."""
|
smolbrain/_version.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"""Where the math actually happens.
|
|
2
|
+
|
|
3
|
+
The rest of smolbrain never calls NumPy directly: it goes through a backend, an
|
|
4
|
+
*engine*. There are two (chapter 14):
|
|
5
|
+
|
|
6
|
+
* ``numpy``: NumPy does everything. Always there.
|
|
7
|
+
* ``native``: NumPy, plus a few loops in C for squeezed tables, compiled once on
|
|
8
|
+
your machine. Needs a C compiler.
|
|
9
|
+
|
|
10
|
+
``run_on`` picks one. Nothing above this folder knows which one is running.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from smolbrain.backend.base import GROUP, Array, Backend, Shape
|
|
14
|
+
from smolbrain.backend.native import NativeBackend
|
|
15
|
+
from smolbrain.backend.numpy_backend import NumpyBackend
|
|
16
|
+
from smolbrain.errors import NoGpuError, NotFoundError, WrongThingError
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"GROUP",
|
|
20
|
+
"Array",
|
|
21
|
+
"Backend",
|
|
22
|
+
"NativeBackend",
|
|
23
|
+
"NumpyBackend",
|
|
24
|
+
"Shape",
|
|
25
|
+
"engine",
|
|
26
|
+
"get_backend",
|
|
27
|
+
"run_on",
|
|
28
|
+
"set_backend",
|
|
29
|
+
"use_engine",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
_current: Backend = NumpyBackend()
|
|
33
|
+
|
|
34
|
+
_ENGINES = ("auto", "native", "numpy")
|
|
35
|
+
_GRAPHICS = ("gpu", "cuda", "mps", "metal", "apple", "graphics card")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def get_backend() -> Backend:
|
|
39
|
+
"""The backend every new calculation uses."""
|
|
40
|
+
return _current
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def engine() -> str:
|
|
44
|
+
"""The name of the engine doing the math right now: ``"numpy"`` or ``"native"``."""
|
|
45
|
+
return _current.name
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def use_engine(backend: Backend) -> None:
|
|
49
|
+
"""Make ``backend`` do every new calculation. The dice (random numbers) carry over."""
|
|
50
|
+
global _current
|
|
51
|
+
dice = getattr(_current, "_rng", None)
|
|
52
|
+
if dice is not None and hasattr(backend, "_rng"):
|
|
53
|
+
backend._rng = dice
|
|
54
|
+
_current = backend
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def run_on(engine: str = "auto", *, quiet: bool = False) -> str:
|
|
58
|
+
"""Choose the engine that does the math, and say which one runs.
|
|
59
|
+
|
|
60
|
+
``"native"``: NumPy plus C loops for squeezed tables (compiled the first time).
|
|
61
|
+
``"numpy"``: NumPy only. ``"auto"``: native if this computer can, NumPy if not.
|
|
62
|
+
Returns the name of the engine now running.
|
|
63
|
+
"""
|
|
64
|
+
asked = engine.lower().strip() if isinstance(engine, str) else engine
|
|
65
|
+
if asked in _GRAPHICS:
|
|
66
|
+
raise NoGpuError(
|
|
67
|
+
"smolbrain doesn't use graphics cards: everything runs on the processor",
|
|
68
|
+
try_this='sb.run_on("native"), the fastest engine smolbrain has',
|
|
69
|
+
)
|
|
70
|
+
if asked in ("cpu", "processor"):
|
|
71
|
+
asked = "auto"
|
|
72
|
+
if asked not in _ENGINES:
|
|
73
|
+
raise WrongThingError(
|
|
74
|
+
f"there is no engine called {engine!r}",
|
|
75
|
+
try_this='sb.run_on("native"), sb.run_on("numpy") or sb.run_on("auto")',
|
|
76
|
+
)
|
|
77
|
+
why_not = ""
|
|
78
|
+
if asked == "numpy":
|
|
79
|
+
chosen: Backend = NumpyBackend()
|
|
80
|
+
else:
|
|
81
|
+
try:
|
|
82
|
+
chosen = NativeBackend() # compiles kernels.c the first time
|
|
83
|
+
except OSError as problem:
|
|
84
|
+
if asked == "native":
|
|
85
|
+
raise NotFoundError(
|
|
86
|
+
f"the native engine can't be built here: {problem}",
|
|
87
|
+
try_this="install a C compiler (macOS: xcode-select --install, "
|
|
88
|
+
'Linux: apt install gcc), or keep sb.run_on("numpy")',
|
|
89
|
+
) from None
|
|
90
|
+
chosen, why_not = NumpyBackend(), f" (no native engine: {problem})"
|
|
91
|
+
use_engine(chosen)
|
|
92
|
+
if not quiet:
|
|
93
|
+
print(f"smolbrain runs on: {chosen.name}{why_not}")
|
|
94
|
+
return chosen.name
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
#: Serious alias of :func:`run_on`.
|
|
98
|
+
set_backend = run_on
|