Giter Club home page Giter Club logo

shiftaddnet's Introduction

ShiftAddNet

License: MIT

This is a PyTorch implementation of ShiftAddNet: A Hardware-Inspired Deep Network published on the NeurIPS 2020


Prerequisite

  • GCC >= 5.4.0
  • PyTorch == 1.4
  • Other common library are included in requirements.txt

Compile Adder Cuda Kernal

The original AdderNet Repo considers using PyTorch for implementing add absed convolution, however it remains slow and requires much more runtime memory costs as compared to the variant with CUDA acceleration.

We here provide one kind of CUDA implementation, please follow the intruction below to compile and check that the forwad/backward results are consistent with the original version.

Step 1: modify PyTorch before launch (for solving compiling issue)

Change lines:57-64 in anaconda3/lib/python3.7/site-packages/torch/include/THC/THCTensor.hpp from:

#include <THC/generic/THCTensor.hpp>
#include <THC/THCGenerateAllTypes.h>

#include <THC/generic/THCTensor.hpp>
#include <THC/THCGenerateBoolType.h>

#include <THC/generic/THCTensor.hpp>
#include <THC/THCGenerateBFloat16Type.h>

to:

#include <THC/generic/THCTensor.h>
#include <THC/THCGenerateAllTypes.h>

#include <THC/generic/THCTensor.h>
#include <THC/THCGenerateBoolType.h>

#include <THC/generic/THCTensor.h>
#include <THC/THCGenerateBFloat16Type.h>

Step 2: launch command to make sure you can successfully compile

python check.py

You should be able to successfully compile and see the runtime speed comparisons in the toy cases.

Reproduce Results in Paper

We release the pretrained checkpoints in Google Drive. To evaluate the inference accuracy of test set, we provide evaluation scripts shown below for your convenience. If you want to train your own model, the only change should be removing --eval_only option in the commands.

  • Examples for training of AdderNet
# CIFAR-10
    bash ./scripts/addernet/cifar10/FP32.sh
    bash ./scripts/addernet/cifar10/FIX8.sh

# CIFAR-100
    bash ./scripts/addernet/cifar100/FP32.sh
    bash ./scripts/addernet/cifar100/FIX8.sh
  • Examples for training of DeepShift
# CIFAR-10
    bash ./scripts/deepshift/cifar10.sh

# CIFAR-100
    bash ./scripts/deepshift/cifar100.sh
  • Examples for training of ShiftAddNet
# CIFAR-10
    bash ./scripts/shiftaddnet/cifar10/FP32.sh
    bash ./scripts/shiftaddnet/cifar10/FIX8.sh

# CIFAR-100
    bash ./scripts/shiftaddnet/cifar100/FP32.sh
    bash ./scripts/shiftaddnet/cifar100/FIX8.sh
  • Examples for training of ShiftAddNet (Fixed shift variant)
# CIFAR-10
    bash ./scripts/shiftaddnet_fix/cifar10/FP32.sh
    bash ./scripts/shiftaddnet_fix/cifar10/FIX8.sh

# CIFAR-100
    bash ./scripts/shiftaddnet_fix/cifar100/FP32.sh
    bash ./scripts/shiftaddnet_fix/cifar100/FIX8.sh

ShiftAddNet on IoT

Please refer to ./IoT directory for detailed description.

T-SNE Visualization

Reproduce the T-SNE visualization of the class divergences in AdderNet, and the proposed ShiftAddNet, using ResNet-20 on CIFAR-10 as an example.

bash ./scripts/gen_feat.sh # generate the features that will be used for visualization

cd tsne_vis &&
python visual_tsne.py --save_dir resnet20_add_FP32 --scratch
python visual_tsne.py --save_dir resnet20_add_FIX8 --scratch
python visual_tsne.py --save_dir resnet20_shiftadd_FP32 --scratch
python visual_tsne.py --save_dir resnet20_shiftadd_FIX8 --scratch

python visual_tsne.py --save_dir resnet20_add_FP32 --scratch --dim_3d
python visual_tsne.py --save_dir resnet20_add_FIX8 --scratch --dim_3d
python visual_tsne.py --save_dir resnet20_shiftadd_FP32 --scratch --dim_3d
python visual_tsne.py --save_dir resnet20_shiftadd_FIX8 --scratch --dim_3d

The output figure should look like below:

Citation

If you find this codebase is useful for your research, please cite:

@inproceedings{ShiftAddNet,
title={ShiftAddNet: A Hardware-Inspired Deep Network},
author={Haoran You, Xiaohan Chen, Yongan Zhang, Chaojian Li, Sicheng Li, Zihao Liu, Zhangyang Wang, Yingyan Lin},
booktitle={Thirty-fourth Conference on Neural Information Processing Systems},
year={2020},
}

shiftaddnet's People

Contributors

dependabot[bot] avatar ranery avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

shiftaddnet's Issues

AdderNet Problem

Hello,I want to realize the Transposed Adder layer according to the relationship between convolution and transposed convolution, but from the result I found that the image it generates has very obvious checkerboard artifacts. I think that it is because the variance of the output of AdderNet is much larger than CNN. So I want to know if you could give me some suggestions to solve it? Thanks very much!!!

data format

Hi, Author.
I have noticed that the experiments shown in the paper say that "Specifically, with full-precision
floating-point (FP32) ShiftAddNet even surpasses both the multiplication-based ConvNet and the
AdderNet". However, I wonder if the data format is FP-32, how can we use shift? Can float point data use shift operation? If so, how?

quantize 'sbm'

Hi,
Thanks a lot for the great work!
Just something that I would like to validate, in case we want to quantize only the FWD pass, we need to set 'sbm'?

round_to_fixed function

Hi, thanks for the great work!! And I am very interested in this work.

However, I am new to the area of quantization and have some questions about the round_to_fixed function in deepshift.utils Line7-18.

In line15 the torch.floor(input/delta) round the fp32 input to the nearest 16bit interger. In my opinion the clamp function should then be followed to clamp the nearest intergers to range(min_val, max_val), that is changing line15-17 to the following:
rounded = torch.floor(input/delta)
rounded = torch.clamp(rounded, min_val, max_val)
rounded = rounded*delta

Could you give me some comments about the difference of these two implementations? Thanks!!

Problem of unzip trained model

Hello, thank you for sharing your code.
I tried to inference with the trained model that you uploaded in google drive.
but when I unzip the trained model like 'tar -xvf resnet20-cifar10-FIX16.pth.tar', error messages are occurred like under sentences.
tar: This does not look like a tar archive
tar: Skipping to next header
tar: Exiting with failure status due to previous errors

I tried to handle with references in google search, but I couldn't.
Do you know how to handle this problem? Is there any problem in the trained model?
Thank you :)

.

.

How is back propagation of shift layer implemented

Hi,

I am really interested in your work and I think the proposed ShiftAddNet is really promising! However, it seems that there is some mismatch of the implementation and the paper. I find that the shift layer is implemented using a convolution in your code (in resnet_shiftadd.py). In fact, I really want to know the implementation details of back propagation of shift layer, since the weights of shift layer (i.e. s and p) should be integers which are not continuous variables. But I havn't find the code for back propagation of shift layer yet. Hope you can help me and correct me if I understand it wrong. Thank you very much!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.