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

FusedBiasAct Native Pytorch Implementation #70

Open
mandelapatrick opened this issue Apr 28, 2020 · 5 comments
Open

FusedBiasAct Native Pytorch Implementation #70

mandelapatrick opened this issue Apr 28, 2020 · 5 comments

Comments

@mandelapatrick
Copy link

Hi! Great job on this repo - well done! I am trying to crate a native Pytorch implementation of the C++ function fused.fused_bias_act and I was wonder if you can help me. My attempt is below.
Mostly I am not clear what bias, refer, act, grad and scale refer to in the C++ implementation.

def fused_bias_act(inp, bias, refer, act, grad, negative_slope, scale):
noise = bias + refer
fused_act = torch.nn.functional.leaky_relu(inp + noise, negative_slope=negative_slope, inplace=False) * scale
return fused_act

@rosinality
Copy link
Owner

rosinality commented Apr 28, 2020

It will be like this:

def fused_bias_act(inp, bias, negative_slope=0.2, scale=2 ** 0.5):
    fused_act = torch.nn.functional.leaky_relu(inp + bias, negative_slope=negative_slope, inplace=False) * scale

    return fused_act

@mandelapatrick
Copy link
Author

Thanks so much for your response! I am having one issue though. When doing a forward pass through the mode, inp=torch.Size([16, 512, 4, 4]) and bias=torch.Size([512]) during the forward pass of StyledConv on line 357, so it can't do the addition. Any suggestions to fix this?

@rosinality
Copy link
Owner

You will need to use .view to add axes to bias vector.

@Animadversio
Copy link

Animadversio commented Jun 10, 2020

@rosinality Currently I used this .view statement

def fused_leaky_relu(input, bias, negative_slope=0.2, scale=2 ** 0.5):
    return scale * F.leaky_relu(input + bias.view((1, -1)+(1,)*(len(input.shape)-2)), negative_slope=negative_slope)

Not sure if this is the most efficient one but it works on my side!
The (len(input.shape)-2)) is added since sometimes the input is 2d sometimes it's 4d

@silence-tang
Copy link

Thanks so much for your answers. It helps me a lot!
I have a question that will the PyTorch implementation of fused_leaky_relu affect the performance of the model or the results of the model negatively?

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

4 participants