Skip to content

Enable fix for UpsampleNearest functions in Pytorch #4959

@mikaylagawarecki

Description

@mikaylagawarecki

🚀 Feature

In response to pytorch/pytorch#62396, we opened pytorch/pytorch#97869 and pytorch/pytorch#97868 in the PyTorch repo

Motivation

Upsample with mode nearest in Pytorch has a bug where the output size is rounded incorrectly using floor() rather than round() which makes it inconsistent with other frameworks like openCV, tensorflow etc. We are fixing this by adding a kwarg per the approach in pytorch/pytorch#64983 (comment). This is breaking xla CI, we need to fix this on XLA side as well in order to land the PR

Pitch

The specific code is here:

std::vector<int64_t> GetOutputSizeWithScale(
absl::Span<const int64_t> input_size, const c10::optional<double> scales_h,
const c10::optional<double> scales_w,
const std::vector<int64_t>& output_size) {
XLA_CHECK(scales_h);
XLA_CHECK(scales_w);
// Calculate the output size from input_shape and scale_factors
XLA_CHECK_EQ(input_size.size(), 4);
int64_t output_h = input_size[2] * (*scales_h);
int64_t output_w = input_size[3] * (*scales_w);
return {output_h, output_w};
}
which should compute output size with either round or implicit floor
int64_t output_h = 0.5 + input_size[2] * (*scales_h);
or
int64_t output_h = input_size[2] * (*scales_h);
in the UpsampleNearest variants depending on new flag round_with_scale_factor

However, this flag can't be exposed in, for example, upsample_bilinear2d function:

at::Tensor XLANativeFunctions::upsample_bilinear2d(
as output size was already correctly computed before dispatching to XLA.

Alternatives

Additional context

cc @vfdev-5 @JackCaoG

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions