forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lerp.cu
114 lines (103 loc) · 4.65 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <ATen/NativeFunctions.h>
#include <ATen/cuda/CUDAApplyUtils.cuh>
#include <ATen/Dispatch.h>
#include <ATen/ExpandUtils.h>
#include <ATen/native/cuda/Loops.cuh>
#include <ATen/native/TensorIterator.h>
namespace {
inline void lerp_cuda(at::Tensor& ret, const at::Tensor& self, const at::Tensor& end, const at::Tensor& weights) {
TORCH_CHECK(self.dtype() == end.dtype(), "expected dtype ", self.dtype(), " for `end` but got dtype ", end.dtype());
TORCH_CHECK(self.dtype() == weights.dtype(), "expected dtype ", self.dtype(), " for `weights` but got dtype ", weights.dtype());
at::TensorIterator iter = at::TensorIteratorConfig()
.add_output(ret)
.add_input(self)
.add_input(end)
.add_input(weights)
.build();
AT_DISPATCH_FLOATING_TYPES_AND_HALF(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 (weight_val < 0.5) ?
self_val + weight_val * (end_val - self_val) : end_val - (end_val - self_val) * (1 - weight_val);
});
});
}
template <typename scalar_t>
void lerp_scalar_cuda(at::Tensor& ret, const at::Tensor& self, const at::Tensor& end, scalar_t weight_val) {
TORCH_CHECK(self.dtype() == end.dtype(), "expected dtype ", self.dtype(), " for `end` but got dtype ", end.dtype());
at::TensorIterator iter = at::TensorIteratorConfig()
.add_output(ret)
.add_input(self)
.add_input(end)
.build();
at::native::gpu_kernel(iter,
[=] GPU_LAMBDA (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);
});
}
} // namespace
namespace at {
namespace native {
Tensor& lerp_cuda_tensor_out(Tensor& result, const Tensor& self,
const Tensor& end, const Tensor& weight) {
Tensor b_self, b_end, b_weight;
TORCH_CHECK(weight.dim() <= std::max(self.dim(), end.dim()),
"weight should be of dimension max(self.dim(), end.dim()) or lesser");
std::tie(b_self, b_end, b_weight) = expand_outplace(self, end, weight, "lerp_out_cuda");
lerp_cuda(result, b_self, b_end, b_weight);
return result;
}
Tensor& lerp_cuda_scalar_out(Tensor& result, const Tensor& self,
const Tensor& end, Scalar weight) {
Tensor b_self, b_end;
std::tie(b_self, b_end) = expand_outplace(self, end, "lerp_out_cuda");
AT_DISPATCH_FLOATING_TYPES_AND_HALF(self.scalar_type(), "lerp_out_cuda", [&]{
lerp_scalar_cuda<scalar_t>(result, b_self, b_end, weight.to<scalar_t>());
});
return result;
}
Tensor& lerp_cuda_tensor_(Tensor& self, const Tensor& end, const Tensor& weight) {
Tensor b_self, b_end, b_weight;
std::tie(b_self, b_end, b_weight) = expand_outplace(self, end, weight, "lerp__cuda");
TORCH_CHECK(b_self.sizes() == self.sizes(),
"output with shape ", self.sizes(),
" doesn't match the broadcast shape ", b_self.sizes());
TORCH_CHECK(weight.dim() <= std::max(self.dim(), end.dim()),
"weight should be of dimension max(self.dim(), end.dim()) or lesser");
lerp_cuda(self, b_self, b_end, b_weight);
return self;
}
Tensor& lerp_cuda_scalar_(Tensor& self, const Tensor& end, Scalar weight) {
Tensor b_self, b_end;
std::tie(b_self, b_end) = expand_outplace(self, end, "lerp__cuda");
TORCH_CHECK(b_self.sizes() == self.sizes(),
"output with shape ", self.sizes(),
" doesn't match the broadcast shape ", b_self.sizes());
AT_DISPATCH_FLOATING_TYPES_AND_HALF(self.scalar_type(), "lerp__cuda", [&]{
lerp_scalar_cuda<scalar_t>(self, b_self, b_end, weight.to<scalar_t>());
});
return self;
}
Tensor lerp_cuda_tensor(const Tensor& self, const Tensor& end, const Tensor& weight) {
Tensor b_self, b_end, b_weight;
TORCH_CHECK(weight.dim() <= std::max(self.dim(), end.dim()),
"weight should be of dimension max(self.dim(), end.dim()) or lesser");
std::tie(b_self, b_end, b_weight) = expand_outplace(self, end, weight, "lerp_cuda");
Tensor result = at::empty_like(b_self, b_self.suggest_memory_format());
lerp_cuda(result, b_self, b_end, b_weight);
return result;
}
Tensor lerp_cuda_scalar(const Tensor& self, const Tensor& end, Scalar weight) {
Tensor b_self, b_end;
std::tie(b_self, b_end) = expand_outplace(self, end, "lerp_cuda");
Tensor result = at::empty_like(b_self, b_self.suggest_memory_format());
AT_DISPATCH_FLOATING_TYPES_AND_HALF(self.scalar_type(), "lerp_cuda", [&]{
lerp_scalar_cuda<scalar_t>(result, b_self, b_end, weight.to<scalar_t>());
});
return result;
}
} // namespace native
} // namespace at