ashawkey / torch-ngp

A pytorch CUDA extension implementation of instant-ngp (sdf and nerf), with a GUI.
MIT License
2.11k stars 275 forks source link

[BUG?] GridEncoder doesn't give correct gradient with respect to the coordinate #149

Closed zjumsj closed 1 year ago

zjumsj commented 1 year ago

@ashawkey My recent project requires the gradient with respect to the coordinate (inputs in code, BxD, D=3). However, the gradient returned seems incorrect. Only the first channel of grad_inputs has non-zero value. I guess the mistake is at line 143 in gridencoder.cu where only the first element of array pos_deriv is set to 1.0f. Hope that you can fix it if it's really a mistake.

    // calculate coordinate (always use float for precision!)
    float pos[D];
    float pos_deriv[D] = {1.0f}; // linear deriv is default to 1
    uint32_t pos_grid[D];
ashawkey commented 1 year ago

@zjumsj You are right! Thanks for reporting this bug. It should have been fixed in https://github.com/ashawkey/torch-ngp/commit/c3fab17e762efa46e56d5baaa2ad82531270ab16.

zjumsj commented 1 year ago

Thanks, I think it gives reasonable results now.