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

conversion from bnn.SpatialConvolution and bnn.binary to nn or cudnn #12

Open
vinayak618 opened this issue Oct 5, 2018 · 5 comments
Open

Comments

@vinayak618
Copy link

vinayak618 commented Oct 5, 2018

I was trying to convert this model into CPU supported device(nn), was able to convert some of the layers using cudnn.convert, but getting stuck at layers from bnn.torch, has anyone converted bnn layers to nn or cudnn.
any help would be appreciated.
Thank you

@vinayak618 vinayak618 changed the title is Cuda Neccessary? conversion from bnn.SpatialConvolution and bnn.binary to nn or cudnn Oct 12, 2018
@btomtom5
Copy link

btomtom5 commented Nov 6, 2018

I don't think that converting layers in bnn.torch to a cpu layer is going to work without some effort. If you look at the implementation of the layers in bnn.torch, they are implemented in Nvidia CUDA. You are going to have to write the corresponding cpu code yourself and use this function to swap it out:

def replace_module(module, check_fn, create_fn):
    if not hasattr(module, 'modules'):
        return
    if module.modules is None:
        return
    for i in range(len(module.modules)):
        m = module.modules[i]
        if check_fn(m):
            module.modules[i] = create_fn(m)
        replace_module(m, check_fn, create_fn)

@vinayak618
Copy link
Author

Thank you for reply btomtom5.
I understand that, But since the base code for bnn.torch is written in Nvidia Cuda.
Can I atleast convert it to "cudnn."
So then I can use cudnn.torch to convert it to equivalent nn

@btomtom5
Copy link

btomtom5 commented Nov 7, 2018

Hmmm. I don't think that works because the lua convert function relies on the destination module nn in this case to have the corresponding classes SpatialConvolution and Binary. nn does have a SpatialConvolution defined but it needs to decode the weights before it can do the convolution. bnn.SpatialConvolution encodes the 32 weights of 0 and 1 as a single 32 bit integer.

Why do you want to run it in cpu only mode? I suggest renting out an ec2 instance with gpu support.

@vinayak618
Copy link
Author

Yeah.
So the only alternative would be writing a CPU equivalent of bnn.torch?

@btomtom5
Copy link

btomtom5 commented Nov 8, 2018

That's correct, but you only need to write the forward pass of it.

  1. decode the weights from int32 to binary
  2. perform regular spatial convolution from the binary weights || perform bitwise operation which is much more performant.

After you do this, you should search through the loaded module and replace them with your newly written module using the code I shared above. You also have to make sure that the weights are migrated over though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants