Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Helpers/LayerHelper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using AiDotNet.NeuralNetworks.Layers.Graph;

namespace AiDotNet.Helpers;

/// <summary>
Expand Down
21 changes: 16 additions & 5 deletions src/LoRA/DefaultLoRAConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using AiDotNet.Interfaces;
using AiDotNet.LoRA.Adapters;
using AiDotNet.NeuralNetworks.Layers;
using AiDotNet.NeuralNetworks.Layers.Graph;

namespace AiDotNet.LoRA;

Expand Down Expand Up @@ -205,16 +206,17 @@ public DefaultLoRAConfiguration(
/// <para><b>Supported Layer Types:</b>
/// - <b>Dense/Linear:</b> DenseLayer, FullyConnectedLayer, FeedForwardLayer
/// - <b>Convolutional:</b> ConvolutionalLayer, DeconvolutionalLayer, DepthwiseSeparableConvolutionalLayer,
/// DilatedConvolutionalLayer, SeparableConvolutionalLayer, SubpixelConvolutionalLayer, GraphConvolutionalLayer
/// DilatedConvolutionalLayer, SeparableConvolutionalLayer, SubpixelConvolutionalLayer
/// - <b>Recurrent:</b> LSTMLayer, GRULayer, RecurrentLayer, ConvLSTMLayer, BidirectionalLayer
/// - <b>Attention:</b> AttentionLayer, MultiHeadAttentionLayer, SelfAttentionLayer
/// - <b>Transformer:</b> TransformerEncoderLayer, TransformerDecoderLayer
/// - <b>Embedding:</b> EmbeddingLayer, PatchEmbeddingLayer
/// - <b>Specialized:</b> LocallyConnectedLayer, HighwayLayer, GatedLinearUnitLayer, SqueezeAndExcitationLayer
/// - <b>Advanced:</b> CapsuleLayer, PrimaryCapsuleLayer, DigitCapsuleLayer, ConditionalRandomFieldLayer
///
/// <b>Excluded Layer Types</b> (no trainable weights or not suitable):
/// - Activation, Pooling, Dropout, Flatten, Reshape, Normalization, etc.
/// <b>Excluded Layer Types:</b>
/// - Activation, Pooling, Dropout, Flatten, Reshape, Normalization (no trainable weights)
/// - GraphConvolutionalLayer (requires specialized adapter that implements IGraphConvolutionLayer)
/// </para>
/// <para><b>For Beginners:</b> This method decides whether to add LoRA to each layer.
///
Expand Down Expand Up @@ -300,12 +302,21 @@ layer is DepthwiseSeparableConvolutionalLayer<T> || layer is DilatedConvolutiona

// Specialized layers with trainable weights
if (layer is LocallyConnectedLayer<T> || layer is HighwayLayer<T> ||
layer is GatedLinearUnitLayer<T> || layer is SqueezeAndExcitationLayer<T> ||
layer is GraphConvolutionalLayer<T>)
layer is GatedLinearUnitLayer<T> || layer is SqueezeAndExcitationLayer<T>)
{
return CreateAdapter(layer);
}

// NOTE: GraphConvolutionalLayer is intentionally excluded from LoRA adaptation
// because StandardLoRAAdapter does not implement IGraphConvolutionLayer<T>,
// which breaks type checks in GraphNeuralNetwork (SetAdjacencyMatrix, etc.).
// Future work: Create GraphLoRAAdapter<T> that implements IGraphConvolutionLayer<T>
// and delegates graph-specific methods to the wrapped layer.
if (layer is GraphConvolutionalLayer<T>)
{
return layer; // Return unwrapped for now
}

