xyshi2000 / SpikingResformer

Codes of the paper: SpikingResformer: Bridging ResNet and Vision Transformer in Spiking Neural Networks (CVPR2024)
MIT License
33 stars 1 forks source link

sOPs的计算 #5

Closed 2ephyrus closed 1 month ago

2ephyrus commented 1 month ago

请问sops的计算是否漏掉了某些步骤?为什么本地运行的结果与表格相差近1000倍?已经明确不是单位的问题。

xyshi2000 commented 1 month ago

请提供日志以及复现的命令

2ephyrus commented 1 month ago

仅在yaml中设置test_only: True。命令与readme当中提供的训练指令保持一致

2ephyrus commented 1 month ago

{NA5KL(KA Q $X7~$(C({U6

xyshi2000 commented 1 month ago

使用test_only选项测试一个仅初始化而未经训练的网络会产生这种结果。这是因为仅初始化而未经训练的网络在推理时大部分神经元不会激活,因此SOPs接近于0。您可以尝试测试一个训练完毕的网络,结果应当与报告的结果一致

2ephyrus commented 1 month ago
  1. 请问如何测试训练完毕的网络,它的运行指令是什么样的?
  2. 另外,使用“n_parameters = sum(p.numel() for p in model.parameters() if p.requires_grad)”计算Param略大于SpikingResformer的计算方法,能否解释它们之间的差异以及应该选择哪种?
  3. 能否结合sops的计算代码解释sops的估算过程?

    
    ops, params = profile(
        model, inputs=(torch.rand(input_size).cuda().unsqueeze(0), ), verbose=False, custom_ops={
            layer.Conv2d: count_convNd,
            Conv3x3: count_convNd,
            Conv1x1: count_convNd,
            Linear: count_linear,
            SpikingMatmul: count_matmul, })[0:2]
    if step_mode == 'm':
        ops, params = (ops / (1000**3)) / args.T, params / (1000**2)
    else:
        ops, params = (ops / (1000**3)), params / (1000**2)
    functional.reset_net(model)
    logger.info('MACs: {:.5f} G, params: {:.2f} M.'.format(ops, params))
    
    sops = 0
    for name in mon.monitored_layers:
        sublist = mon[name]
        sop = torch.cat(sublist).mean().item()
        sops = sops + sop
    sops = sops / (1000**3)
    # input is [N, C, H, W] or [T*N, C, H, W]
    sops = sops / args.batch_size
    if step_mode == 's':
        sops = sops * args.T
    logger.info('Avg SOPs: {:.5f} G, Power: {:.5f} mJ.'.format(sops, 0.9 * sops))
    logger.info('A/S Power Ratio: {:.6f}'.format((4.6 * ops) / (0.9 * sops)))

十分感谢你的回复
xyshi2000 commented 1 month ago

Q1:使用--resume选项指定训练完毕的网络的权重.pth文件 Q2:这可能是因为BN层未被统计,因为事实上训练完毕的权重在实际部署时可以将BN层的参数合并到前面的卷积/线性层,因此可以忽略这一部分参数 Q3:这里 1 SOP 定义为1个突触前神经元发放的1个脉冲经过1条突触连接传递到1个突触后神经元发生的计算。具体实现可以参见SOPMonitor类。更进一步的解释可以参见我的另一篇文章Towards Energy Efficient Spiking Neural Networks: An Unstructured Pruning Framework和仓库,SOPs统计代码与这里的一致。

2ephyrus commented 1 month ago

十分感谢你的回复!