Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/quantem/core/ml/models/kplanes.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ def __init__(
nn.init.zeros_(out.bias)
layers.append(out)
self.sigma_net = nn.Sequential(*layers)
else:
# Linear head fallback, matching KPlanesTILTED._build_sigma_net and
# CPTilted: forward/get_params reference sigma_net unconditionally.
self.sigma_net = nn.Linear(self.feature_dim, 1, bias=True)
nn.init.normal_(self.sigma_net.weight, std=0.01)
nn.init.zeros_(self.sigma_net.bias)

def get_densities(self, coords: torch.Tensor):
"""Computes and returns densities"""
Expand Down
21 changes: 21 additions & 0 deletions tests/ml/test_kplanes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,24 @@ def test_anisotropic_raises(self):
KPlanes(M_features=2, resolution=(16, 16, 8))
with pytest.raises(ValueError, match="isotropic"):
KPlanesTILTED(M_features=2, T=2, resolution=(16, 8, 16))


class TestDefaultHeadConstruction:
"""Regression: KPlanes(use_hybrid_mlp=False) -- the constructor default --
built no sigma_net at all, so forward / get_params / ObjectTensorDecomp
.from_model crashed with AttributeError. KPlanesTILTED and CPTilted both
fall back to a linear head; plain KPlanes must do the same."""

def test_default_get_params(self):
model = KPlanes(M_features=2, resolution=(8, 8, 8))
params = model.get_params()
assert set(params) == set(model.param_keys)
assert all(len(v) > 0 for v in params.values())

def test_default_forward(self):
import torch

model = KPlanes(M_features=2, resolution=(8, 8, 8))
out = model(torch.rand(5, 3) * 2 - 1)
assert out.shape == (5, 1)
assert torch.isfinite(out).all()