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

[MPS] Add lerp implementation #105470

Closed
wants to merge 3 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 29 additions & 12 deletions aten/src/ATen/native/mps/operations/BinaryOps.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <ATen/ops/gt_native.h>
#include <ATen/ops/hypot_native.h>
#include <ATen/ops/le_native.h>
#include <ATen/ops/lerp_native.h>
#include <ATen/ops/logaddexp2_native.h>
#include <ATen/ops/logaddexp_native.h>
#include <ATen/ops/lt_native.h>
Expand Down Expand Up @@ -46,7 +47,7 @@
#define BinaryOpFn(graph, primary, secondary) \
MPSGraphTensor*(mps::BinaryOpCachedGraph * graph, MPSGraphTensor * primary, MPSGraphTensor * secondary)

// alpha is always 1.0 except when this function is called from add_sub_template()
// alpha is always 1.0 except when this function is called from add_sub_lerp_template()
void binaryOpTensor(const Tensor& self,
const Tensor& other,
const Scalar& alpha,
Expand Down Expand Up @@ -173,7 +174,7 @@ void binaryOpTensor(const Tensor& self,
feeds[otherPlaceholder.getMPSGraphTensor()] = otherPlaceholder.getMPSGraphTensorData();
}

// 'cachedGraph->alphaTensor' is not nil only if add_sub_template() was called with an alpha value != 1.0
// 'cachedGraph->alphaTensor' is not nil only if add_sub_lerp_template() was called with an alpha value != 1.0
if (cachedGraph->alphaTensor) {
alpha_scalar = getMPSScalar(alpha, other.scalar_type());
feeds[cachedGraph->alphaTensor] = getMPSGraphTensorFromScalar(mpsStream, alpha_scalar);
Expand Down Expand Up @@ -255,11 +256,11 @@ void div_mode_template(const Tensor& self,
div_mode_op_block);
}

void add_sub_template(const Tensor& self,
const Tensor& other,
const Scalar& alpha,
const Tensor& output,
std::string op_name) {
void add_sub_lerp_template(const Tensor& self,
const Tensor& other,
const Scalar& alpha,
const Tensor& output,
std::string op_name) {
if (alpha.toDouble() == 0.0) {
if (!self.is_alias_of(output)) { // if inplace, no-op
const_cast<Tensor&>(output) = self.clone();
Expand All @@ -273,18 +274,31 @@ void add_sub_template(const Tensor& self,
at::native::alpha_check(commonDtype, alpha);
}

BinaryOpBlock add_sub_op_block = ^BinaryOpFn(cachedGraph, primaryCastTensor, secondaryCastTensor) {
if (!alpha_has_value && op_name == "lerp") {
if (!self.is_alias_of(other)) { // if inplace, no-op
output.copy_(other);
}
return;
}

BinaryOpBlock add_sub_lerp_op_block = ^BinaryOpFn(cachedGraph, primaryCastTensor, secondaryCastTensor) {
MPSGraph* mpsGraph = cachedGraph->graph();
MPSGraphTensor* secondaryTensor = secondaryCastTensor;

if (op_name == "lerp") {
secondaryCastTensor = [mpsGraph subtractionWithPrimaryTensor:secondaryCastTensor
secondaryTensor:primaryCastTensor
name:nil];
}

// if alpha is 1.0, then we don't bother adding another multiply to graph
if (alpha_has_value) {
cachedGraph->alphaTensor = mpsGraphRankedPlaceHolder(mpsGraph, getMPSScalarType(other.scalar_type()), @[ @1 ]);
secondaryTensor = [mpsGraph multiplicationWithPrimaryTensor:secondaryCastTensor
secondaryTensor:cachedGraph->alphaTensor
name:nil];
}
if (op_name == "add")
if (op_name == "add" || op_name == "lerp")
return [mpsGraph additionWithPrimaryTensor:primaryCastTensor secondaryTensor:secondaryTensor name:nil];
else
return [mpsGraph subtractionWithPrimaryTensor:primaryCastTensor secondaryTensor:secondaryTensor name:nil];
Expand All @@ -295,7 +309,7 @@ void add_sub_template(const Tensor& self,
alpha,
output,
op_name + "_out_mps:" + (alpha_has_value ? getMPSTypeString(alpha.type()) : ""),
add_sub_op_block);
add_sub_lerp_op_block);
}

} // namespace mps
Expand Down Expand Up @@ -389,11 +403,11 @@ void add_sub_template(const Tensor& self,
}

TORCH_IMPL_FUNC(add_out_mps)(const Tensor& self, const Tensor& other, const Scalar& alpha, const Tensor& output) {
mps::add_sub_template(self, other, alpha, output, "add");
mps::add_sub_lerp_template(self, other, alpha, output, "add");
}

TORCH_IMPL_FUNC(sub_out_mps)(const Tensor& self, const Tensor& other, const Scalar& alpha, const Tensor& output) {
mps::add_sub_template(self, other, alpha, output, "sub");
mps::add_sub_lerp_template(self, other, alpha, output, "sub");
}

TORCH_IMPL_FUNC(pow_Scalar_out_mps)(const Scalar& base, const Tensor& exp, const Tensor& out) {
Expand Down Expand Up @@ -492,4 +506,7 @@ Tensor floor_divide_mps(const Tensor& self, const Tensor& other) {
mps::binaryOpTensor(self, other, Scalar(1.0), output, "xlogy_out_mps", xlogy_op_block);
}

TORCH_IMPL_FUNC(lerp_Scalar_mps)(const Tensor& self, const Tensor& end, const Scalar& weight, const Tensor& out) {
mps::add_sub_lerp_template(self, end, weight, out, "lerp");
}
} // namespace at::native
18 changes: 18 additions & 0 deletions aten/src/ATen/native/mps/operations/Lerp.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#define TORCH_ASSERT_ONLY_METHOD_OPERATORS
#include <ATen/core/Tensor.h>

#ifndef AT_PER_OPERATOR_HEADERS
#include <ATen/Functions.h>
#include <ATen/NativeFunctions.h>
#else
#include <ATen/ops/add.h>
#include <ATen/ops/lerp_native.h>
#endif

namespace at::native {
TORCH_IMPL_FUNC(lerp_Tensor_mps)(const Tensor& self, const Tensor& end, const Tensor& weight, const Tensor& out) {
// TODO: Write a much better implementation
at::add_out(const_cast<Tensor&>(out), self, weight.mul(end.sub(self)));
}

} // namespace at::native
2 changes: 2 additions & 0 deletions aten/src/ATen/native/native_functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9237,6 +9237,7 @@
structured_inherits: TensorIteratorBase
dispatch:
CPU, CUDA: lerp_Scalar
MPS: lerp_Scalar_mps
tags: pointwise

- func: lerp.Tensor_out(Tensor self, Tensor end, Tensor weight, *, Tensor(a!) out) -> Tensor(a!)
Expand All @@ -9245,6 +9246,7 @@
structured_inherits: TensorIteratorBase
dispatch:
CPU, CUDA: lerp_Tensor
MPS: lerp_Tensor_mps
tags: pointwise

- func: lerp.Scalar(Tensor self, Tensor end, Scalar weight) -> Tensor
Expand Down
1 change: 0 additions & 1 deletion test/test_mps.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,6 @@ def mps_ops_modifier(ops):
'isposinf': None,
'kthvalue': None,
'lcm': None,
'lerp': None,
'lgamma': None,
'linalg.cholesky': None,
'linalg.cholesky_ex': None,
Expand Down