mapFolding 0.12.0__py3-none-any.whl → 0.12.2__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.
- mapFolding/__init__.py +42 -18
- mapFolding/_theSSOT.py +137 -0
- mapFolding/basecamp.py +28 -18
- mapFolding/beDRY.py +21 -19
- mapFolding/dataBaskets.py +170 -18
- mapFolding/datatypes.py +109 -1
- mapFolding/filesystemToolkit.py +38 -33
- mapFolding/oeis.py +209 -93
- mapFolding/someAssemblyRequired/RecipeJob.py +120 -9
- mapFolding/someAssemblyRequired/__init__.py +35 -38
- mapFolding/someAssemblyRequired/_toolIfThis.py +80 -18
- mapFolding/someAssemblyRequired/_toolkitContainers.py +123 -45
- mapFolding/someAssemblyRequired/infoBooth.py +37 -2
- mapFolding/someAssemblyRequired/makeAllModules.py +712 -0
- mapFolding/someAssemblyRequired/makeJobTheorem2Numba.py +111 -48
- mapFolding/someAssemblyRequired/toolkitNumba.py +171 -19
- mapFolding/someAssemblyRequired/transformationTools.py +93 -49
- mapfolding-0.12.2.dist-info/METADATA +167 -0
- mapfolding-0.12.2.dist-info/RECORD +53 -0
- {mapfolding-0.12.0.dist-info → mapfolding-0.12.2.dist-info}/WHEEL +1 -1
- tests/__init__.py +28 -44
- tests/conftest.py +66 -61
- tests/test_computations.py +39 -82
- tests/test_filesystem.py +25 -1
- tests/test_oeis.py +30 -1
- tests/test_other.py +27 -0
- tests/test_tasks.py +31 -1
- mapFolding/someAssemblyRequired/Z0Z_makeAllModules.py +0 -433
- mapFolding/theSSOT.py +0 -34
- mapfolding-0.12.0.dist-info/METADATA +0 -184
- mapfolding-0.12.0.dist-info/RECORD +0 -53
- {mapfolding-0.12.0.dist-info → mapfolding-0.12.2.dist-info}/entry_points.txt +0 -0
- {mapfolding-0.12.0.dist-info → mapfolding-0.12.2.dist-info}/licenses/LICENSE +0 -0
- {mapfolding-0.12.0.dist-info → mapfolding-0.12.2.dist-info}/top_level.txt +0 -0
|
@@ -1,41 +1,71 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"""
|
|
2
|
+
Map folding AST transformation system: Specialized job generation and optimization implementation.
|
|
3
|
+
|
|
4
|
+
This module implements the specialized job generation layer of the map folding AST transformation
|
|
5
|
+
system, executing the complete transformation process to convert generic map folding algorithms
|
|
6
|
+
into highly optimized, standalone computation modules. Building upon the configuration orchestration
|
|
7
|
+
established in the recipe system, this module applies the full sequence of transformations from
|
|
8
|
+
pattern recognition through Numba compilation to produce self-contained computational solutions
|
|
9
|
+
optimized for specific map dimensions and calculation contexts.
|
|
10
|
+
|
|
11
|
+
The transformation implementation addresses the computational demands of map folding research where
|
|
12
|
+
calculations can require hours or days to complete. The specialization process converts abstract
|
|
13
|
+
algorithms with flexible parameters into concrete, statically-optimized code that leverages
|
|
14
|
+
just-in-time compilation for maximum performance. Each generated module targets specific map
|
|
15
|
+
shapes and calculation modes, enabling aggressive compiler optimizations based on known constraints
|
|
16
|
+
and embedded constants.
|
|
17
|
+
|
|
18
|
+
The optimization process executes systematic transformations including static value embedding to
|
|
19
|
+
replace parameterized values with compile-time constants, dead code elimination to remove unused
|
|
20
|
+
variables and code paths, parameter internalization to convert function parameters into embedded
|
|
21
|
+
variables, import optimization to replace generic imports with specific implementations, Numba
|
|
22
|
+
decoration with appropriate compilation directives, progress integration for long-running calculations,
|
|
23
|
+
and launcher generation for standalone execution entry points.
|
|
24
|
+
|
|
25
|
+
The resulting modules represent the culmination of the entire AST transformation system, producing
|
|
26
|
+
self-contained Python scripts that execute independently with dramatically improved performance
|
|
27
|
+
compared to original generic algorithms while maintaining mathematical correctness and providing
|
|
28
|
+
essential progress feedback capabilities for large-scale computational research.
|
|
29
|
+
"""
|
|
30
|
+
|
|
3
31
|
from astToolkit import (
|
|
4
|
-
Be,
|
|
5
|
-
|
|
6
|
-
extractFunctionDef,
|
|
7
|
-
IngredientsFunction,
|
|
8
|
-
IngredientsModule,
|
|
9
|
-
LedgerOfImports,
|
|
10
|
-
Make,
|
|
11
|
-
NodeChanger,
|
|
12
|
-
NodeTourist,
|
|
13
|
-
str_nameDOTname,
|
|
14
|
-
Then,
|
|
32
|
+
Be, ClassIsAndAttribute, extractFunctionDef, identifierDotAttribute, IngredientsFunction,
|
|
33
|
+
IngredientsModule, LedgerOfImports, Make, NodeChanger, NodeTourist, Then,
|
|
15
34
|
)
|
|
16
35
|
from astToolkit.transformationTools import write_astModule
|
|
36
|
+
from mapFolding import getPathFilenameFoldsTotal, MapFoldingState, packageSettings
|
|
37
|
+
from mapFolding.someAssemblyRequired import IfThis
|
|
17
38
|
from mapFolding.someAssemblyRequired.RecipeJob import RecipeJobTheorem2Numba
|
|
18
|
-
from mapFolding.someAssemblyRequired.toolkitNumba import
|
|
39
|
+
from mapFolding.someAssemblyRequired.toolkitNumba import (
|
|
40
|
+
decorateCallableWithNumba, parametersNumbaLight, SpicesJobNumba,
|
|
41
|
+
)
|
|
19
42
|
from mapFolding.syntheticModules.initializeCount import initializeGroupsOfFolds
|
|
20
43
|
from pathlib import PurePosixPath
|
|
21
44
|
from typing import cast, NamedTuple
|
|
22
|
-
from Z0Z_tools import autoDecodingRLE
|
|
45
|
+
from Z0Z_tools import autoDecodingRLE, raiseIfNone
|
|
23
46
|
import ast
|
|
24
|
-
"""Synthesize one file to compute `foldsTotal` of `mapShape`."""
|
|
25
47
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
48
|
+
# Configuration lists for code optimization and dead code elimination
|
|
49
|
+
listIdentifiersNotUsedAllHARDCODED: list[str] = ['concurrencyLimit', 'foldsTotal', 'mapShape',]
|
|
50
|
+
"""Identifiers that are universally unused across all optimization contexts."""
|
|
51
|
+
|
|
52
|
+
listIdentifiersNotUsedParallelSequentialHARDCODED: list[str] = ['indexLeaf']
|
|
53
|
+
"""Identifiers unused in both parallel and sequential execution modes."""
|
|
54
|
+
|
|
55
|
+
listIdentifiersNotUsedSequentialHARDCODED: list[str] = ['foldGroups', 'taskDivisions', 'taskIndex',]
|
|
56
|
+
"""Identifiers unused specifically in sequential execution mode."""
|
|
29
57
|
|
|
30
|
-
|
|
58
|
+
listIdentifiersReplacedHARDCODED: list[str] = ['groupsOfFolds',]
|
|
59
|
+
"""Identifiers that get replaced with optimized equivalents during transformation."""
|
|
31
60
|
|
|
32
|
-
|
|
61
|
+
listIdentifiersStaticValuesHARDCODED: list[str] = ['dimensionsTotal', 'leavesTotal',]
|
|
62
|
+
"""Identifiers with compile-time constant values that can be embedded directly."""
|
|
33
63
|
|
|
34
|
-
|
|
64
|
+
listIdentifiersNotUsedHARDCODED: list[str] = listIdentifiersStaticValuesHARDCODED + listIdentifiersReplacedHARDCODED + listIdentifiersNotUsedAllHARDCODED + listIdentifiersNotUsedParallelSequentialHARDCODED + listIdentifiersNotUsedSequentialHARDCODED
|
|
65
|
+
"""Complete list of all identifiers that can be eliminated during optimization."""
|
|
35
66
|
|
|
36
67
|
def addLauncherNumbaProgress(ingredientsModule: IngredientsModule, ingredientsFunction: IngredientsFunction, job: RecipeJobTheorem2Numba, spices: SpicesJobNumba) -> tuple[IngredientsModule, IngredientsFunction]:
|
|
37
|
-
"""
|
|
38
|
-
Add progress tracking capabilities to a Numba-optimized function.
|
|
68
|
+
"""Add progress tracking capabilities to a Numba-optimized function.
|
|
39
69
|
|
|
40
70
|
This function modifies both the module and the function to integrate Numba-compatible
|
|
41
71
|
progress tracking for long-running calculations. It performs several key transformations:
|
|
@@ -56,8 +86,9 @@ def addLauncherNumbaProgress(ingredientsModule: IngredientsModule, ingredientsFu
|
|
|
56
86
|
spices: Configuration specifying progress bar details.
|
|
57
87
|
|
|
58
88
|
Returns:
|
|
59
|
-
|
|
89
|
+
Modified module and function with integrated progress tracking capabilities.
|
|
60
90
|
"""
|
|
91
|
+
|
|
61
92
|
linesLaunch: str = f"""
|
|
62
93
|
if __name__ == '__main__':
|
|
63
94
|
with ProgressBar(total={job.foldsTotalEstimated}, update_interval=2) as statusUpdate:
|
|
@@ -76,7 +107,7 @@ if __name__ == '__main__':
|
|
|
76
107
|
ast_argNumbaProgress = ast.arg(arg=spices.numbaProgressBarIdentifier, annotation=ast.Name(id=numba_progressPythonClass, ctx=ast.Load()))
|
|
77
108
|
ingredientsFunction.astFunctionDef.args.args.append(ast_argNumbaProgress)
|
|
78
109
|
|
|
79
|
-
findThis = ClassIsAndAttribute.targetIs(ast.AugAssign, IfThis.
|
|
110
|
+
findThis = ClassIsAndAttribute.targetIs(ast.AugAssign, IfThis.isNameIdentifier(job.shatteredDataclass.countingVariableName.id))
|
|
80
111
|
doThat = Then.replaceWith(Make.Expr(Make.Call(Make.Attribute(Make.Name(spices.numbaProgressBarIdentifier),'update'),[Make.Constant(1)])))
|
|
81
112
|
countWithProgressBar = NodeChanger(findThis, doThat)
|
|
82
113
|
countWithProgressBar.visit(ingredientsFunction.astFunctionDef)
|
|
@@ -90,17 +121,16 @@ if __name__ == '__main__':
|
|
|
90
121
|
return ingredientsModule, ingredientsFunction
|
|
91
122
|
|
|
92
123
|
def move_arg2FunctionDefDOTbodyAndAssignInitialValues(ingredientsFunction: IngredientsFunction, job: RecipeJobTheorem2Numba) -> IngredientsFunction:
|
|
93
|
-
"""
|
|
94
|
-
Convert function parameters into initialized variables with concrete values.
|
|
124
|
+
"""Convert function parameters into initialized variables with concrete values.
|
|
95
125
|
|
|
96
126
|
This function implements a critical transformation that converts function parameters
|
|
97
127
|
into statically initialized variables in the function body. This enables several
|
|
98
128
|
optimizations:
|
|
99
129
|
|
|
100
|
-
1. Eliminating parameter passing overhead
|
|
101
|
-
2. Embedding concrete values directly in the code
|
|
102
|
-
3. Allowing Numba to optimize based on known value characteristics
|
|
103
|
-
4. Simplifying function signatures for specialized use cases
|
|
130
|
+
1. Eliminating parameter passing overhead
|
|
131
|
+
2. Embedding concrete values directly in the code
|
|
132
|
+
3. Allowing Numba to optimize based on known value characteristics
|
|
133
|
+
4. Simplifying function signatures for specialized use cases
|
|
104
134
|
|
|
105
135
|
The function handles different data types (scalars, arrays, custom types) appropriately,
|
|
106
136
|
replacing abstract parameter references with concrete values from the computation state.
|
|
@@ -115,16 +145,16 @@ def move_arg2FunctionDefDOTbodyAndAssignInitialValues(ingredientsFunction: Ingre
|
|
|
115
145
|
"""
|
|
116
146
|
ingredientsFunction.imports.update(job.shatteredDataclass.imports)
|
|
117
147
|
|
|
118
|
-
list_argCuzMyBrainRefusesToThink = ingredientsFunction.astFunctionDef.args.args + ingredientsFunction.astFunctionDef.args.posonlyargs + ingredientsFunction.astFunctionDef.args.kwonlyargs
|
|
148
|
+
list_argCuzMyBrainRefusesToThink: list[ast.arg] = ingredientsFunction.astFunctionDef.args.args + ingredientsFunction.astFunctionDef.args.posonlyargs + ingredientsFunction.astFunctionDef.args.kwonlyargs
|
|
119
149
|
list_arg_arg: list[str] = [ast_arg.arg for ast_arg in list_argCuzMyBrainRefusesToThink]
|
|
120
150
|
listName: list[ast.Name] = []
|
|
121
151
|
NodeTourist(Be.Name, Then.appendTo(listName)).visit(ingredientsFunction.astFunctionDef)
|
|
122
|
-
|
|
123
|
-
|
|
152
|
+
listIdentifiers: list[str] = [astName.id for astName in listName]
|
|
153
|
+
listIdentifiersNotUsed: list[str] = list(set(list_arg_arg) - set(listIdentifiers))
|
|
124
154
|
|
|
125
155
|
for ast_arg in list_argCuzMyBrainRefusesToThink:
|
|
126
156
|
if ast_arg.arg in job.shatteredDataclass.field2AnnAssign:
|
|
127
|
-
if ast_arg.arg in
|
|
157
|
+
if ast_arg.arg in listIdentifiersNotUsed:
|
|
128
158
|
pass
|
|
129
159
|
else:
|
|
130
160
|
ImaAnnAssign, elementConstructor = job.shatteredDataclass.Z0Z_field2AnnAssign[ast_arg.arg]
|
|
@@ -146,7 +176,7 @@ def move_arg2FunctionDefDOTbodyAndAssignInitialValues(ingredientsFunction: Ingre
|
|
|
146
176
|
|
|
147
177
|
ingredientsFunction.astFunctionDef.body.insert(0, ImaAnnAssign)
|
|
148
178
|
|
|
149
|
-
findThis = IfThis.
|
|
179
|
+
findThis = IfThis.is_argIdentifier(ast_arg.arg)
|
|
150
180
|
remove_arg = NodeChanger(findThis, Then.removeIt)
|
|
151
181
|
remove_arg.visit(ingredientsFunction.astFunctionDef)
|
|
152
182
|
|
|
@@ -154,22 +184,44 @@ def move_arg2FunctionDefDOTbodyAndAssignInitialValues(ingredientsFunction: Ingre
|
|
|
154
184
|
return ingredientsFunction
|
|
155
185
|
|
|
156
186
|
def makeJobNumba(job: RecipeJobTheorem2Numba, spices: SpicesJobNumba) -> None:
|
|
187
|
+
"""Generate an optimized Numba-compiled computation module for map folding calculations.
|
|
188
|
+
|
|
189
|
+
This function orchestrates the complete code transformation pipeline to convert
|
|
190
|
+
a generic map folding algorithm into a highly optimized, specialized computation
|
|
191
|
+
module. The transformation process includes:
|
|
192
|
+
|
|
193
|
+
1. Extract and modify the source function from the generic algorithm
|
|
194
|
+
2. Replace static-valued identifiers with their concrete values
|
|
195
|
+
3. Convert function parameters to embedded initialized variables
|
|
196
|
+
4. Remove unused code paths and variables for optimization
|
|
197
|
+
5. Configure appropriate Numba decorators for JIT compilation
|
|
198
|
+
6. Add progress tracking capabilities for long-running computations
|
|
199
|
+
7. Generate standalone launcher code for direct execution
|
|
200
|
+
8. Write the complete optimized module to the filesystem
|
|
157
201
|
|
|
158
|
-
|
|
159
|
-
|
|
202
|
+
The resulting module is a self-contained Python script that can execute
|
|
203
|
+
map folding calculations for the specific map dimensions with maximum
|
|
204
|
+
performance through just-in-time compilation.
|
|
205
|
+
|
|
206
|
+
Parameters:
|
|
207
|
+
job: Configuration recipe containing source locations, target paths, and state.
|
|
208
|
+
spices: Optimization settings including Numba parameters and progress options.
|
|
209
|
+
"""
|
|
210
|
+
|
|
211
|
+
astFunctionDef: ast.FunctionDef = raiseIfNone(extractFunctionDef(job.source_astModule, job.countCallable))
|
|
160
212
|
ingredientsCount: IngredientsFunction = IngredientsFunction(astFunctionDef, LedgerOfImports())
|
|
161
213
|
|
|
162
214
|
# Remove `foldGroups` and any other unused statements, so you can dynamically determine which variables are not used
|
|
163
|
-
findThis = ClassIsAndAttribute.targetsIs(ast.Assign, lambda list_expr: any([IfThis.
|
|
164
|
-
# findThis = IfThis.isAssignAndTargets0Is(IfThis.
|
|
215
|
+
findThis = ClassIsAndAttribute.targetsIs(ast.Assign, lambda list_expr: any([IfThis.isSubscriptIdentifier('foldGroups')(node) for node in list_expr ]))
|
|
216
|
+
# findThis = IfThis.isAssignAndTargets0Is(IfThis.isSubscriptIdentifier('foldGroups'))
|
|
165
217
|
doThat = Then.removeIt
|
|
166
218
|
remove_foldGroups = NodeChanger(findThis, doThat)
|
|
167
219
|
# remove_foldGroups.visit(ingredientsCount.astFunctionDef)
|
|
168
220
|
|
|
169
221
|
# replace identifiers with static values with their values, so you can dynamically determine which variables are not used
|
|
170
|
-
|
|
171
|
-
for identifier in
|
|
172
|
-
findThis = IfThis.
|
|
222
|
+
listIdentifiersStaticValues: list[str] = listIdentifiersStaticValuesHARDCODED
|
|
223
|
+
for identifier in listIdentifiersStaticValues:
|
|
224
|
+
findThis = IfThis.isNameIdentifier(identifier)
|
|
173
225
|
doThat = Then.replaceWith(Make.Constant(int(job.state.__dict__[identifier])))
|
|
174
226
|
NodeChanger(findThis, doThat).visit(ingredientsCount.astFunctionDef)
|
|
175
227
|
|
|
@@ -198,14 +250,26 @@ if __name__ == '__main__':
|
|
|
198
250
|
ingredientsCount.astFunctionDef.returns = job.shatteredDataclass.countingVariableAnnotation
|
|
199
251
|
|
|
200
252
|
ingredientsCount = move_arg2FunctionDefDOTbodyAndAssignInitialValues(ingredientsCount, job)
|
|
201
|
-
|
|
202
253
|
class DatatypeConfig(NamedTuple):
|
|
203
|
-
|
|
254
|
+
"""Configuration for mapping framework datatypes to Numba-compatible types.
|
|
255
|
+
|
|
256
|
+
This configuration class defines how abstract datatypes used in the map folding
|
|
257
|
+
framework should be replaced with concrete Numba-compatible types during code
|
|
258
|
+
generation. Each configuration specifies the source module, target type name,
|
|
259
|
+
and optional import alias for the transformation.
|
|
260
|
+
|
|
261
|
+
Attributes:
|
|
262
|
+
fml: Framework datatype identifier to be replaced.
|
|
263
|
+
Z0Z_module: Module containing the target datatype (e.g., 'numba', 'numpy').
|
|
264
|
+
Z0Z_type_name: Concrete type name in the target module.
|
|
265
|
+
Z0Z_asname: Optional import alias for the type.
|
|
266
|
+
"""
|
|
204
267
|
fml: str
|
|
268
|
+
Z0Z_module: identifierDotAttribute
|
|
205
269
|
Z0Z_type_name: str
|
|
206
270
|
Z0Z_asname: str | None = None
|
|
207
271
|
|
|
208
|
-
listDatatypeConfigs = [
|
|
272
|
+
listDatatypeConfigs: list[DatatypeConfig] = [
|
|
209
273
|
DatatypeConfig(fml='DatatypeLeavesTotal', Z0Z_module='numba', Z0Z_type_name='uint8'),
|
|
210
274
|
DatatypeConfig(fml='DatatypeElephino', Z0Z_module='numba', Z0Z_type_name='uint16'),
|
|
211
275
|
DatatypeConfig(fml='DatatypeFoldsTotal', Z0Z_module='numba', Z0Z_type_name='uint64'),
|
|
@@ -234,7 +298,6 @@ if __name__ == '__main__':
|
|
|
234
298
|
ingredientsCount.astFunctionDef.decorator_list = [] # TODO low-priority, handle this more elegantly
|
|
235
299
|
# TODO when I add the function signature in numba style back to the decorator, the logic needs to handle `ProgressBarType:`
|
|
236
300
|
ingredientsCount = decorateCallableWithNumba(ingredientsCount, spices.parametersNumba)
|
|
237
|
-
|
|
238
301
|
ingredientsModule.appendIngredientsFunction(ingredientsCount)
|
|
239
302
|
write_astModule(ingredientsModule, job.pathFilenameModule, job.packageIdentifier)
|
|
240
303
|
|
|
@@ -1,30 +1,61 @@
|
|
|
1
1
|
"""
|
|
2
|
-
Numba
|
|
2
|
+
Map folding AST transformation system: Numba integration and just-in-time compilation optimization.
|
|
3
3
|
|
|
4
|
-
This module provides
|
|
5
|
-
Numba-
|
|
6
|
-
|
|
4
|
+
This module provides the compilation optimization layer of the map folding AST transformation system,
|
|
5
|
+
implementing comprehensive tools for applying Numba's just-in-time compilation to functions generated
|
|
6
|
+
by the core transformation tools. Building upon the dataclass decomposition and function optimization
|
|
7
|
+
capabilities established in the operational core, this module completes the transformation process
|
|
8
|
+
by applying aggressive performance optimizations through strategic compiler configuration.
|
|
7
9
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
The integration addresses the final stage of the transformation process where decomposed, optimized
|
|
11
|
+
functions receive Numba decorators with carefully configured optimization parameters. The module
|
|
12
|
+
handles the complexities of type signature generation, import management, and decorator conflict
|
|
13
|
+
resolution that arise when converting high-level map folding algorithms into machine-optimized code.
|
|
12
14
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
Two primary compilation strategies accommodate different performance and compatibility requirements:
|
|
16
|
+
aggressive optimization for production numerical computing provides maximum performance through
|
|
17
|
+
comprehensive compiler directives including nopython mode, bounds checking elimination, forced
|
|
18
|
+
function inlining, and fastmath optimizations; lightweight optimization maintains broader compatibility
|
|
19
|
+
while achieving significant performance gains through selective compiler optimizations suitable for
|
|
20
|
+
development and testing environments.
|
|
21
|
+
|
|
22
|
+
The strategic application of these optimization configurations enables map folding calculations that
|
|
23
|
+
require hours or days to complete, transforming abstract mathematical algorithms into highly efficient
|
|
24
|
+
computational modules. The compilation layer integrates seamlessly with the broader transformation
|
|
25
|
+
system to produce standalone modules optimized for specific map dimensions and computational contexts.
|
|
16
26
|
"""
|
|
17
27
|
|
|
28
|
+
from astToolkit import identifierDotAttribute, IngredientsFunction, Make
|
|
18
29
|
from collections.abc import Callable, Sequence
|
|
19
|
-
from mapFolding import NotRequired, TypedDict
|
|
20
|
-
from astToolkit import IngredientsFunction, Make, str_nameDOTname
|
|
21
|
-
from astToolkit.transformationTools import write_astModule
|
|
22
30
|
from numba.core.compiler import CompilerBase as numbaCompilerBase
|
|
23
|
-
from typing import Any, cast, Final
|
|
31
|
+
from typing import Any, cast, Final, NotRequired, TypedDict
|
|
24
32
|
import ast
|
|
25
33
|
import dataclasses
|
|
34
|
+
import warnings
|
|
26
35
|
|
|
27
36
|
class ParametersNumba(TypedDict):
|
|
37
|
+
"""
|
|
38
|
+
Configuration parameters for Numba compilation decorators.
|
|
39
|
+
|
|
40
|
+
This TypedDict defines all possible configuration options that can be passed to Numba's
|
|
41
|
+
`@jit` decorator to control compilation behavior. The parameters enable fine-tuned control
|
|
42
|
+
over optimization strategies, debugging features, and runtime behavior.
|
|
43
|
+
|
|
44
|
+
Key compilation modes:
|
|
45
|
+
nopython: Forces compilation without Python fallback for maximum performance
|
|
46
|
+
cache: Enables compilation result caching to disk for faster subsequent runs
|
|
47
|
+
fastmath: Allows aggressive mathematical optimizations at cost of IEEE compliance
|
|
48
|
+
parallel: Enables automatic parallelization of supported operations
|
|
49
|
+
|
|
50
|
+
Debug and development options:
|
|
51
|
+
debug: Enables debug information generation
|
|
52
|
+
boundscheck: Controls array bounds checking (disabled for performance in production)
|
|
53
|
+
error_model: Defines how numerical errors are handled ('numpy' vs 'python')
|
|
54
|
+
|
|
55
|
+
Only `cache`, `error_model`, and `fastmath` are required fields. All other fields are
|
|
56
|
+
optional via `NotRequired`, allowing flexible configuration while requiring explicit
|
|
57
|
+
decisions on critical performance and correctness parameters.
|
|
58
|
+
"""
|
|
28
59
|
_dbg_extend_lifetimes: NotRequired[bool]
|
|
29
60
|
_dbg_optnone: NotRequired[bool]
|
|
30
61
|
_nrt: NotRequired[bool]
|
|
@@ -49,22 +80,121 @@ class ParametersNumba(TypedDict):
|
|
|
49
80
|
target: NotRequired[str]
|
|
50
81
|
|
|
51
82
|
parametersNumbaDefault: Final[ParametersNumba] = { '_nrt': True, 'boundscheck': False, 'cache': True, 'error_model': 'numpy', 'fastmath': True, 'forceinline': True, 'inline': 'always', 'looplift': False, 'no_cfunc_wrapper': False, 'no_cpython_wrapper': False, 'nopython': True, 'parallel': False, }
|
|
52
|
-
"""
|
|
83
|
+
"""
|
|
84
|
+
Comprehensive Numba configuration for maximum performance optimization.
|
|
85
|
+
|
|
86
|
+
This configuration provides aggressive optimization settings suitable for production
|
|
87
|
+
numerical computing code. Key characteristics:
|
|
88
|
+
Enables nopython mode for full compilation without Python fallback
|
|
89
|
+
Disables bounds checking for maximum array access performance
|
|
90
|
+
Forces function inlining with 'always' policy for reduced call overhead
|
|
91
|
+
Uses numpy error model for consistent numerical behavior
|
|
92
|
+
Enables fastmath for aggressive floating-point optimizations
|
|
93
|
+
Disables loop lifting to prevent unexpected performance penalties
|
|
94
|
+
|
|
95
|
+
The configuration prioritizes execution speed over compatibility, producing highly
|
|
96
|
+
optimized machine code at the cost of reduced interoperability with uncompiled
|
|
97
|
+
Python functions.
|
|
98
|
+
"""
|
|
99
|
+
|
|
53
100
|
parametersNumbaLight: Final[ParametersNumba] = {'cache': True, 'error_model': 'numpy', 'fastmath': True, 'forceinline': True}
|
|
101
|
+
"""
|
|
102
|
+
Minimal Numba configuration for basic optimization with maximum compatibility.
|
|
103
|
+
|
|
104
|
+
This lightweight configuration provides essential optimizations while maintaining
|
|
105
|
+
broad compatibility with existing Python code. Suitable for:
|
|
106
|
+
Development and debugging phases
|
|
107
|
+
Code that needs to interoperate with non-Numba functions
|
|
108
|
+
Situations where full nopython mode causes compilation issues
|
|
109
|
+
|
|
110
|
+
Key features:
|
|
111
|
+
Enables compilation caching for faster subsequent runs
|
|
112
|
+
Uses numpy error model for consistent mathematical behavior
|
|
113
|
+
Enables fastmath and function inlining for performance gains
|
|
114
|
+
Allows Python object mode fallback when needed
|
|
115
|
+
"""
|
|
116
|
+
|
|
117
|
+
Z0Z_numbaDataTypeModule: identifierDotAttribute = 'numba'
|
|
118
|
+
"""
|
|
119
|
+
Module identifier for Numba imports and type annotations.
|
|
120
|
+
|
|
121
|
+
This constant specifies the module path used when importing Numba-specific types and decorators
|
|
122
|
+
in generated code. It serves as the single source of truth for the Numba module reference,
|
|
123
|
+
enabling consistent import statements across all generated functions.
|
|
124
|
+
"""
|
|
54
125
|
|
|
55
|
-
Z0Z_numbaDataTypeModule: str_nameDOTname = 'numba'
|
|
56
126
|
Z0Z_decoratorCallable: str = 'jit'
|
|
127
|
+
"""
|
|
128
|
+
The Numba decorator function name used for just-in-time compilation.
|
|
129
|
+
|
|
130
|
+
This constant identifies the specific Numba decorator applied to functions for compilation.
|
|
131
|
+
While Numba offers multiple decorators (`@jit`, `@njit`, `@vectorize`), this toolkit focuses
|
|
132
|
+
on the general-purpose `@jit` decorator with configurable parameters for flexibility.
|
|
133
|
+
"""
|
|
57
134
|
|
|
58
135
|
def decorateCallableWithNumba(ingredientsFunction: IngredientsFunction, parametersNumba: ParametersNumba | None = None) -> IngredientsFunction:
|
|
136
|
+
"""
|
|
137
|
+
Transform a Python function into a Numba-accelerated version with appropriate decorators.
|
|
138
|
+
|
|
139
|
+
This function applies Numba's @jit decorator to an existing function definition within
|
|
140
|
+
an IngredientsFunction container. It handles the complete transformation pipeline:
|
|
141
|
+
|
|
142
|
+
1. Removes any existing decorators that might conflict with Numba
|
|
143
|
+
2. Constructs type signatures for Numba compilation when possible
|
|
144
|
+
3. Applies the @jit decorator with specified or default parameters
|
|
145
|
+
4. Updates import requirements to include necessary Numba modules
|
|
146
|
+
|
|
147
|
+
The transformation preserves function semantics while enabling significant performance
|
|
148
|
+
improvements through just-in-time compilation. Type inference is attempted for
|
|
149
|
+
function parameters and return values to enable optimized compilation paths.
|
|
150
|
+
Parameters:
|
|
151
|
+
ingredientsFunction: Container holding the function definition and associated metadata
|
|
152
|
+
parametersNumba: Optional Numba configuration; uses parametersNumbaDefault if None
|
|
153
|
+
Returns:
|
|
154
|
+
Modified IngredientsFunction with Numba decorator applied and imports updated
|
|
155
|
+
"""
|
|
59
156
|
def Z0Z_UnhandledDecorators(astCallable: ast.FunctionDef) -> ast.FunctionDef:
|
|
157
|
+
"""
|
|
158
|
+
Remove existing decorators from function definition to prevent conflicts with Numba.
|
|
159
|
+
|
|
160
|
+
Numba compilation can be incompatible with certain Python decorators, so this
|
|
161
|
+
function strips all existing decorators from a function definition before
|
|
162
|
+
applying the Numba @jit decorator. Removed decorators are logged as warnings
|
|
163
|
+
for debugging purposes.
|
|
164
|
+
|
|
165
|
+
TODO: Implement more sophisticated decorator handling that can preserve
|
|
166
|
+
compatible decorators and intelligently handle decorator composition.
|
|
167
|
+
|
|
168
|
+
Parameters:
|
|
169
|
+
astCallable: Function definition AST node to process
|
|
170
|
+
|
|
171
|
+
Returns:
|
|
172
|
+
astCallable: Function definition with decorator list cleared
|
|
173
|
+
"""
|
|
60
174
|
# TODO: more explicit handling of decorators. I'm able to ignore this because I know `algorithmSource` doesn't have any decorators.
|
|
61
175
|
for decoratorItem in astCallable.decorator_list.copy():
|
|
62
|
-
import warnings
|
|
63
176
|
astCallable.decorator_list.remove(decoratorItem)
|
|
64
177
|
warnings.warn(f"Removed decorator {ast.unparse(decoratorItem)} from {astCallable.name}")
|
|
65
178
|
return astCallable
|
|
66
179
|
|
|
67
180
|
def makeSpecialSignatureForNumba(signatureElement: ast.arg) -> ast.Subscript | ast.Name | None: # pyright: ignore[reportUnusedFunction]
|
|
181
|
+
"""
|
|
182
|
+
Generate Numba-compatible type signatures for function parameters.
|
|
183
|
+
|
|
184
|
+
This function analyzes function parameter type annotations and converts them into
|
|
185
|
+
Numba-compatible type signature expressions. It handles various annotation patterns
|
|
186
|
+
including array types with shape and dtype information, scalar types with simple
|
|
187
|
+
name annotations, and complex subscripted types requiring special handling.
|
|
188
|
+
|
|
189
|
+
The generated signatures enable Numba to perform more efficient compilation by
|
|
190
|
+
providing explicit type information rather than relying solely on type inference.
|
|
191
|
+
|
|
192
|
+
Parameters:
|
|
193
|
+
signatureElement: Function parameter with type annotation to convert
|
|
194
|
+
|
|
195
|
+
Returns:
|
|
196
|
+
Numba-compatible type signature AST node, or None if conversion not possible
|
|
197
|
+
"""
|
|
68
198
|
if isinstance(signatureElement.annotation, ast.Subscript) and isinstance(signatureElement.annotation.slice, ast.Tuple):
|
|
69
199
|
annotationShape: ast.expr = signatureElement.annotation.slice.elts[0]
|
|
70
200
|
if isinstance(annotationShape, ast.Subscript) and isinstance(annotationShape.slice, ast.Tuple):
|
|
@@ -113,7 +243,7 @@ def decorateCallableWithNumba(ingredientsFunction: IngredientsFunction, paramete
|
|
|
113
243
|
ingredientsFunction.astFunctionDef = Z0Z_UnhandledDecorators(ingredientsFunction.astFunctionDef)
|
|
114
244
|
if parametersNumba is None:
|
|
115
245
|
parametersNumba = parametersNumbaDefault
|
|
116
|
-
listDecoratorKeywords: list[ast.keyword] = [Make.keyword(parameterName, Make.Constant(parameterValue)) for parameterName, parameterValue in parametersNumba.items()]
|
|
246
|
+
listDecoratorKeywords: list[ast.keyword] = [Make.keyword(parameterName, Make.Constant(parameterValue)) for parameterName, parameterValue in parametersNumba.items()] # pyright: ignore[reportArgumentType]
|
|
117
247
|
|
|
118
248
|
decoratorModule = Z0Z_numbaDataTypeModule
|
|
119
249
|
decoratorCallable = Z0Z_decoratorCallable
|
|
@@ -127,6 +257,28 @@ def decorateCallableWithNumba(ingredientsFunction: IngredientsFunction, paramete
|
|
|
127
257
|
|
|
128
258
|
@dataclasses.dataclass
|
|
129
259
|
class SpicesJobNumba:
|
|
260
|
+
"""
|
|
261
|
+
Configuration container for Numba-specific job processing options.
|
|
262
|
+
|
|
263
|
+
This dataclass encapsulates configuration settings that control how Numba
|
|
264
|
+
compilation and execution is applied to job processing functions. It provides
|
|
265
|
+
a centralized way to manage Numba-specific settings that affect both
|
|
266
|
+
compilation behavior and runtime features like progress reporting.
|
|
267
|
+
|
|
268
|
+
The class serves as a bridge between the generic job processing system and
|
|
269
|
+
Numba's specialized requirements, enabling consistent application of
|
|
270
|
+
optimization settings across different computational contexts.
|
|
271
|
+
|
|
272
|
+
Attributes:
|
|
273
|
+
useNumbaProgressBar: Enable progress bar display for long-running computations
|
|
274
|
+
numbaProgressBarIdentifier: Progress bar implementation identifier
|
|
275
|
+
parametersNumba: Numba compilation parameters with sensible defaults
|
|
276
|
+
"""
|
|
130
277
|
useNumbaProgressBar: bool = True
|
|
278
|
+
"""Enable progress bar display for Numba-compiled functions with long execution times."""
|
|
279
|
+
|
|
131
280
|
numbaProgressBarIdentifier: str = 'ProgressBarGroupsOfFolds'
|
|
281
|
+
"""Identifier for the progress bar implementation used in Numba-compiled code."""
|
|
282
|
+
|
|
132
283
|
parametersNumba: ParametersNumba = dataclasses.field(default_factory=ParametersNumba) # pyright: ignore[reportArgumentType, reportCallIssue, reportUnknownVariableType]
|
|
284
|
+
"""Numba compilation parameters; defaults to empty dict allowing decorator defaults."""
|