Skip to content

Commit 29798e0

Browse files
committed
apply formatting
1 parent 09d4b2d commit 29798e0

6 files changed

Lines changed: 119 additions & 32 deletions

File tree

tests/test_laplacian_pyramid.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,54 @@
66
from topoloss.losses.laplacian_pyramid import laplacian_pyramid_loss
77

88
supported_dtypes = [
9-
torch.float32,
10-
# torch.float16,
9+
torch.float32,
10+
# torch.float16,
1111
# torch.bfloat16
1212
]
1313

14+
1415
@pytest.mark.parametrize("dtype1", supported_dtypes)
1516
@pytest.mark.parametrize("dtype2", supported_dtypes)
1617
@pytest.mark.parametrize("height, width", [(16, 16), (32, 32)])
1718
@pytest.mark.parametrize("factor_w, factor_h", [(2.0, 2.0), (3.0, 3.0), (4.0, 4.0)])
1819
@pytest.mark.parametrize("interpolation", ["bilinear", "nearest"])
19-
def test_laplacian_pyramid_loss_precision(dtype1, dtype2, height, width, factor_w, factor_h, interpolation):
20+
def test_laplacian_pyramid_loss_precision(
21+
dtype1, dtype2, height, width, factor_w, factor_h, interpolation
22+
):
2023
# Create a sample cortical_sheet tensor
2124
e = 16 # Example depth
2225
torch.manual_seed(42) # Set seed for reproducibility
2326
cortical_sheet = torch.rand(height, width, e).to(dtype1)
2427

2528
# Call the function with the given precision
26-
loss = laplacian_pyramid_loss(cortical_sheet, factor_w=factor_w, factor_h=factor_h, interpolation=interpolation)
29+
loss = laplacian_pyramid_loss(
30+
cortical_sheet,
31+
factor_w=factor_w,
32+
factor_h=factor_h,
33+
interpolation=interpolation,
34+
)
2735

2836
# Ensure the loss is finite
2937
assert torch.isfinite(loss), f"Loss is not finite for dtype {dtype1}"
30-
38+
3139
# Check type consistency
32-
assert loss.dtype == dtype1, f"Loss dtype {loss.dtype} does not match input dtype {dtype1}."
40+
assert (
41+
loss.dtype == dtype1
42+
), f"Loss dtype {loss.dtype} does not match input dtype {dtype1}."
3343

3444
# Compare results to float32 (considered ground truth for higher precision)
35-
if dtype1 !=dtype2:
45+
if dtype1 != dtype2:
3646
float32_sheet = cortical_sheet.to(dtype2)
37-
expected_loss = laplacian_pyramid_loss(cortical_sheet=float32_sheet, factor_w=factor_w, factor_h=factor_h, interpolation=interpolation)
47+
expected_loss = laplacian_pyramid_loss(
48+
cortical_sheet=float32_sheet,
49+
factor_w=factor_w,
50+
factor_h=factor_h,
51+
interpolation=interpolation,
52+
)
3853
assert_close(
39-
loss.to(dtype2), expected_loss, rtol=1e-3, atol=1e-4,
40-
msg=f"Loss mismatch for dtype1 {dtype1} loss: {loss.to(dtype2)} and dtype2 {dtype2} loss2: {expected_loss}"
41-
)
54+
loss.to(dtype2),
55+
expected_loss,
56+
rtol=1e-3,
57+
atol=1e-4,
58+
msg=f"Loss mismatch for dtype1 {dtype1} loss: {loss.to(dtype2)} and dtype2 {dtype2} loss2: {expected_loss}",
59+
)

tests/test_loss_conv.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,22 @@
77
supported_dtypes = [
88
torch.float32,
99
# torch.float16,
10-
#torch.bfloat16,
10+
# torch.bfloat16,
1111
]
1212

