| | |
| |
|
| | import torch |
| | from tqdm import tqdm |
| | import numpy as np |
| | try: |
| | import tinycudann as tcnn |
| | except: |
| | pass |
| |
|
| | |
| | |
| |
|
| | class TcnnFCBlock(tcnn.Network): |
| | def __init__( |
| | self, in_features, out_features, |
| | num_hidden_layers, hidden_features, |
| | activation:str='ReLU', last_activation:str='None', |
| | seed=42): |
| | assert hidden_features in [16, 32, 64, 128], "hidden_features can only be 16, 32, 64, or 128." |
| | super().__init__(in_features, out_features, network_config={ |
| | "otype": "FullyFusedMLP", |
| | "activation": activation, |
| | "output_activation": last_activation, |
| | "n_neurons": hidden_features, |
| | "n_hidden_layers": num_hidden_layers, |
| | }, seed=seed) |
| |
|
| | def forward(self, x: torch.Tensor): |
| | prefix = x.shape[:-1] |
| | return super().forward(x.flatten(0,-2)).unflatten(0, prefix) |
| |
|
| | device = torch.device('cuda:0') |
| | mlp = TcnnFCBlock(3, 256, 8, 128) |
| |
|
| | for _ in range(10000): |
| | for n, p in mlp.named_parameters(): |
| | p.grad = None |
| | _x = np.random.randint(200, 1000, 1)[0] |
| | x = torch.rand([_x,1000,3], dtype=torch.float, device=device) |
| | |
| | y = mlp.forward(x) |
| | y.mean().backward() |
| |
|