forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLerp.cu
52 lines (47 loc) · 1.71 KB
/
Lerp.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#define TORCH_ASSERT_NO_OPERATORS
#include <ATen/native/Lerp.h>
#include <ATen/Dispatch.h>
#include <ATen/TensorIterator.h>
#include <ATen/native/cuda/Loops.cuh>
namespace at {
namespace native {
namespace {
void lerp_tensor_kernel(at::TensorIteratorBase& iter) {
AT_DISPATCH_FLOATING_AND_COMPLEX_TYPES_AND2(
at::ScalarType::Half, at::ScalarType::BFloat16,
iter.common_dtype(), "lerp_cuda",
[&] {
at::native::gpu_kernel(
iter,
[] GPU_LAMBDA(
scalar_t self_val,
scalar_t end_val,
scalar_t weight_val) -> scalar_t {
return (std::abs(weight_val) < 0.5)
? self_val + weight_val * (end_val - self_val)
: end_val -
(end_val - self_val) *
(static_cast<scalar_t>(1) - weight_val);
});
});
}
void lerp_scalar_kernel(at::TensorIteratorBase& iter, const c10::Scalar& weight) {
AT_DISPATCH_FLOATING_AND_COMPLEX_TYPES_AND2(
at::ScalarType::Half, at::ScalarType::BFloat16,
iter.common_dtype(), "lerp_cuda",
[&]{
auto weight_val = weight.to<scalar_t>();
at::native::gpu_kernel(
iter, [=] GPU_LAMBDA(scalar_t self_val, scalar_t end_val) {
return (std::abs(weight_val) < 0.5)
? self_val + weight_val * (end_val - self_val)
: end_val -
(end_val - self_val) * (static_cast<scalar_t>(1) - weight_val);
});
});
}
} // anonymous namespace
REGISTER_DISPATCH(lerp_kernel_tensor_weight, &lerp_tensor_kernel);
REGISTER_DISPATCH(lerp_kernel_scalar_weight, &lerp_scalar_kernel);
} // namespace native
} // namespace at