13+
1314
# Define the fixture that provides the num_steps argument
1415
@pytest.mark.parametrize("num_steps", [2, 9])
1516
@pytest.mark.parametrize("hidden_channels", [16, 32])
1617
@pytest.mark.parametrize("init_from_layer", [True, False])
1718
@pytest.mark.parametrize("dtype", supported_dtypes)
1819
@pytest.mark.parametrize("interpolation", ["bilinear", "nearest"])
1920
def test_loss_conv(
20-
num_steps: int, hidden_channels: int, init_from_layer: bool, dtype, interpolation: str
21+
num_steps: int,
22+
hidden_channels: int,
23+
init_from_layer: bool,
24+
dtype,
25+
interpolation: str,
2126
): # num_steps is now passed by the fixture
2227

2328
# Define the model
@@ -31,16 +36,38 @@ def test_loss_conv(
3136
if init_from_layer:
3237
losses = [
3338
LaplacianPyramid.from_layer(
34-
model=model, layer=model[0], scale=1.0, factor_h=3.0, factor_w=3.0, interpolation=interpolation
39+
model=model,
40+
layer=model[0],
41+
scale=1.0,
42+
factor_h=3.0,
43+
factor_w=3.0,
44+
interpolation=interpolation,
3545
),
3646
LaplacianPyramid.from_layer(
37-
model=model, layer=model[2], scale=1.0, factor_h=3.0, factor_w=3.0, interpolation=interpolation
47+
model=model,
48+
layer=model[2],
49+
scale=1.0,
50+
factor_h=3.0,
51+
factor_w=3.0,
52+
interpolation=interpolation,
3853
),
3954
]
4055
else:
4156
losses = [
42-
LaplacianPyramid(layer_name="0", scale=1.0, factor_h=3.0, factor_w=3.0, interpolation=interpolation),
43-
LaplacianPyramid(layer_name="2", scale=1.0, factor_h=3.0, factor_w=3.0, interpolation=interpolation),
57+
LaplacianPyramid(
58+
layer_name="0",
59+
scale=1.0,
60+
factor_h=3.0,
61+
factor_w=3.0,
62+
interpolation=interpolation,
63+
),
64+
LaplacianPyramid(
65+
layer_name="2",
66+
scale=1.0,
67+
factor_h=3.0,
68+
factor_w=3.0,
69+
interpolation=interpolation,
70+
),
4471
]
4572

4673
# Define the TopoLoss

tests/test_loss_linear.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# torch.bfloat16, ### tests fail on bfloat16
1111
]
1212

13+
1314
# Define the fixture that provides the num_steps argument
1415
@pytest.mark.parametrize("num_steps", [2, 9])
1516
@pytest.mark.parametrize("hidden_size", [30, 25])

topoloss/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
from .core import TopoLoss, LaplacianPyramid, LaplacianPyramidOnBias, LaplacianPyramidOnInput
1+
from .core import (
2+
TopoLoss,
3+
LaplacianPyramid,
4+
LaplacianPyramidOnBias,
5+
LaplacianPyramidOnInput,
6+
)

topoloss/cortical_sheet/input.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def get_cortical_sheet_linear_input(layer: nn.Linear):
1313
return rearrange(
1414
weight,
1515
"o (h w) -> h w o",
16-
h = cortical_sheet_size.height,
17-
w = cortical_sheet_size.width,
18-
o = weight.shape[0]
16+
h=cortical_sheet_size.height,
17+
w=cortical_sheet_size.width,
18+
o=weight.shape[0],
1919
)

topoloss/losses/laplacian_pyramid.py

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99
from dataclasses import dataclass, field
1010
import torch
1111

12+
1213
def laplacian_pyramid_loss(
13-
cortical_sheet: TensorType["height", "width", "e"], factor_w: float, factor_h: float, interpolation: str = "bilinear"
14+
cortical_sheet: TensorType["height", "width", "e"],
15+
factor_w: float,
16+
factor_h: float,
17+
interpolation: str = "bilinear",
1418
):
1519
grid = cortical_sheet
1620
assert grid.ndim == 3, "Expected grid to be a 3d tensor of shape (h, w, e)"
@@ -27,7 +31,9 @@ def laplacian_pyramid_loss(
2731
grid, scale_factor=(1 / factor_h, 1 / factor_w), mode=interpolation
2832
)
2933
# Upscale the downscaled grid tensor
30-
upscaled_grid = F.interpolate(downscaled_grid, size=grid.shape[2:], mode=interpolation)
34+
upscaled_grid = F.interpolate(
35+
downscaled_grid, size=grid.shape[2:], mode=interpolation
36+
)
3137

