Deeploy.CommonExtensions.CodeTransformationPasses.IntrospectiveCodeTransformation.IntrospectiveCodeTransformationMixIn
- class Deeploy.CommonExtensions.CodeTransformationPasses.IntrospectiveCodeTransformation.IntrospectiveCodeTransformationMixIn
Bases:
objectA mix-in class providing introspective code transformation capabilities for template-based code generation.
This class enables analysis and manipulation of template code by parsing it into an abstract syntax tree (AST), allowing for dynamic transformations such as variable indexing, dereferencing, and extraction of dynamic references. It is designed to work with template objects and their parse trees, supporting advanced code introspection and modification tasks commonly required in code generation frameworks.
Key Features
Parse template source code into a tree structure for introspection.
Programmatically index or dereference variables within templates.
Extract dynamic references (e.g., buffers, tensors) used in code blocks.
Support for unrolling struct references and distinguishing between local/global context.
Efficient caching of parse trees for repeated template analysis.
Intended Usage
This mix-in is intended to be used with classes that manage code templates, enabling them to inspect and transform template code at runtime. It is particularly useful in scenarios where code generation must adapt dynamically to context or user input, such as in neural network frameworks or domain-specific languages.
Methods
- __init__()
__init__()dereferenceVars(template, varNames)Dereference the specified variables in the given template.
extractDynamicReferences(ctxt[, ...])Extract all dynamic references from the given execution block.
indexVars(template, varNames, index)Index the specified variables in the given template.
Attributes
parseTreeDict- static indexVars(template: Template, varNames: List[str], index: str) None
Index the specified variables in the given template.
Modifies the template in place by indexing the specified variable names.
- Parameters:
template (Template) – The template to modify.
varNames (List[str]) – The variable names to index.
index (str) – The index to use.
- static dereferenceVars(template: Template, varNames: List[str]) None
Dereference the specified variables in the given template.
This function modifies the provided template in place by dereferencing the variables listed in varNames. The template is modified in place.
- Parameters:
template (Template) – The template object to be modified.
varNames (list of str) – List of variable names to dereference within the template.
- extractDynamicReferences(ctxt: NetworkContext, executionBlock: ExecutionBlock = None, unrollStructs=False, includeGlobalReferences=False)
Extract all dynamic references from the given execution block.
- Parameters:
ctxt (NetworkContext) – The network context.
executionBlock (ExecutionBlock, optional) – The execution block.
unrollStructs (bool, optional) – Whether to unroll structs.
includeGlobalReferences (bool, optional) – Whether to include global references.
- Returns:
A list of dynamic references.
- Return type:
List[str]