Error when i create new model and split model

I need to process and refine the model for many different purposes, one automatically (split, concat, …) both in terms of model and weight. I can use some models (VGG, EffciennetB3, Xception, ViT), however in some variants of ViT (CrossViT, CrossFormer) I get error messages, still can’t find a fix.

import torch
from vit_pytorch.cross_vit import CrossViT
import torch.nn as nn
v = CrossViT(
image_size = 256,
num_classes = 1000,
depth = 4, # number of multi-scale encoding blocks
sm_dim = 192, # high res dimension
sm_patch_size = 16, # high res patch size (should be smaller than lg_patch_size)
sm_enc_depth = 2, # high res depth
sm_enc_heads = 8, # high res heads
sm_enc_mlp_dim = 2048, # high res feedforward dimension
lg_dim = 384, # low res dimension
lg_patch_size = 64, # low res patch size
lg_enc_depth = 3, # low res depth
lg_enc_heads = 8, # low res heads
lg_enc_mlp_dim = 2048, # low res feedforward dimensions
cross_attn_depth = 2, # cross attention rounds
cross_attn_heads = 8, # cross attention heads
dropout = 0.1,
emb_dropout = 0.1
)

v = nn.Sequential(*list(v.children()))
v1 = nn.Sequential(*list(v.children()[:5]))

img = torch.randn(1, 3, 256, 256)

pred = v(img) # (1, 1000)
print(pred)

“einops.EinopsError: Expected 4 dimensions, got 3”

from vit_pytorch.crossformer import CrossFormer

model = CrossFormer(
num_classes = 1000, # number of output classes
dim = (64, 128, 256, 512), # dimension at each stage
depth = (2, 2, 8, 2), # depth of transformer at each stage
global_window_size = (8, 4, 2, 1), # global window sizes at each stage
local_window_size = 7, # local window size (can be customized for each stage, but in paper, held constant at 7 for all stages)
)

img = torch.randn(1, 3, 224, 224)
model = nn.Sequential(*list(model.children()))
pred = model(img) # (1, 1000)

“NotImplementedError: Module [ModuleList] is missing the required “forward” function”

I don’t know where this error is raised from:

but the error message is pointing towards a dimension mismatch so you would need to check which tensors are used and that they have the expected number of dimensions.

This error is raise if you try to “call” an nn.ModuleList object like a module, which won’t work as you would need to iterate it.
Here is a small example:

modules = nn.ModuleList([
    nn.Linear(10, 10),
    nn.Linear(10, 10),
    nn.Linear(10, 10),
    nn.Linear(10, 10),
    nn.Linear(10, 10),
])

x = torch.randn(1, 10)

# breaks
out = modules(x)
# NotImplementedError: Module [ModuleList] is missing the required "forward" function

# works
for module in modules:
    x = module(x)

Please I have a similar issue with my script here:
from IPython.core.completerlib import module_list
from numpy.core.fromnumeric import shape
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.modules.container import ModuleList, Sequential
from methods.utils.model_utilities_orth import ( CustomCNN,PositionalEncoding, init_layer) #( DoubleConv,PositionalEncoding, init_layer)
from methods.utils.cbam import CBAM
from performer_stuffs.slim_performer_model import SLiMPerformerLayer

#import performer.models.slim_performer.pytorch.numerator_and_denominator as num_and_den
from performer_stuffs import numerator_and_denominator as num_and_den

