Skip to content

Conversation

@TimKoornstra
Copy link
Owner

Summary

This PR adds the ability to register and parse custom layers for both TensorFlow and PyTorch backends in VGSLify. It also introduces a few breaking changes related to file organization and simplifies import paths to make the library easier to use.

New Features

  1. Custom Layer Registration (TensorFlow and PyTorch)

    • You can now register custom layer builder functions directly with the TensorFlowLayerFactoryand (similarly) with the Torch layer factory.

      from vgslify.tensorflow import TensorFlowLayerFactory
      
      @classmethod
      def register(cls, prefix: str, builder_fn):
          ...
    • Alternatively, use the new decorator approach:

      from vgslify.tensorflow import register_custom_layer
      
      @register_custom_layer("Xsw")
      def build_custom_layer(factory, spec):
          # Custom layer logic
          return tf.keras.layers.Dense(10)
    • This allows users to incorporate any custom TensorFlow or PyTorch layers into a VGSL specification simply by providing a prefix and builder function.

  2. Custom Parser Registration for Model -> Spec

    • To convert a custom layer back into a VGSL specification, you can now register parser functions:

      from vgslify.model_parsers 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}})"
    • These functions are then picked up automatically by the parser (TensorFlowModelParser or TorchModelParser) to handle custom layers.

Breaking Changes

  1. Module Renaming and Reorganization

    • vgslify.parsers is now vgslify.model_parsers.
    • tf_parser.py, torch_parser.py, and base_parser.py have been reorganized into tensorflow/, torch/, and base/ directories respectively.
    • vgslify.core.parser is now called vgslify.core.spec_parser.
  2. Simplified Imports

    • You can now import the main classes and utilities more directly:

      from vgslify import VGSLModelGenerator, model_to_spec
    • Parser imports have been simplified:

      from vgslify.tensorflow import TensorFlowModelParser
      from vgslify.torch import TorchModelParser
    • Layer factory imports have also been unified:

      from vgslify.tensorflow import TensorFlowLayerFactory, register_custom_layer

Example Usage

from vgslify import VGSLModelGenerator, model_to_spec
from vgslify.tensorflow import TensorFlowLayerFactory, register_custom_layer
from vgslify.model_parsers import register_custom_parser

# Register a custom layer builder
@register_custom_layer("Xsw")
def my_custom_builder(factory, spec):
    # Implement custom layer creation here
    return tf.keras.layers.Dense(10)

# Register a custom parser for that layer
@register_custom_parser(MyCustomLayer)
def my_custom_parser(layer):
    # Return the spec string for this custom layer
    return f"Xsw({{layer.units}})"

# Example model creation and spec conversion
model = ...
vgsl_spec = model_to_spec(model)

@TimKoornstra TimKoornstra merged commit ed80f4b into main Feb 1, 2025
1 check failed
@TimKoornstra TimKoornstra deleted the feat/user-defined-layers branch February 1, 2025 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants