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

Rewrite for convolution operation #241

Open
CheungBH opened this issue May 6, 2023 · 0 comments
Open

Rewrite for convolution operation #241

CheungBH opened this issue May 6, 2023 · 0 comments

Comments

@CheungBH
Copy link

CheungBH commented May 6, 2023

Thanks for your great work.
I want to use the inference of sparse conv operation, but the code doesn't provide such a function. Therefore, I am rewriting it like this.
When only_forward=True, the input will be processed directly without the operation of ctx.
However, I found there are nan using such a method. Do you have any ideas for solving it?

class ConvolutionFunction(Function):

@staticmethod
def forward(
        ctx,
        input_features,
        weight,
        bias,
        input_metadata,
        input_spatial_size,
        output_spatial_size,
        dimension,
        filter_size,
        filter_stride,
        only_forward=False):
    output_features = input_features.new()
    if only_forward:
        sparseconvnet.SCN.Convolution_updateOutput(
            input_spatial_size,
            output_spatial_size,
            filter_size,
            filter_stride,
            input_metadata,
            input_features,
            output_features,
            weight,
            bias)
        return output_features

    output_features = input_features.new()
    ctx.input_metadata = input_metadata
    ctx.dimension = dimension
    ctx.save_for_backward(
        input_features,
        input_spatial_size,
        weight,
        bias,
        output_spatial_size,
        filter_size,
        filter_stride)
    sparseconvnet.forward_pass_multiplyAdd_count +=\
        sparseconvnet.SCN.Convolution_updateOutput(
            input_spatial_size,
            output_spatial_size,
            filter_size,
            filter_stride,
            input_metadata,
            input_features,
            output_features,
            weight,
            bias)
    sparseconvnet.forward_pass_hidden_states += output_features.nelement()
    return output_features

@staticmethod
def backward(ctx, grad_output):
    input_features, input_spatial_size, weight, bias, output_spatial_size, filter_size, filter_stride = ctx.saved_tensors
    grad_input = grad_output.new()
    grad_weight = torch.zeros_like(weight)
    grad_bias = torch.zeros_like(bias)
    sparseconvnet.SCN.Convolution_backward(
        input_spatial_size,
        output_spatial_size,
        filter_size,
        filter_stride,
        ctx.input_metadata,
        input_features,
        grad_input,
        grad_output.contiguous(),
        weight,
        grad_weight,
        grad_bias)
    return grad_input, grad_weight, optionalTensorReturn(grad_bias), None, None, None, None, None, None
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

1 participant