class EINV2(nn.Module):
def init(self, cfg, dataset):
super().init()
self.pe_enable = False # Ture | False

    if cfg['data']['audio_feature'] == 'logmel&intensity':
        self.f_bins = cfg['data']['n_mels']
        self.in_channels = 7
        n_layers = 2
        feature_type = 'logmel&intensity'
    self.downsample_ratio = 2 ** 2
    self.sed_conv_block1 = nn.Sequential(
    CustomCNN(in_channels=4, out_channels=64, kernel_size = 3),
       
    )
    # self.cbam_sed1 = CBAM(gate_channels =64) 
    nn.AvgPool2d(kernel_size=(2, 2))
    
    self.sed_conv_block2 = nn.Sequential(
       CustomCNN(in_channels=64, out_channels=128, kernel_size = 3),
        nn.AvgPool2d(kernel_size=(2, 2)),
    )
    # self.cbam_sed2 = CBAM(gate_channels =128) 
    # nn.AvgPool2d(kernel_size=(2, 2)),
    
    self.sed_conv_block3 = nn.Sequential(
    CustomCNN(in_channels=128, out_channels=256, kernel_size = 3),
        nn.AvgPool2d(kernel_size=(1, 2)),
    )
    # self.cbam_sed3 = CBAM(gate_channels =256) 
    # nn.AvgPool2d(kernel_size=(2, 2))
    self.sed_conv_block4 = nn.Sequential(
      CustomCNN(in_channels=256, out_channels=512, kernel_size = 3),
        nn.AvgPool2d(kernel_size=(1, 2)),
    )
    # self.cbam_sed4 = CBAM(gate_channels =512) 
    # nn.AvgPool2d(kernel_size=(2, 2))
    self.doa_conv_block1 = nn.Sequential(
        CustomCNN(in_channels=self.in_channels, out_channels=64, kernel_size = 3),
        nn.AvgPool2d(kernel_size=(2, 2)),
    )
    # self.cbam_doa1 = CBAM(gate_channels =64) 
    # nn.AvgPool2d(kernel_size=(2, 2))
    self.doa_conv_block2 = nn.Sequential(
     CustomCNN(in_channels=64, out_channels=128, kernel_size = 3),
        nn.AvgPool2d(kernel_size=(2, 2)),
    )
    # self.cbam_doa2 = CBAM(gate_channels =128) 
    nn.AvgPool2d(kernel_size=(2, 2))
    self.doa_conv_block3 = nn.Sequential(
      CustomCNN(in_channels=128, out_channels=256, kernel_size = 3),
        nn.AvgPool2d(kernel_size=(1, 2)),
    )
   
    self.doa_conv_block4 = nn.Sequential(
       CustomCNN(in_channels=256, out_channels=512, kernel_size = 3),
        nn.AvgPool2d(kernel_size=(1, 2)),
    )
   
    self.stitch = nn.ParameterList([
        nn.Parameter(torch.FloatTensor(64, 2, 2).uniform_(0.1, 0.9)),
        nn.Parameter(torch.FloatTensor(128, 2, 2).uniform_(0.1, 0.9)),
        nn.Parameter(torch.FloatTensor(256, 2, 2).uniform_(0.1, 0.9)),
    ])
    if self.pe_enable:
        self.pe = PositionalEncoding(pos_len=100, d_model=512, pe_type='t', dropout=0.0)
    self.sed_trans_track1 = torch.nn.ModuleList([
    SLiMPerformerLayer(hidden_dim = 512, ffn_dim= 2048, n_heads=8, feature_type = feature_type,
                       compute_type =iter, on_gptln =True) for _ in range(n_layers)]

)  
    self.sed_trans_track2 = torch.nn.ModuleList([
    SLiMPerformerLayer(hidden_dim = 512, ffn_dim= 2048, n_heads=8, feature_type = feature_type,
                       compute_type =iter, on_gptln =True) for _ in range(n_layers)]
    )
    self.doa_trans_track1 = torch.nn.ModuleList([
    SLiMPerformerLayer(hidden_dim = 512, ffn_dim= 2048, n_heads=8, feature_type = feature_type,
                       compute_type =iter, on_gptln =True) for _ in range(n_layers)]
) 
    
    self.doa_trans_track2 = torch.nn.ModuleList([
    SLiMPerformerLayer(hidden_dim = 512, ffn_dim= 2048, n_heads=8, feature_type = feature_type,
                       compute_type =iter, on_gptln =True) for _ in range(n_layers)]

)

    self.fc_sed_track1 = nn.Linear(512, 14, bias=True)
    self.fc_sed_track2 = nn.Linear(512, 14, bias=True)
    self.fc_doa_track1 = nn.Linear(512, 3, bias=True)
    self.fc_doa_track2 = nn.Linear(512, 3, bias=True)
    self.final_act_sed = nn.Sequential() # nn.Sigmoid()
    self.final_act_doa = nn.Tanh()

    self.init_weight()

