Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving more modules to C#. #1259

Closed
wants to merge 217 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
217 commits
Select commit Hold shift + click to select a range
39aa1c5
Override 'Module._to()' overloads on some modules that do not have bu…
NiklasGustafsson Feb 27, 2023
7b0350a
Shortcircuit '_to()' for modules that are known not to have buffers o…
NiklasGustafsson Feb 28, 2023
534baaa
Experimental rewrite of some modules.
NiklasGustafsson Mar 3, 2023
ae18c5e
Merge branch 'main' into unit
NiklasGustafsson Mar 6, 2023
7b89432
Slight modification to the experimental re-implementation of Linear.
NiklasGustafsson Mar 6, 2023
4c25d33
Merge branch 'missing' into unit
NiklasGustafsson Mar 6, 2023
bf32faf
Implemented a number of modules as managed code calling into function…
NiklasGustafsson Mar 7, 2023
6316f0f
Merge branch 'main' into unit
NiklasGustafsson Mar 7, 2023
d5b64d3
More modules implemented without native module instances
NiklasGustafsson Mar 7, 2023
0228eb5
Merge branch 'main' into unit
NiklasGustafsson Mar 8, 2023
16cfbc3
Merge branch 'main' into unit
NiklasGustafsson Mar 13, 2023
f844cd5
Merge branch 'main' into unit
NiklasGustafsson Mar 14, 2023
cfaea64
Merge branch 'main' into unit
NiklasGustafsson Mar 17, 2023
06c9ac7
Merge branch 'main' into unit
NiklasGustafsson Mar 27, 2023
4778090
Merge branch 'main' into unit
NiklasGustafsson Mar 27, 2023
339e381
Fixed infinite recursion in bilinear.
NiklasGustafsson Mar 27, 2023
a5f7514
Merge branch 'unit' of https://github.com/NiklasGustafsson/TorchSharp…
NiklasGustafsson Mar 27, 2023
b1ff253
Merge branch 'main' into unit
NiklasGustafsson Mar 28, 2023
3419f73
Merge branch 'main' into unit
NiklasGustafsson Mar 28, 2023
eeba298
Merge branch 'unit' of https://github.com/NiklasGustafsson/TorchSharp…
NiklasGustafsson Mar 30, 2023
0108153
Merge branch 'main' into unit
Apr 4, 2023
a771c6e
Merge branch 'main' into unit
NiklasGustafsson Apr 10, 2023
0130a1d
Merge branch 'unit' of https://github.com/NiklasGustafsson/TorchSharp…
NiklasGustafsson Apr 10, 2023
1dba6d1
Merge branch 'main' into unit
NiklasGustafsson Apr 24, 2023
0f8122f
Merge branch 'main' into unit
NiklasGustafsson Apr 25, 2023
c617c1c
Merge branch 'main' into unit
NiklasGustafsson Apr 26, 2023
0d9132e
Merge branch 'main' into unit
NiklasGustafsson Apr 27, 2023
3c876ef
Merge branch 'main' into unit
NiklasGustafsson Apr 28, 2023
c017dbc
Merge branch 'main' into unit
NiklasGustafsson May 8, 2023
86f22c4
Merge branch 'main' into unit
NiklasGustafsson May 9, 2023
0397f47
Merge branch 'main' into unit
NiklasGustafsson May 9, 2023
5d3e664
Manual merge
NiklasGustafsson May 17, 2023
13761a4
Manual merge
NiklasGustafsson May 18, 2023
e311a07
Corrected faulty merge.
NiklasGustafsson May 18, 2023
0a448cd
Merge branch 'main' into unit
NiklasGustafsson May 24, 2023
0ad47e4
Merge branch 'main' into unit
NiklasGustafsson May 30, 2023
4bb03e7
Merge branch 'main' into unit
Jun 5, 2023
3df1dba
Merge branch 'main' into unit
NiklasGustafsson Jun 12, 2023
577bb38
Merge branch 'main' into unit
NiklasGustafsson Jun 14, 2023
e50ad22
Merge branch 'main' into unit
Jun 21, 2023
2faa5ab
Merge branch 'main' into unit
Jun 23, 2023
2cfde4e
Adding more VS Copilot-driven unit tests for TV.
Jun 23, 2023
c0a38e6
More unit tests
Jun 23, 2023
7730748
Merge branch 'main' into torchvision
Jun 24, 2023
dec12cf
Merge branch 'main' into unit
Jun 24, 2023
a503fd2
Merge branch 'main' into torchvision
Jun 25, 2023
0d6b180
Merge branch 'main' into unit
Jun 25, 2023
2c61850
Merge branch 'main' into torchvision
NiklasGustafsson Jun 27, 2023
e8e47d6
Merge branch 'main' into unit
NiklasGustafsson Jun 27, 2023
e86cd12
Merge branch 'main' into torchvision
NiklasGustafsson Jul 12, 2023
162d613
Merge branch 'main' into unit
NiklasGustafsson Jul 12, 2023
25ab128
Merge branch 'main' into torchvision
NiklasGustafsson Jul 14, 2023
25b20ea
Merge branch 'main' into unit
NiklasGustafsson Jul 14, 2023
6e6d48e
Merge branch 'main' into torchvision
NiklasGustafsson Jul 17, 2023
55e96d7
Merge branch 'main' into unit
NiklasGustafsson Jul 17, 2023
5ed9c20
Merge branch 'main' into torchvision
NiklasGustafsson Jul 20, 2023
5906909
Merge branch 'main' into unit
NiklasGustafsson Jul 20, 2023
8828f4b
Merge branch 'main' into torchvision
NiklasGustafsson Aug 8, 2023
1a8263b
Merge branch 'main' into unit
NiklasGustafsson Aug 8, 2023
f95fb9a
Merge branch 'main' into torchvision
NiklasGustafsson Aug 9, 2023
db20fcd
Merge branch 'main' into unit
NiklasGustafsson Aug 9, 2023
9ad026f
Merge branch 'main' into torchvision
NiklasGustafsson Aug 9, 2023
93c1efc
Merge branch 'main' into unit
NiklasGustafsson Aug 9, 2023
3d73426
Manual merges
NiklasGustafsson Aug 9, 2023
e6c5b0d
Manual merge
NiklasGustafsson Aug 9, 2023
889a906
Merge branch 'main' into unit
NiklasGustafsson Aug 9, 2023
7301f04
Merge branch 'main' into torchvision
NiklasGustafsson Aug 10, 2023
b5c8ae7
Merge branch 'main' into unit
NiklasGustafsson Aug 10, 2023
7c8d73a
Merge branch 'main' into torchvision
NiklasGustafsson Aug 10, 2023
fecb310
Merge branch 'main' into unit
NiklasGustafsson Aug 10, 2023
58aa38a
Merge branch 'main' into torchvision
NiklasGustafsson Aug 10, 2023
678ed3b
Merge branch 'main' into unit
NiklasGustafsson Aug 10, 2023
6ca94fa
Merge branch 'main' into torchvision
NiklasGustafsson Aug 11, 2023
1abc324
Merge branch 'main' into unit
NiklasGustafsson Aug 11, 2023
4d6515e
Merge branch 'main' into torchvision
NiklasGustafsson Aug 14, 2023
8afc8d6
Merge branch 'main' into unit
NiklasGustafsson Aug 14, 2023
99a69a9
Merge branch 'main' into torchvision
NiklasGustafsson Aug 15, 2023
e17eb31
Merge branch 'main' into unit
NiklasGustafsson Aug 15, 2023
a8e1d90
Merge branch 'main' into torchvision
NiklasGustafsson Aug 15, 2023
53be92f
Merge branch 'main' into unit
NiklasGustafsson Aug 15, 2023
bf39a68
Merge branch 'main' into torchvision
NiklasGustafsson Aug 16, 2023
a341978
Merge branch 'main' into unit
NiklasGustafsson Aug 16, 2023
f81e4a6
Merge branch 'main' into torchvision
NiklasGustafsson Aug 17, 2023
53a20bb
Merge branch 'main' into unit
NiklasGustafsson Aug 17, 2023
5d6eab3
Merge branch 'main' into torchvision
NiklasGustafsson Aug 29, 2023
f4c688e
Merge branch 'main' into unit
NiklasGustafsson Aug 29, 2023
df02776
Merge branch 'main' into torchvision
NiklasGustafsson Aug 29, 2023
2fd482d
Merge branch 'main' into unit
NiklasGustafsson Aug 29, 2023
b4054f0
Merge branch 'main' into torchvision
NiklasGustafsson Aug 31, 2023
b2c69ed
Merge branch 'main' into unit
NiklasGustafsson Aug 31, 2023
472c56f
Merge branch 'main' into torchvision
NiklasGustafsson Sep 7, 2023
b91dd85
Merge branch 'main' into unit
NiklasGustafsson Sep 7, 2023
9244d9e
Merge branch 'main' into torchvision
NiklasGustafsson Sep 8, 2023
c3fe4e6
Merge branch 'main' into unit
NiklasGustafsson Sep 8, 2023
88bab20
Merge branch 'main' into torchvision
NiklasGustafsson Sep 21, 2023
89286f4
Merge branch 'main' into unit
NiklasGustafsson Sep 21, 2023
3971f0b
Merge branch 'main' into torchvision
NiklasGustafsson Oct 9, 2023
d50b4e9
Merge branch 'main' into unit
NiklasGustafsson Oct 9, 2023
6cf95c3
Merge branch 'main' into torchvision
NiklasGustafsson Oct 17, 2023
375202a
Merge branch 'main' into unit
NiklasGustafsson Oct 17, 2023
a632356
Merge branch 'main' into torchvision
NiklasGustafsson Oct 19, 2023
153e279
Merge branch 'main' into unit
NiklasGustafsson Oct 19, 2023
439b79c
Merge branch 'main' into torchvision
NiklasGustafsson Oct 20, 2023
c9b446a
Merge branch 'main' into unit
NiklasGustafsson Oct 20, 2023
e13e3ec
Merge branch 'main' into torchvision
NiklasGustafsson Oct 23, 2023
1a87010
Manual merge
NiklasGustafsson Oct 23, 2023
7c63275
Merge branch 'main' into torchvision
NiklasGustafsson Oct 23, 2023
5c2e771
Merge branch 'main' into unit
NiklasGustafsson Oct 23, 2023
0d622f7
Merge branch 'main' into torchvision
NiklasGustafsson Oct 23, 2023
4081615
Merge branch 'main' into unit
NiklasGustafsson Oct 23, 2023
9bcb3d6
Merge branch 'main' into torchvision
NiklasGustafsson Oct 23, 2023
9c741c8
Merge branch 'main' into unit
NiklasGustafsson Oct 23, 2023
69ae1a0
Merge branch 'main' into torchvision
NiklasGustafsson Oct 24, 2023
26eff10
Merge branch 'main' into unit
NiklasGustafsson Oct 24, 2023
e58ed85
Merge branch 'main' into torchvision
NiklasGustafsson Nov 2, 2023
d5d7a25
Merge branch 'main' into unit
NiklasGustafsson Nov 2, 2023
fc85494
Merge branch 'main' into torchvision
NiklasGustafsson Nov 2, 2023
511b544
Merge branch 'main' into unit
NiklasGustafsson Nov 2, 2023
eeeb69a
Merge branch 'main' into torchvision
NiklasGustafsson Nov 6, 2023
d9e1d81
Merge branch 'main' into unit
NiklasGustafsson Nov 6, 2023
5491976
Merge branch 'main' into torchvision
NiklasGustafsson Nov 9, 2023
1bf6120
Merge branch 'main' into unit
NiklasGustafsson Nov 9, 2023
fd5aefa
Merge branch 'main' into torchvision
NiklasGustafsson Nov 10, 2023
0fd9729
Merge branch 'main' into unit
NiklasGustafsson Nov 10, 2023
cd841d2
Merge branch 'main' into torchvision
NiklasGustafsson Nov 21, 2023
272eb24
Merge branch 'main' into unit
NiklasGustafsson Nov 21, 2023
1db7664
Merge branch 'main' into torchvision
NiklasGustafsson Nov 27, 2023
6eb34ca
Merge branch 'main' into unit
NiklasGustafsson Nov 27, 2023
38711cb
Merge branch 'main' into torchvision
NiklasGustafsson Nov 27, 2023
ef57e9d
Merge branch 'main' into unit
NiklasGustafsson Nov 27, 2023
66a6115
Merge branch 'main' into torchvision
NiklasGustafsson Nov 28, 2023
cd94263
Merge branch 'main' into unit
NiklasGustafsson Nov 28, 2023
677ae9e
Merge branch 'main' into torchvision
NiklasGustafsson Nov 29, 2023
1c36534
Merge branch 'main' into unit
NiklasGustafsson Nov 29, 2023
b696cb9
Merge branch 'main' into torchvision
NiklasGustafsson Nov 29, 2023
7b6c5fe
Merge branch 'main' into unit
NiklasGustafsson Nov 29, 2023
8163e9e
Merge branch 'main' into torchvision
NiklasGustafsson Dec 1, 2023
a496721
Merge branch 'main' into unit
NiklasGustafsson Dec 1, 2023
4136825
Merge branch 'main' into torchvision
NiklasGustafsson Dec 1, 2023
2567bc4
Manual merge
NiklasGustafsson Dec 1, 2023
a05d94d
Merge branch 'main' into torchvision
NiklasGustafsson Dec 1, 2023
fdaa7a9
Merge branch 'main' into unit
NiklasGustafsson Dec 1, 2023
5f76d78
Merge branch 'main' into torchvision
NiklasGustafsson Dec 4, 2023
11ad7f2
Merge branch 'main' into unit
NiklasGustafsson Dec 4, 2023
78943f3
Merge branch 'main' into torchvision
NiklasGustafsson Dec 7, 2023
6c10bc6
Merge branch 'main' into unit
NiklasGustafsson Dec 7, 2023
4b0c61c
Merge branch 'main' into torchvision
NiklasGustafsson Dec 7, 2023
0e7e33a
Merge branch 'main' into unit
NiklasGustafsson Dec 7, 2023
363b63f
Merge branch 'main' into torchvision
NiklasGustafsson Dec 11, 2023
9ff7231
Merge branch 'main' into unit
NiklasGustafsson Dec 11, 2023
ed09b63
Merge branch 'main' into torchvision
NiklasGustafsson Dec 12, 2023
4e8fe39
Merge branch 'main' into unit
NiklasGustafsson Dec 12, 2023
79b4542
Merge branch 'main' into torchvision
NiklasGustafsson Dec 12, 2023
658a11a
Merge branch 'main' into unit
NiklasGustafsson Dec 12, 2023
08335b1
Merge branch 'main' into torchvision
NiklasGustafsson Dec 20, 2023
59b4304
Merge branch 'main' into unit
NiklasGustafsson Dec 20, 2023
e67c7c1
Merge branch 'main' into torchvision
NiklasGustafsson Jan 19, 2024
bcfe3da
Merge branch 'main' into unit
NiklasGustafsson Jan 19, 2024
a2dc9cb
Merge branch 'main' into torchvision
NiklasGustafsson Jan 26, 2024
111ff89
Merge branch 'main' into unit
NiklasGustafsson Jan 26, 2024
86ffcfd
Stashing my changes.
NiklasGustafsson Feb 2, 2024
22c7e5f
Merge branch 'main' into torchvision
NiklasGustafsson Feb 2, 2024
f5cac11
Merge branch 'main' into unit
NiklasGustafsson Feb 2, 2024
f5eacb6
Merge branch 'main' into torchvision
NiklasGustafsson Feb 7, 2024
42fe2f4
Merge branch 'main' into unit
NiklasGustafsson Feb 7, 2024
6c30fdc
Merge branch 'main' into torchvision
NiklasGustafsson Feb 7, 2024
fccd005
Merge branch 'main' into unit
NiklasGustafsson Feb 7, 2024
8eb3315
Merge branch 'main' into torchvision
NiklasGustafsson Feb 9, 2024
7dec447
Merge branch 'main' into unit
NiklasGustafsson Feb 9, 2024
a6e94cd
Merge branch 'main' into torchvision
NiklasGustafsson Feb 14, 2024
ce76e55
Merge branch 'main' into unit
NiklasGustafsson Feb 14, 2024
431b816
Merge branch 'main' into torchvision
NiklasGustafsson Feb 22, 2024
8392b2f
Merge branch 'main' into unit
NiklasGustafsson Feb 22, 2024
47624e8
Merge branch 'torchvision' of https://github.com/NiklasGustafsson/Tor…
Feb 28, 2024
26c4360
Merge branch 'unit' of https://github.com/NiklasGustafsson/TorchSharp…
Feb 28, 2024
6fafdbd
Added no grad in loadstatedict
shaltielshmid Feb 27, 2024
0742fb6
typo
shaltielshmid Feb 27, 2024
72b1261
Added release notes
shaltielshmid Feb 28, 2024
c5bd494
Updated release notes for 0.102.0
shaltielshmid Feb 28, 2024
e2ca473
Fixed average pool 1d + npstr/cstr with one dimension
shaltielshmid Mar 2, 2024
428a66a
Added release notes
shaltielshmid Mar 2, 2024
8c37c5c
Renamed kernel_size
shaltielshmid Mar 4, 2024
c9ec3b2
Fixed issue with read and added test + release notes
shaltielshmid Feb 29, 2024
d5d1fd0
Added check for end of stream mid tensor
shaltielshmid Feb 29, 2024
4fb25b6
Added check for net 6
shaltielshmid Mar 4, 2024
9191f9b
Update TestTorchTensorBugs.cs
NiklasGustafsson Mar 4, 2024
54ee83b
Address issue with ordering of initializations in the torch cctor.
NiklasGustafsson Mar 5, 2024
06675ec
Update BranchInfo.props
NiklasGustafsson Mar 5, 2024
88bd908
Update version number
NiklasGustafsson Mar 5, 2024
1d6a76f
Merge branch 'main' into unit
NiklasGustafsson Mar 5, 2024
89f53c2
Finished merging a file that was missed.
NiklasGustafsson Mar 5, 2024
c6d80f9
Merge branch 'main' into unit
NiklasGustafsson Mar 5, 2024
6f23a5d
Remove duplicate test
NiklasGustafsson Mar 5, 2024
c5470fe
Added properties to a number of builtin modules.
NiklasGustafsson Mar 5, 2024
6c0f2a2
Introduced 'ParamLessModule' as the base class on more modules.
NiklasGustafsson Mar 5, 2024
4cb57e1
Added properties to a number of modules.
NiklasGustafsson Mar 5, 2024
34405c6
Removed unused Bilinear native APIs.
NiklasGustafsson Mar 5, 2024
f246c11
Added Component name attribute to some classes.
NiklasGustafsson Mar 5, 2024
1192d8e
Merge branch 'main' into unit
Mar 11, 2024
cff452a
Reviewed all modified modules and continued, made adjustments and con…
shaltielshmid Mar 12, 2024
5bcab46
Revert "Reviewed all modified modules and continued, made adjustments…
NiklasGustafsson Mar 12, 2024
66cab20
Reopening PR after testing on Mac (#18)
shaltielshmid Mar 13, 2024
e21aacf
Moved all activation modules to managed code.
NiklasGustafsson Mar 13, 2024
640b7bf
Added check of 'disposing' in one place.
NiklasGustafsson Mar 13, 2024
6c93429
Address PR comments.
NiklasGustafsson Mar 14, 2024
0ef818b
More PR adjustments
NiklasGustafsson Mar 14, 2024
d4c0482
Moved GroupNorm to C#
NiklasGustafsson Mar 14, 2024
4ba0a92
Implement BatchNorm and InstanceNorm modules in terms of the function…
NiklasGustafsson Mar 14, 2024
39fce55
Merge branch 'main' into unit
NiklasGustafsson Mar 15, 2024
ff0ffbc
Merge branch 'main' into unit
NiklasGustafsson Mar 15, 2024
9f61b15
Merge branch 'main' into unit
NiklasGustafsson Mar 15, 2024
ed3ecd3
Address PR comments and a unit test failure.
NiklasGustafsson Mar 19, 2024
b24ef9a
Fixed unit test error and PRELU implementation.
NiklasGustafsson Mar 19, 2024
25e128c
Added some missing test files to the CUDA test project.
NiklasGustafsson Mar 21, 2024
254470e
Overrides on ParamLessModule
NiklasGustafsson Mar 22, 2024
09598b9
Merge branch 'main' into unit
NiklasGustafsson Mar 22, 2024
72356a4
Manual merge
NiklasGustafsson Mar 26, 2024
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
60 changes: 57 additions & 3 deletions src/TorchSharp/NN/ParamLessModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ namespace TorchSharp

namespace Modules
{
public interface IParameterLessModule {

}
/// <summary>
/// Base class for all modules that do not have any tensor parameters or buffers, and
/// for which the `_to()` implementation can therefore be simplified.
/// </summary>
public abstract class ParamLessModule<T1, T2> : nn.Module<T1, T2>
public abstract class ParamLessModule<T1, T2> : nn.Module<T1, T2>, IParameterLessModule
{
protected ParamLessModule(string name) : base(name) { }

Expand All @@ -26,13 +29,30 @@ protected ParamLessModule(IntPtr handle, IntPtr boxedHandle) : base(handle, boxe
protected internal override nn.Module _to(DeviceType deviceType, int deviceIndex = -1) => this;

protected internal override nn.Module _to(ScalarType dtype) => this;

public override void register_buffer(string name, Tensor tensor, bool persistent = true)
{
throw new InvalidOperationException($"Cannot register a buffer on a module that is declared 'parameter-less.'");
}

public override void register_parameter(string name, Parameter param)
{
throw new InvalidOperationException($"Cannot register a parameter on a module that is declared 'parameter-less.'");
}

public override void register_module(string name, nn.Module submodule)
{
if (submodule is not IParameterLessModule)
throw new InvalidOperationException($"Submodules of a parameter-less module must also be parameter-less.");
base.register_module(name, submodule);
}
}

/// <summary>
/// Base class for all modules that do not have any tensor parameters or buffers, and
/// for which the `_to()` implementation can therefore be simplified.
/// </summary>
public abstract class ParamLessModule<T1, T2, T3> : nn.Module<T1, T2, T3>
public abstract class ParamLessModule<T1, T2, T3> : nn.Module<T1, T2, T3>, IParameterLessModule
{
protected ParamLessModule(string name) : base(name) { }

Expand All @@ -45,13 +65,30 @@ protected ParamLessModule(IntPtr handle, IntPtr boxedHandle) : base(handle, boxe
protected internal override nn.Module _to(DeviceType deviceType, int deviceIndex = -1) => this;

protected internal override nn.Module _to(ScalarType dtype) => this;

public override void register_buffer(string name, Tensor tensor, bool persistent = true)
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps it is worth adding sealed to the override?

{
throw new InvalidOperationException($"Cannot register a buffer on a module that is declared 'parameter-less.'");
}

public override void register_parameter(string name, Parameter param)
{
throw new InvalidOperationException($"Cannot register a parameter on a module that is declared 'parameter-less.'");
}

public override void register_module(string name, nn.Module submodule)
{
if (submodule is not IParameterLessModule)
throw new InvalidOperationException($"Submodules of a parameter-less module must also be parameter-less.");
base.register_module(name, submodule);
}
}

/// <summary>
/// Base class for all modules that do not have any tensor parameters or buffers, and
/// for which the `_to()` implementation can therefore be simplified.
/// </summary>
public abstract class ParamLessModule<T1, T2, T3, T4> : nn.Module<T1, T2, T3, T4>
public abstract class ParamLessModule<T1, T2, T3, T4> : nn.Module<T1, T2, T3, T4>, IParameterLessModule
{
protected ParamLessModule(string name) : base(name) { }

Expand All @@ -64,6 +101,23 @@ protected ParamLessModule(IntPtr handle, IntPtr boxedHandle) : base(handle, boxe
protected internal override nn.Module _to(DeviceType deviceType, int deviceIndex = -1) => this;

protected internal override nn.Module _to(ScalarType dtype) => this;

public override void register_buffer(string name, Tensor tensor, bool persistent = true)
{
throw new InvalidOperationException($"Cannot register a buffer on a module that is declared 'parameter-less.'");
}

public override void register_parameter(string name, Parameter param)
{
throw new InvalidOperationException($"Cannot register a parameter on a module that is declared 'parameter-less.'");
}

public override void register_module(string name, nn.Module submodule)
{
if (submodule is not IParameterLessModule)
throw new InvalidOperationException($"Submodules of a parameter-less module must also be parameter-less.");
base.register_module(name, submodule);
}
}
}
}
32 changes: 32 additions & 0 deletions test/TorchSharpTest/NN.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6633,5 +6633,37 @@ public void TestModulePostHooks()
lin1.call(input);
Assert.Equal(1, counter);
}

[Fact]
public void TestCustomParameterLessModule()
{
var cnp = new CustomNoParameters("test");

// Should not throw
cnp.register_module("sub", new CustomNoParameters("test"));

Assert.True(cnp.named_modules().Count() > 0);
Assert.Equal("sub", cnp.named_modules().First().name);

Assert.Throws<InvalidOperationException>(() => cnp.register_module("test", torch.nn.Linear(10,10, true)));
Assert.Throws<InvalidOperationException>(() => cnp.register_buffer("test", torch.rand(10)));
Assert.Throws<InvalidOperationException>(() => cnp.register_parameter("test", new Parameter(torch.rand(10))));
}

class CustomNoParameters : ParamLessModule<Tensor, Tensor>
{
public CustomNoParameters(string name) : base(name)
{
}

public CustomNoParameters(IntPtr handle, IntPtr boxedHandle) : base(handle, boxedHandle)
{
}

public override Tensor forward(Tensor input)
{
throw new NotImplementedException();
}
}
}
}