vgslify.parsers package

Submodules

vgslify.parsers.base_parser module

class vgslify.parsers.base_parser.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:

ActivationConfig

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:

Conv2DConfig

abstract parse_dense(layer)[source]

Parse the Dense layer into a DenseConfig dataclass.

Return type:

DenseConfig

abstract parse_dropout(layer)[source]

Parse the Dropout layer into a DropoutConfig dataclass.

Return type:

DropoutConfig

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:

InputConfig

abstract parse_model(model)[source]

Parse the model into a VGSL spec string.

Return type:

str

abstract parse_pooling(layer)[source]

Parse the Pooling layer into a Pooling2DConfig dataclass.

Return type:

Pooling2DConfig

abstract parse_reshape(layer)[source]

Parse the Reshape layer into a ReshapeConfig dataclass.

Return type:

ReshapeConfig

abstract parse_rnn(layer)[source]

Parse the RNN layer into a RNNConfig dataclass.

Return type:

RNNConfig

vgslify.parsers.tf_parser module

class vgslify.parsers.tf_parser.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.

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:

ActivationConfig

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:

Conv2DConfig

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:

DenseConfig

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:

DropoutConfig

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:

InputConfig

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, pool_type)[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.

  • pool_type (str) – Type of pooling (‘max’ or ‘average’).

Returns:

The configuration for the Pooling layer.

Return type:

Pooling2DConfig

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:

ReshapeConfig

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:

RNNConfig

vgslify.parsers.torch_parser module

class vgslify.parsers.torch_parser.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.

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:

ActivationConfig

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:

Conv2DConfig

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:

DenseConfig

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:

DropoutConfig

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:

InputConfig

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:

Pooling2DConfig

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:

ReshapeConfig

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:

RNNConfig

Module contents