def init_weight(self):

    init_layer(self.fc_sed_track1)
    init_layer(self.fc_sed_track2)
    init_layer(self.fc_doa_track1)
    init_layer(self.fc_doa_track2)

def forward(self, x):
    """
    x: waveform, (batch_size, num_channels, data_length)
    """
    x_sed = x[:, :4]
    x_doa = x

    # cnn
    x_sed = self.sed_conv_block1(x_sed)
    x_doa = self.doa_conv_block1(x_doa)
    

    x_sed = self.sed_conv_block4(x_sed)
    x_doa = self.doa_conv_block4(x_doa)
    x_sed = x_sed.mean(dim=3) # (N, C, T)
    x_doa = x_doa.mean(dim=3) # (N, C, T)

    #transformer
    # if self.pe_enable:
    #     x_sed = self.pe(x_sed)
    # if self.pe_enable:
    #     x_doa = self.pe(x_doa)
    x_sed = self.sed_trans_track1(x_sed)
    x_sed = self.sed_trans_track2(x_sed)
    
    x_sed = self.doa_trans_track1(x_doa)
    x_sed = self.doa_trans_track1(x_doa)
    x_sed = x_sed.permute(2, 0, 1) # (T, N, C)
    x_doa = x_doa.permute(2, 0, 1) # (T, N, C)

    x_sed_1 = self.sed_trans_track1(x_sed).transpose(0, 1) # (N, T, C)
    x_sed_2 = self.sed_trans_track2(x_sed).transpose(0, 1) # (N, T, C)   
    x_doa_1 = self.doa_trans_track1(x_doa).transpose(0, 1) # (N, T, C)
    x_doa_2 = self.doa_trans_track2(x_doa).transpose(0, 1) # (N, T, C)

    
    x_sed_1 = self.final_act_sed(self.fc_sed_track1(x_sed_1))
    x_sed_2 = self.final_act_sed(self.fc_sed_track2(x_sed_2))
    x_sed = torch.stack((x_sed_1, x_sed_2), 2)
    x_doa_1 = self.final_act_doa(self.fc_doa_track1(x_doa_1))
    x_doa_2 = self.final_act_doa(self.fc_doa_track2(x_doa_2))
    x_doa = torch.stack((x_doa_1, x_doa_2), 2)
    output = {
        'sed': x_sed,
        'doa': x_doa,
    }

    return output

raise NotImplementedError(f"Module [{type(self).name}] is missing the required "forward" function")
NotImplementedError: Module [ModuleList] is missing the required “forward” function

NotImplementedError: Module [ModuleList] is missing the required “forward” function

You cannot call the nn.ModuleList directly as it’s not implementing a forward method and you thus need to iterate each module separately in a loop. If you want to execute the internal modules sequentially, use nn.Sequential instead.

Could you provide me with an example of how to implement this using my script? Thanks

In the forward method you would need to replace the usage of each nn.ModuleList from:

x_sed = self.sed_trans_track1(x_sed)

to

for module in self.sed_trans_track1:
    x_sed = module(x_sed)

Thanks , but the error still persists after I have replaced the implementation of the nn. ModuleList in the forward method like this;
for module in self.sed_trans_track1:
x_sed = module(x_sed)
for module in self.sed_trans_track2:
x_doa = module(x_sed)
for module in self.doa_trans_track1:
x_sed = module(x_sed)
for module in self.doa_trans_track2:
x_doa = module(x_sed)
x_sed = x_sed.permute(2, 0, 1) # (T, N, C)
x_doa = x_doa.permute(2, 0, 1) # (T, N, C)

    x_sed_1 = self.sed_trans_track1(x_sed).transpose(0, 1) # (N, T, C)
    x_sed_2 = self.sed_trans_track2(x_sed).transpose(0, 1) # (N, T, C)   
    x_doa_1 = self.doa_trans_track1(x_doa).transpose(0, 1) # (N, T, C)
    x_doa_2 = self.doa_trans_track2(x_doa).transpose(0, 1) # (N, T, C)
    x_sed_1 = self.final_act_sed(self.fc_sed_track1(x_sed_1))
    x_sed_2 = self.final_act_sed(self.fc_sed_track2(x_sed_2))
    x_sed = torch.stack((x_sed_1, x_sed_2), 2)
    x_doa_1 = self.final_act_doa(self.fc_doa_track1(x_doa_1))
    x_doa_2 = self.final_act_doa(self.fc_doa_track2(x_doa_2))
    x_doa = torch.stack((x_doa_1, x_doa_2), 2)
    output = {
        'sed': x_sed,
        'doa': x_doa,
    }

    return output