// Capsule layers
if (layer is CapsuleLayer<T> || layer is PrimaryCapsuleLayer<T> || layer is DigitCapsuleLayer<T>)
{
Expand Down
2 changes: 2 additions & 0 deletions src/NeuralNetworks/GraphNeuralNetwork.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using AiDotNet.NeuralNetworks.Layers.Graph;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Update GraphNeuralNetwork to use IGraphConvolutionLayer<T> and the new SetAdjacencyMatrix/Forward contract

This file still hard-codes GraphConvolutionalLayer<T> and a two-argument Forward that no longer exists, and it ignores the new graph layers that implement IGraphConvolutionLayer<T>.

Concrete issues:

  • Line 532: graphLayer.Forward(current, adjacencyMatrix) no longer compiles because GraphConvolutionalLayer<T> only exposes SetAdjacencyMatrix(Tensor<T>) + Forward(Tensor<T>).
  • Lines 529–545: Only GraphConvolutionalLayer<T> is treated as a graph layer; GraphSAGELayer<T>, GraphAttentionLayer<T>, and GraphIsomorphismLayer<T> (all IGraphConvolutionLayer<T>) will be treated as standard layers and never receive an adjacency matrix. Those layers will throw at runtime when Forward sees a null adjacency.
  • Lines 689–695: Predict skips only GraphConvolutionalLayer<T> but should skip all IGraphConvolutionLayer<T> when adjacency is unavailable.
  • Lines 890–892: GetModelMetadata counts graph layers only via is GraphConvolutionalLayer<T>, so all new graph layers are misclassified as “standard”.

I recommend centralizing on IGraphConvolutionLayer<T> and decoupling adjacency from the specific implementation, e.g.:

-        foreach (var layer in Layers)
-        {
-            if (layer is GraphConvolutionalLayer<T> graphLayer)
-            {
-                current = graphLayer.Forward(current, adjacencyMatrix);
-            }
-            else if (layer is ILayer<T> standardLayer)
-            {
-                // Handle non-graph layers (e.g., Dense, Activation)
-                current = standardLayer.Forward(current);
-            }
-            else
-            {
-                throw new InvalidOperationException($"Unsupported layer type: {layer.GetType().Name}");
-            }
-            ...
-        }
+        foreach (var layer in Layers)
+        {
+            if (layer is IGraphConvolutionLayer<T> graphLayer)
+            {
+                graphLayer.SetAdjacencyMatrix(adjacencyMatrix);
+                current = layer.Forward(current);
+            }
+            else
+            {
+                current = layer.Forward(current);
+            }
+
+            if (current.Rank < 2)
+                throw new InvalidOperationException($"Layer {layer.GetType().Name} produced an invalid output shape.");
+        }

And similarly for Predict and diagnostics:

-            if (Layers[i] is GraphConvolutionalLayer<T>)
+            if (Layers[i] is IGraphConvolutionLayer<T>)
             {
                 // For graph layers, we need adjacency information which is not available
                 // Just pass through without modification for standard prediction
                 continue;
             }
-                { "GraphLayerCount", Layers.Count(l => l is GraphConvolutionalLayer<T>) },
-                { "StandardLayerCount", Layers.Count(l => !(l is GraphConvolutionalLayer<T>)) },
+                { "GraphLayerCount", Layers.Count(l => l is IGraphConvolutionLayer<T>) },
+                { "StandardLayerCount", Layers.Count(l => l is not IGraphConvolutionLayer<T>) },

This will (1) fix the compile-time break with GraphConvolutionalLayer<T>, and (2) ensure all graph layers conforming to IGraphConvolutionLayer<T> are handled consistently.

Also applies to: 529-559, 685-701, 887-893

🤖 Prompt for AI Agents
In src/NeuralNetworks/GraphNeuralNetwork.cs around lines 529-559, 685-701 and
887-893 the code treats only GraphConvolutionalLayer<T> as a graph layer and
calls a non-existent two-argument Forward, causing compile and runtime errors;
change the checks to use the IGraphConvolutionLayer<T> interface, call
SetAdjacencyMatrix(adjacencyMatrix) on that interface (when adjacency is
present) and then call its single-argument Forward(current) (or Forward() as
defined), and update Predict and GetModelMetadata to skip/count all
IGraphConvolutionLayer<T> implementations when adjacency is unavailable; ensure
null-checks for adjacency before calling SetAdjacencyMatrix and handle layers
uniformly via the interface rather than concrete GraphConvolutionalLayer<T>.


namespace AiDotNet.NeuralNetworks;

/// <summary>
Expand Down
Loading
Loading