Torch.nn.Module always necessary?

Hi everyone, I have a question regarding wrapping an operation/module in a CNN model. What I mostly see in codes is people wrap the modules inside custom classes that inherit from torch.nn.Module class. I am curious if it’s necessary to wrap a module in torch.nn.Module class? or it’s only necessary if we want that custom module to be registered and possibly if it requires gradient?

It depends.
If what you need to implement have a custom forward where it requires some logic (if/else, etc) go the module way.
otherwise If for example all you need to do is to tie together some layers in seccession, (create some blocks like conv, bn, activation , etc) you can use functions as well and return a modulelist, nn.Sequential, etc and use that.