vgslify.torch package

Submodules

vgslify.torch.layers module

class vgslify.torch.layers.TorchLayerFactory(input_shape=None)[source]

Bases: LayerFactory

TorchLayerFactory is responsible for creating PyTorch-specific layers based on parsed VGSL (Variable-size Graph Specification Language) specifications.

This factory handles the creation of various types of layers, including convolutional layers, pooling layers, RNN layers, dense layers, activation layers, and more.

layers

A list of PyTorch layers that have been added to the factory.

Type:

list

shape

The current shape of the tensor, excluding the batch size.

Type:

tuple of int

_input_shape

The original input shape provided during initialization.

Type:

tuple of int or None

build(name='VGSL_Model')[source]

Build the final model using the accumulated layers.

Parameters:

name (str, optional) – The name of the model, by default “VGSL_Model”

Returns:

The constructed PyTorch model.

Return type:

torch.nn.Module

Raises:
  • ValueError – If no layers have been added to the model.

  • ValueError – If no input shape has been specified for the model.

classmethod get_custom_layer_registry()[source]

Return the dict of all registered custom layers for this factory class.

classmethod register(prefix, builder_fn)[source]

Register a custom layer builder function under a given spec prefix.

Parameters:
  • prefix (str) – The VGSL spec prefix that triggers this custom layer (e.g. “Xsw”).

  • builder_fn (callable) – A function with signature builder_fn(self, spec: str) -> layer that, given the VGSL spec string, returns the framework-specific layer.

vgslify.torch.layers.register_custom_layer(prefix)[source]

Decorator to register a custom layer builder function for TorchLayerFactory.

This allows users to easily extend TorchLayerFactory with custom layer types by defining a function that constructs a PyTorch layer from a VGSL spec string.

Parameters:

prefix (str) – The VGSL spec prefix that triggers this custom layer (e.g. “Xsw”).

Returns:

A decorator that registers the provided function as a builder for the given prefix.

Return type:

Callable

Raises:

ValueError – If a builder for the prefix is already registered or if the function signature is invalid.

Examples

>>> from vgslify.torch.layers import register_custom_layer
>>> from torch import nn
>>> @register_custom_layer("Xsw")
... def build_custom_layer(factory, spec):
...     # Custom layer building logic
...     return nn.Linear(factory.shape[-1], 10)
class vgslify.torch.reshape.Reshape(*args: Any, **kwargs: Any)[source]

Bases: Module

Custom PyTorch Reshape layer. To be used in the VGSL spec.

forward(x)[source]

Forward pass for reshaping the input tensor.

Parameters:

x (torch.Tensor) – Input tensor to reshape.

Returns:

Reshaped tensor.

Return type:

torch.Tensor

Module contents

class vgslify.torch.TorchLayerFactory(input_shape=None)[source]

Bases: LayerFactory

TorchLayerFactory is responsible for creating PyTorch-specific layers based on parsed VGSL (Variable-size Graph Specification Language) specifications.

This factory handles the creation of various types of layers, including convolutional layers, pooling layers, RNN layers, dense layers, activation layers, and more.

layers

A list of PyTorch layers that have been added to the factory.

Type:

list

shape

The current shape of the tensor, excluding the batch size.

Type:

tuple of int

_input_shape

The original input shape provided during initialization.

Type:

tuple of int or None

build(name='VGSL_Model')[source]

Build the final model using the accumulated layers.

Parameters:

name (str, optional) – The name of the model, by default “VGSL_Model”

Returns:

The constructed PyTorch model.

Return type:

torch.nn.Module

Raises:
  • ValueError – If no layers have been added to the model.

  • ValueError – If no input shape has been specified for the model.

classmethod get_custom_layer_registry()[source]

Return the dict of all registered custom layers for this factory class.

classmethod register(prefix, builder_fn)[source]

Register a custom layer builder function under a given spec prefix.

Parameters:
  • prefix (str) – The VGSL spec prefix that triggers this custom layer (e.g. “Xsw”).

  • builder_fn (callable) – A function with signature builder_fn(self, spec: str) -> layer that, given the VGSL spec string, returns the framework-specific layer.

vgslify.torch.register_custom_layer(prefix)[source]

Decorator to register a custom layer builder function for TorchLayerFactory.

This allows users to easily extend TorchLayerFactory with custom layer types by defining a function that constructs a PyTorch layer from a VGSL spec string.

Parameters:

prefix (str) – The VGSL spec prefix that triggers this custom layer (e.g. “Xsw”).

Returns:

A decorator that registers the provided function as a builder for the given prefix.

Return type:

Callable

Raises:

ValueError – If a builder for the prefix is already registered or if the function signature is invalid.

Examples

>>> from vgslify.torch.layers import register_custom_layer
>>> from torch import nn
>>> @register_custom_layer("Xsw")
... def build_custom_layer(factory, spec):
...     # Custom layer building logic
...     return nn.Linear(factory.shape[-1], 10)