Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interpolate api align #9118

Merged
merged 1 commit into from
Sep 21, 2022
Merged
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
10 changes: 10 additions & 0 deletions python/oneflow/nn/modules/interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ def forward(self, x):
raise ValueError(
"recompute_scale_factor is not meaningful with an explicit size."
)
if isinstance(scale_factors, (list, tuple)):
scale_factors = [
float(_.numpy()) if (flow.is_tensor(_) and len(_.size()) == 0) else _
for _ in scale_factors
]
if isinstance(output_size, (list, tuple)):
output_size = [
int(_.numpy()) if (flow.is_tensor(_) and len(_.size()) == 0) else _
for _ in output_size
]
Comment on lines +124 to +133
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个对性能上影响应该很大,可否再实现一个版本的op,这个op直接接受这些float值作为输入tensor,而不是作为op的属性。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对于torch它也会触发一次同步,我感觉不能要求所有算子都去特化吧,pytorch的interpolate也没有特化scale和size为tensor的情况。另外性能也不一定有问题吧,只是一个scalar同步了一下而已

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对于torch它也会触发一次同步,我感觉不能要求所有算子都去特化吧,pytorch的interpolate也没有特化scale和size为tensor的情况。另外性能也不一定有问题吧,只是一个scalar同步了一下而已

oneflow可能收到的影响更大一点,这个同步会导致main线程大段空白,从而导致cuda stream断流。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果 scalar tensor 是在 cpu 上,影响其实比较小(因为 cpu stream 上相对于比较空闲);如果是在 cuda 上,这个东西要一路传到 kernel 里才能保证不断流,这又和现在的 kernel 不一致,需要单独写才能做到想要的效果。

所以目前这种方案应该还好。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,这种scalar tensor的同步感觉挺常见的,我们也不能要求所有kernel都重写一次

Comment on lines +124 to +133
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以在 python 代码那边加一个 TODO,后面我打算加一个 scalar tensor 到 scalar 的隐式转换,这样可以一次性支持更多 functional 的 scalar tensor 输入。

另外这里的会用到 _ 这个变量,最好改成实际的变量名。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

if self.mode == "area" and output_size is None:
self.recompute_scale_factor = True
if self.recompute_scale_factor is True:
Expand Down
22 changes: 22 additions & 0 deletions python/oneflow/test/modules/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,27 @@ def _test_interpolate_area_3d(test_case, device):
test_case.assertTrue(np.allclose(input.grad.numpy(), np_grad, 1e-05, 1e-05))


def _test_interpolate_output_size_arg_with_scalar(test_case, device):
mode = "bicubic"
x = flow.Tensor(8, 32, 64).to(device)

window = 16
t = x.shape[2]
x = x[:, None]

np_center = np.random.randint(window, t - window, (1,))[0]
np_warped = np.random.randint(np_center - window, np_center + window, (1,))[0] + 1

center = flow.tensor(np_center)
warped = flow.tensor(np_warped)

res = flow.nn.functional.interpolate(
x[:, :, :center], (warped, x.shape[3]), mode=mode, align_corners=False
)
test_case.assertTrue(np.array_equal(res.size()[0], 8))
test_case.assertTrue(np.array_equal(res.size()[1], 1))


@flow.unittest.skip_unless_1n1d()
class TestInterpolate(flow.unittest.TestCase):
def test_interpolate(test_case):
Expand All @@ -670,6 +691,7 @@ def test_interpolate(test_case):
_test_interpolate_area_1d,
_test_interpolate_area_2d,
_test_interpolate_area_3d,
_test_interpolate_output_size_arg_with_scalar,
]
arg_dict["device"] = ["cpu", "cuda"]
for arg in GenArgList(arg_dict):
Expand Down