3238
# Calculate the MSE loss between the original grid and upscaled grid
3339
# loss = F.mse_loss(upscaled_grid, grid)
@@ -54,19 +60,30 @@ class LaplacianPyramid:
5460
scale: Optional[Union[None, float]] = field(default=1.0)
5561

5662
@classmethod
57-
def from_layer(cls, model, layer, factor_h, factor_w, scale=1.0, interpolation: str ="bilinear"):
63+
def from_layer(
64+
cls,
65+
model,
66+
layer,
67+
factor_h,
68+
factor_w,
69+
scale=1.0,
70+
interpolation: str = "bilinear",
71+
):
5872
layer_name = get_name_by_layer(model=model, layer=layer)
5973
return cls(
6074
layer_name=layer_name,
6175
scale=scale,
6276
factor_h=factor_h,
6377
factor_w=factor_w,
64-
interpolation=interpolation
78+
interpolation=interpolation,
6579
)
6680

6781

6882
def laplacian_pyramid_loss_on_bias(
69-
cortical_sheet: TensorType["h", "w"], factor_w: float, factor_h: float, interpolation: str = "bilinear"
83+
cortical_sheet: TensorType["h", "w"],
84+
factor_w: float,
85+
factor_h: float,
86+
interpolation: str = "bilinear",
7087
):
7188

7289
grid = cortical_sheet
@@ -86,7 +103,9 @@ def laplacian_pyramid_loss_on_bias(
86103
grid, scale_factor=(1 / factor_h, 1 / factor_w), mode=interpolation
87104
)
88105
# Upscale the downscaled grid tensor
89-
upscaled_grid = F.interpolate(downscaled_grid, size=grid.shape[2:], mode=interpolation)
106+
upscaled_grid = F.interpolate(
107+
downscaled_grid, size=grid.shape[2:], mode=interpolation
108+
)
90109

91110
grid = rearrange(grid.squeeze(0).squeeze(0), "h w -> (h w)").unsqueeze(0)
92111
upscaled_grid = rearrange(
@@ -113,7 +132,15 @@ class LaplacianPyramidOnBias:
113132
scale: Optional[Union[None, float]] = field(default=1.0)
114133

115134
@classmethod
116-
def from_layer(cls, model, layer, factor_h, factor_w, scale=1.0, interpolation: str ="bilinear"):
135+
def from_layer(
136+
cls,
137+
model,
138+
layer,
139+
factor_h,
140+
factor_w,
141+
scale=1.0,
142+
interpolation: str = "bilinear",
143+
):
117144
assert (
118145
layer.bias is not None
119146
), "Expected layer to have a bias, but got None. *sad sad sad*"
@@ -123,9 +150,10 @@ def from_layer(cls, model, layer, factor_h, factor_w, scale=1.0, interpolation:
123150
scale=scale,
124151
factor_h=factor_h,
125152
factor_w=factor_w,
126-
interpolation=interpolation
153+
interpolation=interpolation,
127154
)
128155

156+
129157
@dataclass
130158
class LaplacianPyramidOnInput:
131159
"""
@@ -144,12 +172,20 @@ class LaplacianPyramidOnInput:
144172
scale: Optional[Union[None, float]] = field(default=1.0)
145173

146174
@classmethod
147-
def from_layer(cls, model, layer, factor_h, factor_w, scale=1.0, interpolation: str ="bilinear"):
175+
def from_layer(
176+
cls,
177+
model,
178+
layer,
179+
factor_h,
180+
factor_w,
181+
scale=1.0,
182+
interpolation: str = "bilinear",
183+
):
148184
layer_name = get_name_by_layer(model=model, layer=layer)
149185
return cls(
150186
layer_name=layer_name,
151187
scale=scale,
152188
factor_h=factor_h,
153189
factor_w=factor_w,
154-
interpolation=interpolation
155-
)
190+
interpolation=interpolation,
191+
)

0 commit comments

Comments
 (0)