Skip to content
Merged
Changes from all 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
23 changes: 21 additions & 2 deletions src/TorchSharp/NN/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
using System.Reflection;
using System.Runtime.InteropServices;
using TorchSharp.Modules;
using static TorchSharp.PInvoke.LibTorchSharp;
using static TorchSharp.torch;
using static TorchSharp.Utils.LEB128Codec;
using static TorchSharp.PInvoke.LibTorchSharp;
using static Google.Protobuf.Reflection.SourceCodeInfo.Types;

namespace TorchSharp
{
Expand Down Expand Up @@ -756,11 +755,25 @@ public virtual void register_parameter(string name, Parameter param)
throw new InvalidOperationException($"Parameter {name} is already registered.");
}

/// <summary>
/// Alias for register_module()
/// </summary>
/// <param name="name">
/// name of the child module.
/// The child module can be accessed from this module using the given name
/// </param>
/// <param name="module">child module to be added to the module.</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="InvalidOperationException"></exception>
public void add_module(string name, Module module)
=> register_module(name, module);

/// <summary>
/// Register a submodule.
/// </summary>
/// <param name="name">Name of the submodule.</param>
/// <param name="submodule">The module to register.</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="InvalidOperationException"></exception>
public virtual void register_module(string name, Module submodule)
{
Expand All @@ -769,6 +782,12 @@ public virtual void register_module(string name, Module submodule)
_internal_submodules.Remove(name);
}
} else {
if (name.Contains(".")) {
throw new ArgumentException($"module name can't contain \".\", got: {name}");
}
if (string.IsNullOrEmpty(name)) {
throw new ArgumentException("module name can't be empty string \"\"");
}
if (_internal_submodules.ContainsKey(name)) {
throw new InvalidOperationException($"Sub-module {name} is already registered.");
}
Expand Down