forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LerpKernel.cpp
60 lines (53 loc) · 1.7 KB
/
LerpKernel.cpp
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
53
54
55
56
57
58
59
60
#include <ATen/ATen.h>
#include <ATen/Dispatch.h>
#include <ATen/native/Lerp.h>
#include <ATen/native/TensorIterator.h>
#include <ATen/native/cpu/Loops.h>
namespace at {
namespace native {
namespace {
static void lerp_kernel_scalar(
Tensor& ret,
const Tensor& self,
const Tensor& end,
Scalar weight) {
auto iter = TensorIterator::binary_op(ret, self, end,
/*check_mem_overlap=*/true);
AT_DISPATCH_FLOATING_TYPES(ret.scalar_type(), "lerp_kernel_scalar", [&] {
scalar_t weight_val = weight.to<scalar_t>();
at::native::cpu_kernel(
iter,
[weight_val](scalar_t self_val, scalar_t end_val) {
return (weight_val < 0.5)
? self_val + weight_val * (end_val - self_val)
: end_val - (end_val - self_val) * (1 - weight_val);
});
});
}
static void lerp_kernel_tensor(
Tensor& ret,
const Tensor& self,
const Tensor& end,
const Tensor& weights) {
auto iter = TensorIterator();
iter.set_check_mem_overlap(true);
iter.add_output(ret);
iter.add_input(self);
iter.add_input(end);
iter.add_input(weights);
iter.build();
AT_DISPATCH_FLOATING_TYPES(ret.scalar_type(), "lerp_kernel_tensor", [&] {
at::native::cpu_kernel(
iter,
[](scalar_t self_val, scalar_t end_val, scalar_t weight_val) {
return (weight_val < 0.5)
? self_val + weight_val * (end_val - self_val)
: end_val - (end_val - self_val) * (1 - weight_val);
});
});
}
} // anonymous namespace
REGISTER_DISPATCH(lerp_kernel_scalar_weight, &lerp_kernel_scalar);
REGISTER_DISPATCH(lerp_kernel_tensor_weight, &lerp_kernel_tensor);
} // namespace native
} // namespace at