8000 Fix lerp weight type promotion by zeshengzong · Pull Request #141117 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

Fix lerp weight type promotion #141117

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

Closed
wants to merge 8 commits into from
Closed
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: 8 additions & 2 deletions aten/src/ATen/native/Lerp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ TORCH_META_FUNC(lerp_Tensor)(
const Tensor& self, const Tensor& end, const Tensor& weight) {
TORCH_CHECK(self.dtype() == end.dtype(), "expected dtype ", self.dtype(),
" for `end` but got dtype ", end.dtype());
TORCH_CHECK(self.dtype() == weight.dtype(), "expected dtype ", self.dtype(),
" for `weight` but got dtype ", weight.dtype());
bool promote_weight = weight.dim() == 0;
if (!promote_weight) {
TORCH_CHECK(self.dtype() == weight.dtype(), "expected dtype ", self.dtype(),
" for `weight` but got dtype ", weight.dtype());
}
build(at::TensorIteratorConfig()
.allow_cpu_scalars(true)
.promote_inputs_to_common_dtype(promote_weight)
.enforce_safe_casting_to_output(promote_weight)
.cast_common_dtype_to_outputs(promote_weight)
Copy link
Contributor

Choose a reason for hiding this comment

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

what does this do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hello,

  • promote_inputs_to_common_dtype enable type promotion instead of raise error directly, main function used to fix the issue.

  • enforce_safe_casting_to_output will do check on out.dtype with others, make sure canCast to out.dtype (which guard use-case like out.dtype=torch.long inconsistent with other param like above)

For operations that do participate in type promotion the copy can be to a different dtype, but the destination of the copy cannot be a lower "type kind" than the source. PyTorch has four type kinds: boolean, integer, float, and complex, in that order.

if (config.enforce_safe_casting_to_output_ && op.is_output && op.current_dtype != common_dtype_) {
TORCH_CHECK(canCast(common_dtype_, op.current_dtype),
"result type ", common_dtype_, " can't be cast to the "
"desired output type ", op.current_dtype);
}

  • cast_common_dtype_to_outputs in cpu device for creating temp tensor to cast output,

if (common_device == kCPU) {
// Casts to outputs by creating temporaries of the correct dtype (if needed)
// NB: we skip this on is_meta_, because the temporary allocation here is
// unnecessary if we aren't going to actually do the compute
if (config.cast_common_dtype_to_outputs_ && op.is_output && op.current_dtype != common_dtype_ && !is_meta_) {
TORCH_INTERNAL_ASSERT(op.tensor_base().defined());
// Marker [Output original_tensor is set]
// NB: do NOT use set_output here, as the temporary is NOT a true output;
// op.tensor is the true output and it was pre-provided for us.
// TODO: The logic for cast_outputs will need to be handled by the
// structured kernels implementation. What probably should happen
// is that we pass in the inferred dtype into the out kernel, and
// then after calling the out kernel, do the conversion (which
// is cast_outputs here), but integrating this with existing
// TensorIterator will take a little doing
op.exchange_tensor(c10::MaybeOwned<TensorBase>::owned(
at::empty_like(op.tensor(),
op.tensor_base().options().dtype(common_dtype_),
LEGACY_CONTIGUOUS_MEMORY_FORMAT)));
if (!names_.empty()) {
namedinference::propagate_names(op.tensor_base(), names_);
}
op.current_dtype = common_dtype_;
op.target_dtype = common_dtype_;
}

For other ops support promotion by setting all these flags to true, like lerp_Scalar (add, sub, mul, ... as well) use macros in here

#define BINARY_OP_CONFIG() \
TensorIteratorConfig() \
.set_check_mem_overlap(true) \
.allow_cpu_scalars(true) \
.promote_inputs_to_common_dtype(true) \
.cast_common_dtype_to_outputs(true) \
.enforce_safe_casting_to_output(true) \
void TensorIteratorBase::build_binary_op(const TensorBase& out, const TensorBase& a, const TensorBase& b) {
build(BINARY_OP_CONFIG()
.add_owned_output(out)
.add_owned_const_input(a)
.add_owned_const_input(b));
}
void TensorIteratorBase::build_borrowing_binary_op(
const TensorBase& out, const TensorBase& a, const TensorBase& b) {
build(BINARY_OP_CONFIG()
.add_output(out)
.add_const_input(a)
.add_const_input(b));
}

The default value of flags is False, currently only make them work when weight.dim == 0. Thanks!

.add_output(maybe_get_output())
.add_const_input(self)
.add_const_input(end)
Expand Down
18 changes: 18 additions & 0 deletions test/test_binary_ufuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3519,6 +3519,24 @@ def test_lerp_lowp_cpu(self, device, dtype):
expected = torch.lerp(xref, yref, wref).to(dtype)
self.assertEqual(actual, expected, atol=0.0, rtol=0.0)

@dtypes(torch.float, torch.double, torch.cfloat, torch.cdouble)
def test_lerp_weight_scalar_tensor_promotion(self, device, dtype):
start = make_tensor((5, 5), dtype=dtype, device=device, low=1, high=100)
end = make_tensor((5, 5), dtype=dtype, device=device, low=1, high=100)
weight = torch.rand((), dtype=torch.float, device=device)

actual = torch.lerp(start, end, weight)
expected = start + weight.to(dtype) * (end - start)
self.assertEqual(expected, actual)

@dtypes(torch.double, torch.cfloat, torch.cdouble)
def test_lerp_weight_tensor_promotion_error(self, device, dtype):
start = make_tensor((5, 5), dtype=dtype, device=device, low=1, high=100)
end = make_tensor((5, 5), dtype=dtype, device=device, low=1, high=100)
weight = torch.rand((5, 5), dtype=torch.float, device=device)
with self.assertRaisesRegex(RuntimeError, "expected dtype"):
torch.lerp(start, end, weight)

def _test_logaddexp(self, device, dtype, base2):
if base2:
ref_func = np.logaddexp2
Expand Down
9 changes: 5 additions & 4 deletions torch/_meta_registrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6971,10 +6971,11 @@ def lerp(start, end, weight):
)
args = [start, end]
if isinstance(weight, TensorLike):
torch._check(
start.dtype == weight.dtype,
lambda: f"expected dtype {start.dtype} for `weight`, but got dtype {weight.dtype}",
)
if weight.ndim != 0:
torch._check(
start.dtype == weight.dtype,
lambda: f"expected dtype {start.dtype} for `weight`, but got dtype {weight.dtype}",
)
args.append(weight)
return elementwise_meta(
*args, type_promotion=ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT
Expand Down
Loading
0