mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-12 19:04:03 +08:00
* some initial cuda code as I learn. using git to save progress. * first cuda kernel for ray sampling that mostly works * cleaning up code. working on lr. structs for scene bounds aabb * added scene bounds collider code * updated setup * setup * setup * black * including some files * exporting env variable * new file * git workflows * github actions * github actions * github actions * removing env var
46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
"""
|
|
Sampler.
|
|
"""
|
|
|
|
import math
|
|
|
|
import torch
|
|
from torch import nn
|
|
|
|
import mattport_cuda
|
|
|
|
|
|
class LLTMFunction(torch.autograd.Function):
|
|
@staticmethod
|
|
def forward(ctx, input, weights, bias, old_h, old_cell):
|
|
outputs = mattport_cuda.forward(input, weights, bias, old_h, old_cell)
|
|
new_h, new_cell = outputs[:2]
|
|
variables = outputs[1:] + [weights]
|
|
ctx.save_for_backward(*variables)
|
|
|
|
return new_h, new_cell
|
|
|
|
@staticmethod
|
|
def backward(ctx, grad_h, grad_cell):
|
|
outputs = mattport_cuda.backward(grad_h.contiguous(), grad_cell.contiguous(), *ctx.saved_variables)
|
|
d_old_h, d_input, d_weights, d_bias, d_old_cell, d_gates = outputs
|
|
return d_input, d_weights, d_bias, d_old_h, d_old_cell
|
|
|
|
|
|
class Sampler(nn.Module):
|
|
def __init__(self, input_features, state_size):
|
|
super().__init__()
|
|
self.input_features = input_features
|
|
self.state_size = state_size
|
|
self.weights = nn.Parameter(torch.Tensor(3 * state_size, input_features + state_size))
|
|
self.bias = nn.Parameter(torch.Tensor(1, 3 * state_size))
|
|
self.reset_parameters()
|
|
|
|
def reset_parameters(self):
|
|
stdv = 1.0 / math.sqrt(self.state_size)
|
|
for weight in self.parameters():
|
|
weight.data.uniform_(-stdv, +stdv)
|
|
|
|
def forward(self, input, state):
|
|
return LLTMFunction.apply(input, self.weights, self.bias, *state)
|