(self)
| 1191 | self.assertEqual(y.dtype, mx.float16) |
| 1192 | |
| 1193 | def test_upsample(self): |
| 1194 | b, h, w, c = 1, 2, 2, 1 |
| 1195 | scale_factor = 2 |
| 1196 | upsample_nearest = nn.Upsample( |
| 1197 | scale_factor=scale_factor, mode="nearest", align_corners=True |
| 1198 | ) |
| 1199 | upsample_bilinear = nn.Upsample( |
| 1200 | scale_factor=scale_factor, mode="linear", align_corners=True |
| 1201 | ) |
| 1202 | upsample_nearest = nn.Upsample( |
| 1203 | scale_factor=scale_factor, mode="nearest", align_corners=True |
| 1204 | ) |
| 1205 | upsample_bilinear_no_align_corners = nn.Upsample( |
| 1206 | scale_factor=scale_factor, mode="linear", align_corners=False |
| 1207 | ) |
| 1208 | upsample_nearest_no_align_corners = nn.Upsample( |
| 1209 | scale_factor=scale_factor, mode="nearest", align_corners=False |
| 1210 | ) |
| 1211 | # Test single feature map, align corners |
| 1212 | x = mx.arange(b * h * w * c).reshape((b, c, h, w)).transpose((0, 2, 3, 1)) |
| 1213 | expected_nearest = mx.array( |
| 1214 | [[[[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]]] |
| 1215 | ).transpose((0, 2, 3, 1)) |
| 1216 | expected_bilinear = mx.array( |
| 1217 | [ |
| 1218 | [ |
| 1219 | [ |
| 1220 | [0, 0.333333, 0.666667, 1], |
| 1221 | [0.666667, 1, 1.33333, 1.66667], |
| 1222 | [1.33333, 1.66667, 2, 2.33333], |
| 1223 | [2, 2.33333, 2.66667, 3], |
| 1224 | ] |
| 1225 | ] |
| 1226 | ] |
| 1227 | ).transpose((0, 2, 3, 1)) |
| 1228 | # Test single feature map, no align corners |
| 1229 | x = ( |
| 1230 | mx.arange(1, b * h * w * c + 1) |
| 1231 | .reshape((b, c, h, w)) |
| 1232 | .transpose((0, 2, 3, 1)) |
| 1233 | ) |
| 1234 | expected_bilinear_no_align_corners = mx.array( |
| 1235 | [ |
| 1236 | [ |
| 1237 | [ |
| 1238 | [1.0000, 1.2500, 1.7500, 2.0000], |
| 1239 | [1.5000, 1.7500, 2.2500, 2.5000], |
| 1240 | [2.5000, 2.7500, 3.2500, 3.5000], |
| 1241 | [3.0000, 3.2500, 3.7500, 4.0000], |
| 1242 | ] |
| 1243 | ] |
| 1244 | ] |
| 1245 | ).transpose((0, 2, 3, 1)) |
| 1246 | expected_nearest_no_align_corners = mx.array( |
| 1247 | [[[[1, 1, 2, 2], [1, 1, 2, 2], [3, 3, 4, 4], [3, 3, 4, 4]]]] |
| 1248 | ).transpose((0, 2, 3, 1)) |
| 1249 | self.assertTrue( |
| 1250 | np.allclose( |
nothing calls this directly
no test coverage detected