Skip to content

Commit 9607b91

Browse files
authored
format unittest of autocast and _calculate_fan_in_and_fan_out (PaddlePaddle#76293)
1 parent f4c45e2 commit 9607b91

File tree

3 files changed

+16
-81
lines changed

3 files changed

+16
-81
lines changed

test/amp/test_amp_api.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ def test_amp_autocast2(self):
128128
)
129129
)
130130

131+
def test_autocast(self):
132+
self._run_autocast_test(
133+
paddle.autocast(
134+
device_type='cuda',
135+
enabled=True,
136+
dtype=paddle.float16,
137+
cache_enabled=True,
138+
)
139+
)
140+
131141
def test_cuda_amp_autocast(self):
132142
self._run_autocast_test(paddle.cuda.amp.autocast())
133143

test/legacy_test/test_autocast.py

Lines changed: 0 additions & 62 deletions
This file was deleted.

test/legacy_test/test_nn_init_function.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,6 @@ def _calculate_gain(nonlinearity, param):
6262
return recommended_gain[nonlinearity]
6363

6464

65-
def _calculate_fan_in_and_fan_out(var: paddle.Tensor) -> tuple[int, int]:
66-
shape = var.shape
67-
if not shape or len(shape) == 0:
68-
fan_in = fan_out = 1
69-
elif len(shape) == 1:
70-
fan_in = fan_out = shape[0]
71-
elif len(shape) == 2:
72-
fan_in = shape[0]
73-
fan_out = shape[1]
74-
else:
75-
receptive_field_size = np.prod(shape[2:])
76-
fan_in = shape[1] * receptive_field_size
77-
fan_out = shape[0] * receptive_field_size
78-
return (fan_in, fan_out)
79-
80-
8165
class Test_calculate_gain(unittest.TestCase):
8266
def test(self):
8367
for nonlinearity in [
@@ -106,20 +90,23 @@ def test(self):
10690
class TestCAlFanINOUT(unittest.TestCase):
10791
def test_cal_fan_in_and_out(self):
10892
x = paddle.tensor.randn([10])
93+
x_expected = (10, 10)
10994
self.assertEqual(
110-
_calculate_fan_in_and_fan_out(x),
95+
x_expected,
11196
paddle.nn.init._calculate_fan_in_and_fan_out(x),
11297
)
11398

11499
y = paddle.tensor.randn([10, 10])
100+
y_expected = (10, 10)
115101
self.assertEqual(
116-
_calculate_fan_in_and_fan_out(y),
102+
y_expected,
117103
paddle.nn.init._calculate_fan_in_and_fan_out(y),
118104
)
119105

120106
z = paddle.randn([10, 10, 10])
107+
z_expected = (100, 100)
121108
self.assertEqual(
122-
_calculate_fan_in_and_fan_out(z),
109+
z_expected,
123110
paddle.nn.init._calculate_fan_in_and_fan_out(z),
124111
)
125112

0 commit comments

Comments
 (0)