I have four tracks in all (2 tracks for sed and doa each) and each track has 2 SLiMperformer layers

This would mean that you are still calling an nn.ModuleList directly:

modules = nn.ModuleList([
    nn.Linear(1, 1),
    nn.ReLU(),
    nn.Linear(1, 1)
])
x = torch.randn(1, 1)

out = modules(x)
# NotImplementedError: Module [ModuleList] is missing the required "forward" function

instead of iterating it:

for module in modules:
    x = module(x)

and the stacktrace should show which line of code fails.

the code fails@
x = module(x)

I am using the SLiMPerformerLayer from 'How to Make Performers SLiM' paper

Could you post the stacktrace including the error message?

0%| | 0/11800 [00:13<?, ?it/s]
Traceback (most recent call last):
File “seld/main.py”, line 59, in
sys.exit(main(args, cfg))
File “seld/main.py”, line 43, in main
train.train(cfg, **train_initializer)
File “/mnt/raid/ni/WALE_SEdl/EIN-SELD/seld/learning/train.py”, line 47, in train
epoch_it=epoch_it
File “/mnt/raid/ni/WALE_SEdl/EIN-SELD/seld/methods/ein_seld/training.py”, line 127, in validate_step
pred = self.model(batch_x)
File “/mnt/antares_raid/home/wale/anaconda3/envs/ein/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 1130, in _call_impl
return forward_call(*input, **kwargs)
File “/mnt/antares_raid/home/wale/anaconda3/envs/ein/lib/python3.7/site-packages/torch/nn/parallel/data_parallel.py”, line 168, in forward
outputs = self.parallel_apply(replicas, inputs, kwargs)
File “/mnt/antares_raid/home/wale/anaconda3/envs/ein/lib/python3.7/site-packages/torch/nn/parallel/data_parallel.py”, line 178, in parallel_apply
return parallel_apply(replicas, inputs, kwargs, self.device_ids[:len(replicas)])
File “/mnt/antares_raid/home/wale/anaconda3/envs/ein/lib/python3.7/site-packages/torch/nn/parallel/parallel_apply.py”, line 86, in parallel_apply
output.reraise()
File “/mnt/antares_raid/home/wale/anaconda3/envs/ein/lib/python3.7/site-packages/torch/_utils.py”, line 461, in reraise
raise exception
NotImplementedError: Caught NotImplementedError in replica 0 on device 0.
Original Traceback (most recent call last):
File “/mnt/antares_raid/home/wale/anaconda3/envs/ein/lib/python3.7/site-packages/torch/nn/parallel/parallel_apply.py”, line 61, in _worker
output = module(*input, **kwargs)
File “/mnt/antares_raid/home/wale/anaconda3/envs/ein/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 1130, in _call_impl
return forward_call(*input, **kwargs)
File “/mnt/raid/ni/WALE_SEdl/EIN-SELD/seld/methods/ein_seld/models/seld.py”, line 173, in forward
x_sed = module(x_sed)
File “/mnt/antares_raid/home/wale/anaconda3/envs/ein/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 1130, in _call_impl
return forward_call(*input, **kwargs)
File “/mnt/antares_raid/home/wale/anaconda3/envs/ein/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 201, in _forward_unimplemented
raise NotImplementedError(f"Module [{type(self).name}] is missing the required "forward" function")
NotImplementedError: Module [SLiMPerformerLayer] is missing the required “forward” function


Thanks

I don’t know which module you are using but EIN-SELD:/seld/methods/ein_seld/models/seld.py does not contain any code beyond line 141, so no idea what exactly is being called there.