vgslify.model_parsers package
Submodules
vgslify.model_parsers.base module
- class vgslify.model_parsers.base.BaseModelParser[source]
Bases:
ABC
Abstract base class for model parsers. Provides common utility methods for parsing different frameworks and generating VGSL spec strings.
- generate_vgsl(configs)[source]
Convert a list of layer configuration dataclasses into a VGSL specification string.
- Parameters:
configs (List[Union[Conv2DConfig, Pooling2DConfig, DenseConfig, RNNConfig,) – DropoutConfig, ReshapeConfig, InputConfig, ActivationConfig]] List of layer configurations.
- Returns:
VGSL specification string.
- Return type:
str
- abstract parse_activation(layer)[source]
Parse the Activation layer into a ActivationConfig dataclass.
- Return type:
- abstract parse_batchnorm(layer)[source]
Parse the BatchNorm layer into a VGSL spec string.
- Return type:
str
- abstract parse_conv2d(layer)[source]
Parse the Conv2D layer into a Conv2DConfig dataclass.
- Return type:
- abstract parse_dense(layer)[source]
Parse the Dense layer into a DenseConfig dataclass.
- Return type:
- abstract parse_dropout(layer)[source]
Parse the Dropout layer into a DropoutConfig dataclass.
- Return type:
- abstract parse_flatten(layer)[source]
Parse the Flatten layer into a VGSL spec string.
- Return type:
str
- abstract parse_input(layer)[source]
Parse the input layer into a InputConfig dataclass.
- Return type:
- abstract parse_pooling(layer)[source]
Parse the Pooling layer into a Pooling2DConfig dataclass.
- Return type:
vgslify.model_parsers.torch module
- class vgslify.model_parsers.torch.TorchModelParser[source]
Bases:
BaseModelParser
Parser for converting PyTorch models into VGSL (Variable-size Graph Specification Language) spec strings.
This class extends the BaseModelParser to provide specific functionality for PyTorch models. It uses configuration dataclasses to represent different layer types and converts them into VGSL spec strings.
- layer_parsers
A dictionary mapping PyTorch layer types to their corresponding parsing methods.
- Type:
Dict[Type[nn.Module], Callable]
Notes
This parser supports a wide range of PyTorch layers and can be extended to support additional layer types by adding new parsing methods and updating the layer_parsers dictionary.
- classmethod get_custom_parsers()[source]
Return the dict of all registered custom parser functions.
- parse_activation(layer)[source]
Parse an activation function.
- Parameters:
layer (nn.Module) – The activation layer to parse.
- Returns:
The configuration for the Activation layer.
- Return type:
- parse_batchnorm(layer)[source]
Parse a BatchNorm2d layer.
- Parameters:
layer (nn.BatchNorm2d) – The BatchNorm2d layer to parse.
- Returns:
Indicates that the VGSL spec should include ‘Bn’.
- Return type:
str
- parse_conv2d(layer)[source]
Parse a Conv2d layer into a Conv2DConfig dataclass.
- Parameters:
layer (nn.Conv2d) – The Conv2d layer to parse.
- Returns:
The configuration for the Conv2D layer.
- Return type:
- parse_dense(layer)[source]
Parse a Linear layer into a DenseConfig dataclass.
- Parameters:
layer (nn.Linear) – The Linear layer to parse.
- Returns:
The configuration for the Dense layer.
- Return type:
- parse_dropout(layer)[source]
Parse a Dropout layer into a DropoutConfig dataclass.
- Parameters:
layer (nn.Dropout) – The Dropout layer to parse.
- Returns:
The configuration for the Dropout layer.
- Return type:
- parse_flatten(layer)[source]
Parse a Flatten layer.
- Parameters:
layer (nn.Flatten) – The Flatten layer to parse.
- Returns:
Indicates that the VGSL spec should include ‘Flatten’.
- Return type:
str
- parse_input(layer)[source]
Parse the input shape from the first layer of the model.
- Parameters:
layer (nn.Module) – The first layer of the PyTorch model.
- Returns:
The configuration for the input layer.
- Return type:
- Raises:
ValueError – If the input shape cannot be determined.
- parse_model(model)[source]
Parse a PyTorch model into a VGSL spec string.
- Parameters:
model (nn.Module) – PyTorch model to be converted.
- Returns:
VGSL spec string.
- Return type:
str
- Raises:
ValueError – If the model contains unsupported layers or if the input shape is invalid.
- parse_pooling(layer)[source]
Parse a Pooling layer into a Pooling2DConfig dataclass.
- Parameters:
layer (nn.MaxPool2d or nn.AvgPool2d) – The Pooling layer to parse.
- Returns:
The configuration for the Pooling layer.
- Return type:
- parse_reshape(layer)[source]
Parse a Reshape layer into a ReshapeConfig dataclass.
- Parameters:
layer (Reshape) – The custom Reshape layer to parse.
- Returns:
The configuration for the Reshape layer.
- Return type:
- parse_rnn(layer)[source]
Parse an RNN layer (LSTM or GRU) into an RNNConfig dataclass.
- Parameters:
layer (Union[nn.LSTM, nn.GRU]) – The RNN layer to parse.
- Returns:
The configuration for the RNN layer.
- Return type:
- classmethod register(layer_cls, parser_fn)[source]
Registers a custom parser function for a given TF layer class.
- Parameters:
layer_cls (Type[tf.keras.layers.Layer]) – The TF layer class this parser function can handle.
parser_fn (Callable) – A function with signature parser_fn(layer) -> str that returns a VGSL spec string for the given layer.
- vgslify.model_parsers.torch.register_custom_parser(layer_cls)[source]
Decorator to register a custom parser function for a given PyTorch layer class.
This allows users to easily extend the TorchModelParser with custom layer types by defining a function that converts a PyTorch layer into a VGSL specification.
- Parameters:
layer_cls (Type[nn.Module]) – The PyTorch layer class to associate with the parser function.
- Returns:
A decorator that registers the provided function as a parser for layer_cls.
- Return type:
Callable
- Raises:
ValueError – If a parser for layer_cls is already registered or if the function does not accept exactly one argument (the layer instance).
Examples
Registering a custom parser for a MyCustomLayer:
>>> from vgslify.model_parsers.torch import register_custom_parser >>> from torch import nn >>> class MyCustomLayer(nn.Module): ... def __init__(self, param: int): ... super().__init__() ... self.param = param ... >>> @register_custom_parser(MyCustomLayer) ... def parse_my_custom_layer(layer: MyCustomLayer): ... return f"MyCustomSpec({layer.param})" ... >>> # Now the parser is automatically registered inside TorchModelParser
vgslify.model_parsers.tensorflow module
- class vgslify.model_parsers.tensorflow.TensorFlowModelParser[source]
Bases:
BaseModelParser
Parser for converting TensorFlow Keras models into VGSL (Variable-size Graph Specification Language) spec strings.
This class extends the BaseModelParser to provide specific functionality for TensorFlow Keras models. It uses configuration dataclasses to represent different layer types and converts them into VGSL spec strings.
- layer_parsers
A dictionary mapping TensorFlow Keras layer types to their corresponding parsing methods.
- Type:
Dict[Type[tf.keras.layers.Layer], Callable]
Notes
This parser supports a wide range of TensorFlow Keras layers and can be extended to support additional layer types by adding new parsing methods and updating the layer_parsers dictionary.
- classmethod get_custom_parsers()[source]
Return the dict of all registered custom parser functions.
- parse_activation(layer)[source]
Parse an Activation layer.
- Parameters:
layer (tf.keras.layers.Activation) – The Activation layer to parse.
- Returns:
The configuration for the Activation layer.
- Return type:
- parse_batchnorm(layer)[source]
Parse a BatchNormalization layer. Since BatchNormalization does not require a VGSL spec beyond ‘Bn’, return a placeholder.
- Parameters:
layer (tf.keras.layers.BatchNormalization) – The BatchNormalization layer to parse.
- Returns:
Indicates that the VGSL spec should include ‘Bn’.
- Return type:
None
- parse_conv2d(layer)[source]
Parse a Conv2D layer into a Conv2DConfig dataclass.
- Parameters:
layer (tf.keras.layers.Conv2D) – The Conv2D layer to parse.
- Returns:
The configuration for the Conv2D layer.
- Return type:
- parse_dense(layer)[source]
Parse a Dense layer into a DenseConfig dataclass.
- Parameters:
layer (tf.keras.layers.Dense) – The Dense layer to parse.
- Returns:
The configuration for the Dense layer.
- Return type:
- parse_dropout(layer)[source]
Parse a Dropout layer into a DropoutConfig dataclass.
- Parameters:
layer (tf.keras.layers.Dropout) – The Dropout layer to parse.
- Returns:
The configuration for the Dropout layer.
- Return type:
- parse_flatten(layer)[source]
Parse a Flatten layer. Since Flatten does not require a VGSL spec beyond ‘Flatten’, return a placeholder.
- Parameters:
layer (tf.keras.layers.Flatten) – The Flatten layer to parse.
- Returns:
Indicates that the VGSL spec should include ‘Flatten’.
- Return type:
None
- parse_input(layer)[source]
Parse an InputLayer into an InputConfig dataclass.
- Parameters:
layer (tf.keras.layers.InputLayer) – The InputLayer to parse.
- Returns:
The configuration for the input layer.
- Return type:
- parse_model(model)[source]
Parse a TensorFlow Keras model into a VGSL spec string.
- Parameters:
model (tf.keras.models.Model) – Keras model to be converted.
- Returns:
VGSL spec string.
- Return type:
str
- Raises:
ValueError – If the model contains unsupported layers or if the input shape is invalid.
- parse_pooling(layer)[source]
Parse a Pooling layer into a Pooling2DConfig dataclass.
- Parameters:
layer (tf.keras.layers.MaxPooling2D or tf.keras.layers.AveragePooling2D) – The Pooling layer to parse.
- Returns:
The configuration for the Pooling layer.
- Return type:
- parse_reshape(layer)[source]
Parse a Reshape layer into a ReshapeConfig dataclass.
- Parameters:
layer (tf.keras.layers.Reshape) – The Reshape layer to parse.
- Returns:
The configuration for the Reshape layer.
- Return type:
- parse_rnn(layer)[source]
Parse an RNN layer (LSTM, GRU, or Bidirectional) into an RNNConfig dataclass.
- Parameters:
layer (Union[tf.keras.layers.LSTM, tf.keras.layers.GRU, tf.keras.layers.Bidirectional]) – The RNN layer to parse.
- Returns:
The configuration for the RNN layer.
- Return type:
- classmethod register(layer_cls, parser_fn)[source]
Registers a custom parser function for a given TF layer class.
- Parameters:
layer_cls (Type[tf.keras.layers.Layer]) – The TF layer class this parser function can handle.
parser_fn (Callable) – A function with signature parser_fn(layer) -> str that returns a VGSL spec string for the given layer.
- vgslify.model_parsers.tensorflow.register_custom_parser(layer_cls)[source]
Decorator to register a custom parser function for a given TensorFlow Keras layer class.
This allows users to extend TensorFlowModelParser by defining a function that converts a TensorFlow Keras layer into a VGSL specification.
- Parameters:
layer_cls (Type[tf.keras.layers.Layer]) – The TensorFlow Keras layer class to associate with the parser function.
- Returns:
A decorator that registers the provided function as a parser for layer_cls.
- Return type:
Callable
- Raises:
ValueError – If a parser for layer_cls is already registered or if the function does not accept exactly one argument (the layer instance).
Examples
Registering a custom parser for a MyCustomLayer:
>>> from vgslify.model_parsers.tensorflow import register_custom_parser >>> import tensorflow as tf >>> class MyCustomLayer(tf.keras.layers.Layer): ... def __init__(self, units: int): ... super().__init__() ... self.units = units ... >>> @register_custom_parser(MyCustomLayer) ... def parse_my_custom_layer(layer: MyCustomLayer): ... return f"MyCustomSpec({layer.units})" ... >>> # Now the parser is automatically registered inside TensorFlowModelParser
Module contents
- class vgslify.model_parsers.TensorFlowModelParser[source]
Bases:
BaseModelParser
Parser for converting TensorFlow Keras models into VGSL (Variable-size Graph Specification Language) spec strings.
This class extends the BaseModelParser to provide specific functionality for TensorFlow Keras models. It uses configuration dataclasses to represent different layer types and converts them into VGSL spec strings.
- layer_parsers
A dictionary mapping TensorFlow Keras layer types to their corresponding parsing methods.
- Type:
Dict[Type[tf.keras.layers.Layer], Callable]
Notes
This parser supports a wide range of TensorFlow Keras layers and can be extended to support additional layer types by adding new parsing methods and updating the layer_parsers dictionary.
- classmethod get_custom_parsers()[source]
Return the dict of all registered custom parser functions.
- parse_activation(layer)[source]
Parse an Activation layer.
- Parameters:
layer (tf.keras.layers.Activation) – The Activation layer to parse.
- Returns:
The configuration for the Activation layer.
- Return type:
- parse_batchnorm(layer)[source]
Parse a BatchNormalization layer. Since BatchNormalization does not require a VGSL spec beyond ‘Bn’, return a placeholder.
- Parameters:
layer (tf.keras.layers.BatchNormalization) – The BatchNormalization layer to parse.
- Returns:
Indicates that the VGSL spec should include ‘Bn’.
- Return type:
None
- parse_conv2d(layer)[source]
Parse a Conv2D layer into a Conv2DConfig dataclass.
- Parameters:
layer (tf.keras.layers.Conv2D) – The Conv2D layer to parse.
- Returns:
The configuration for the Conv2D layer.
- Return type:
- parse_dense(layer)[source]
Parse a Dense layer into a DenseConfig dataclass.
- Parameters:
layer (tf.keras.layers.Dense) – The Dense layer to parse.
- Returns:
The configuration for the Dense layer.
- Return type:
- parse_dropout(layer)[source]
Parse a Dropout layer into a DropoutConfig dataclass.
- Parameters:
layer (tf.keras.layers.Dropout) – The Dropout layer to parse.
- Returns:
The configuration for the Dropout layer.
- Return type:
- parse_flatten(layer)[source]
Parse a Flatten layer. Since Flatten does not require a VGSL spec beyond ‘Flatten’, return a placeholder.
- Parameters:
layer (tf.keras.layers.Flatten) – The Flatten layer to parse.
- Returns:
Indicates that the VGSL spec should include ‘Flatten’.
- Return type:
None
- parse_input(layer)[source]
Parse an InputLayer into an InputConfig dataclass.
- Parameters:
layer (tf.keras.layers.InputLayer) – The InputLayer to parse.
- Returns:
The configuration for the input layer.
- Return type:
- parse_model(model)[source]
Parse a TensorFlow Keras model into a VGSL spec string.
- Parameters:
model (tf.keras.models.Model) – Keras model to be converted.
- Returns:
VGSL spec string.
- Return type:
str
- Raises:
ValueError – If the model contains unsupported layers or if the input shape is invalid.
- parse_pooling(layer)[source]
Parse a Pooling layer into a Pooling2DConfig dataclass.
- Parameters:
layer (tf.keras.layers.MaxPooling2D or tf.keras.layers.AveragePooling2D) – The Pooling layer to parse.
- Returns:
The configuration for the Pooling layer.
- Return type:
- parse_reshape(layer)[source]
Parse a Reshape layer into a ReshapeConfig dataclass.
- Parameters:
layer (tf.keras.layers.Reshape) – The Reshape layer to parse.
- Returns:
The configuration for the Reshape layer.
- Return type:
- parse_rnn(layer)[source]
Parse an RNN layer (LSTM, GRU, or Bidirectional) into an RNNConfig dataclass.
- Parameters:
layer (Union[tf.keras.layers.LSTM, tf.keras.layers.GRU, tf.keras.layers.Bidirectional]) – The RNN layer to parse.
- Returns:
The configuration for the RNN layer.
- Return type:
- classmethod register(layer_cls, parser_fn)[source]
Registers a custom parser function for a given TF layer class.
- Parameters:
layer_cls (Type[tf.keras.layers.Layer]) – The TF layer class this parser function can handle.
parser_fn (Callable) – A function with signature parser_fn(layer) -> str that returns a VGSL spec string for the given layer.
- class vgslify.model_parsers.TorchModelParser[source]
Bases:
BaseModelParser
Parser for converting PyTorch models into VGSL (Variable-size Graph Specification Language) spec strings.
This class extends the BaseModelParser to provide specific functionality for PyTorch models. It uses configuration dataclasses to represent different layer types and converts them into VGSL spec strings.
- layer_parsers
A dictionary mapping PyTorch layer types to their corresponding parsing methods.
- Type:
Dict[Type[nn.Module], Callable]
Notes
This parser supports a wide range of PyTorch layers and can be extended to support additional layer types by adding new parsing methods and updating the layer_parsers dictionary.
- classmethod get_custom_parsers()[source]
Return the dict of all registered custom parser functions.
- parse_activation(layer)[source]
Parse an activation function.
- Parameters:
layer (nn.Module) – The activation layer to parse.
- Returns:
The configuration for the Activation layer.
- Return type:
- parse_batchnorm(layer)[source]
Parse a BatchNorm2d layer.
- Parameters:
layer (nn.BatchNorm2d) – The BatchNorm2d layer to parse.
- Returns:
Indicates that the VGSL spec should include ‘Bn’.
- Return type:
str
- parse_conv2d(layer)[source]
Parse a Conv2d layer into a Conv2DConfig dataclass.
- Parameters:
layer (nn.Conv2d) – The Conv2d layer to parse.
- Returns:
The configuration for the Conv2D layer.
- Return type:
- parse_dense(layer)[source]
Parse a Linear layer into a DenseConfig dataclass.
- Parameters:
layer (nn.Linear) – The Linear layer to parse.
- Returns:
The configuration for the Dense layer.
- Return type:
- parse_dropout(layer)[source]
Parse a Dropout layer into a DropoutConfig dataclass.
- Parameters:
layer (nn.Dropout) – The Dropout layer to parse.
- Returns:
The configuration for the Dropout layer.
- Return type:
- parse_flatten(layer)[source]
Parse a Flatten layer.
- Parameters:
layer (nn.Flatten) – The Flatten layer to parse.
- Returns:
Indicates that the VGSL spec should include ‘Flatten’.
- Return type:
str
- parse_input(layer)[source]
Parse the input shape from the first layer of the model.
- Parameters:
layer (nn.Module) – The first layer of the PyTorch model.
- Returns:
The configuration for the input layer.
- Return type:
- Raises:
ValueError – If the input shape cannot be determined.
- parse_model(model)[source]
Parse a PyTorch model into a VGSL spec string.
- Parameters:
model (nn.Module) – PyTorch model to be converted.
- Returns:
VGSL spec string.
- Return type:
str
- Raises:
ValueError – If the model contains unsupported layers or if the input shape is invalid.
- parse_pooling(layer)[source]
Parse a Pooling layer into a Pooling2DConfig dataclass.
- Parameters:
layer (nn.MaxPool2d or nn.AvgPool2d) – The Pooling layer to parse.
- Returns:
The configuration for the Pooling layer.
- Return type:
- parse_reshape(layer)[source]
Parse a Reshape layer into a ReshapeConfig dataclass.
- Parameters:
layer (Reshape) – The custom Reshape layer to parse.
- Returns:
The configuration for the Reshape layer.
- Return type:
- parse_rnn(layer)[source]
Parse an RNN layer (LSTM or GRU) into an RNNConfig dataclass.
- Parameters:
layer (Union[nn.LSTM, nn.GRU]) – The RNN layer to parse.
- Returns:
The configuration for the RNN layer.
- Return type:
- classmethod register(layer_cls, parser_fn)[source]
Registers a custom parser function for a given TF layer class.
- Parameters:
layer_cls (Type[tf.keras.layers.Layer]) – The TF layer class this parser function can handle.
parser_fn (Callable) – A function with signature parser_fn(layer) -> str that returns a VGSL spec string for the given layer.