Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Aug 20, 2026. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresize.cpp
More file actions
Latest commit
216 lines (196 loc) · 10.9 KB
/
Copy pathresize.cpp
File metadata and controls
216 lines (196 loc) · 10.9 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include<stdexcept>
#include<cstdint>
#include"resize.h"
#include"autocast.h"
#include"dlpack.h"
#include"impl/kernels/cuda_switch.h"
#include"impl/kernels/utils.h"
#include"impl/resize.h"
FF_NAMESPACE_BEGIN(FF)
FF_NAMESPACE_BEGIN(FF_DEVICE)
#defineVOIDPTR(x) (static_cast<void*>(static_cast<char*>(x.data) + x.byte_offset))
#defineCANUSE32BITS(x) (canUse32BitIndexMath(x.ndim, x.shape, x.strides))
/***********************************************************************
* CHECKS *
***********************************************************************/
#defineCHECK_NO_LANES(tensor) \
if (tensor.dtype.lanes > 1) \
throwstd::invalid_argument( \
"Only scalar data types are supported" \
);
#defineCHECK_SAME(X, Y, msg) \
if (X != Y) throwstd::invalid_argument(msg);
#defineCHECK_SAME_DTYPE(X, Y) \
if ( \
(X.dtype.code != Y.dtype.code) || \
(X.dtype.bits != Y.dtype.bits) || \
(X.dtype.lanes != Y.dtype.lanes) \
) \
throwstd::invalid_argument( \
"Tensors do not have the same data type" \
);
#defineCHECK_SAME_BATCH(X, Y, D) \
for (int32_t d=0; d < D; ++d) \
if (X.shape[d] != Y.shape[d]) \
throwstd::invalid_argument( \
"Tensors do not have the same batch shape" \
);
/***********************************************************************
* DISPATCH *
***********************************************************************/
namespace {
template <int ndim, spline::type I, bound::type B, typenamescalar_t, typenameoffset_t>
inlinevoid_resample(
int64_t nbatch , // number of batch dimensions
void * out , // (*batch, *outshape) tensor
constvoid * inp , // (*batch, *inshape) tensor
double shift , // anchor shift
constdouble * scale , // [ndim] scaling
constint64_t * size_out , // [nall] output shape
constint64_t * size_inp , // [nall] input shape
constint64_t * stride_out , // [nall] output strides
constint64_t * stride_inp ) // [nall] input strides
{
constint64_t nall = nbatch + ndim; // == out.ndim == inp.ndim
constoffset_t * _size_out = copy_if_needed<offset_t *>(size_out, nall);
constoffset_t * _size_inp = copy_if_needed<offset_t *>(size_inp, nall);
constoffset_t * _stride_out = copy_if_needed<offset_t *>(stride_out, nall);
constoffset_t * _stride_inp = copy_if_needed<offset_t *>(stride_inp, nall);
scalar_t * _out = static_cast< scalar_t *>(out);
constscalar_t * _inp = static_cast<constscalar_t *>(inp);
// When no scaling is provided, default to the input/output size ratio
// (identity when the shapes match). The kernel dereferences scale[d]
// unconditionally, so a null pointer would otherwise segfault.
constdouble * _scale = scale;
double default_scale[ndim];
if (!_scale) {
for (int d = 0; d < ndim; ++d)
default_scale[d] = static_cast<double>(size_inp[nbatch + d])
/ static_cast<double>(size_out[nbatch + d]);
_scale = default_scale;
}
resize::loop<ndim, scalar_t, offset_t, double, I, B>(
static_cast<offset_t>(nbatch), _out, _inp, shift, _scale,
_size_out, _size_inp, _stride_out, _stride_inp);
free_if_needed<int64_t *>(_size_out);
free_if_needed<int64_t *>(_size_inp);
free_if_needed<int64_t *>(_stride_out);
free_if_needed<int64_t *>(_stride_inp);
}
} // anonymous namespace
#defineRS_DTYPE(D, I, B, args...) \
switch (code) { \
casekDLFloat: switch (inp.dtype.bits) { \
case32: return ( \
use_32bits ? _resample<D,I,B,float, int32_t>(args) \
: _resample<D,I,B,float, int64_t>(args)); \
case64: return ( \
use_32bits ? _resample<D,I,B,double,int32_t>(args) \
: _resample<D,I,B,double,int64_t>(args)); \
default: break; \
}; \
default: break; \
}
#defineRS_BOUND(D, I, args...) \
switch (bnd) { \
casebound_t::Zero: RS_DTYPE(D, I, bound_t::Zero, args); break; \
casebound_t::Replicate: RS_DTYPE(D, I, bound_t::Replicate, args); break; \
casebound_t::DCT1: RS_DTYPE(D, I, bound_t::DCT1, args); break; \
casebound_t::DCT2: RS_DTYPE(D, I, bound_t::DCT2, args); break; \
casebound_t::DST1: RS_DTYPE(D, I, bound_t::DST1, args); break; \
casebound_t::DST2: RS_DTYPE(D, I, bound_t::DST2, args); break; \
casebound_t::DFT: RS_DTYPE(D, I, bound_t::DFT, args); break; \
casebound_t::NoCheck: RS_DTYPE(D, I, bound_t::NoCheck, args); break; \
default: throwstd::invalid_argument("Unsupported boundary condition"); \
}
// Fast-test builds (`-DFF_TEST_SPARSE`) instantiate a covering subset of the
// order x bound matrix: all bounds for Linear + Cubic, DCT2 only for the rest.
// The library / CI build keeps the full matrix (also the compile gate).
#ifdef FF_TEST_SPARSE
#defineRS_BOUND_SPARSE(D, I, args...) \
switch (bnd) { \
casebound_t::DCT2: RS_DTYPE(D, I, bound_t::DCT2, args); break; \
default: throwstd::invalid_argument( \
"bound not instantiated in FF_TEST_SPARSE build"); \
}
#defineRS_ORDER(D, args...) \
switch (spl) { \
casespline_t::Nearest: RS_BOUND_SPARSE(D, spline_t::Nearest, args); break; \
casespline_t::Linear: RS_BOUND(D, spline_t::Linear, args); break; \
casespline_t::Quadratic: RS_BOUND_SPARSE(D, spline_t::Quadratic, args); break; \
casespline_t::Cubic: RS_BOUND(D, spline_t::Cubic, args); break; \
casespline_t::FourthOrder: RS_BOUND_SPARSE(D, spline_t::FourthOrder, args); break; \
casespline_t::FifthOrder: RS_BOUND_SPARSE(D, spline_t::FifthOrder, args); break; \
casespline_t::SixthOrder: RS_BOUND_SPARSE(D, spline_t::SixthOrder, args); break; \
casespline_t::SeventhOrder: RS_BOUND_SPARSE(D, spline_t::SeventhOrder, args); break; \
default: throwstd::invalid_argument("Unsupported spline order"); \
}
#else
#defineRS_ORDER(D, args...) \
switch (spl) { \
casespline_t::Nearest: RS_BOUND(D, spline_t::Nearest, args); break; \
casespline_t::Linear: RS_BOUND(D, spline_t::Linear, args); break; \
casespline_t::Quadratic: RS_BOUND(D, spline_t::Quadratic, args); break; \
casespline_t::Cubic: RS_BOUND(D, spline_t::Cubic, args); break; \
casespline_t::FourthOrder: RS_BOUND(D, spline_t::FourthOrder, args); break; \
casespline_t::FifthOrder: RS_BOUND(D, spline_t::FifthOrder, args); break; \
casespline_t::SixthOrder: RS_BOUND(D, spline_t::SixthOrder, args); break; \
casespline_t::SeventhOrder: RS_BOUND(D, spline_t::SeventhOrder, args); break; \
default: throwstd::invalid_argument("Unsupported spline order"); \
}
#endif
#defineDISPATCH_RESIZE(args...) \
{ \
constbool use_32bits = CANUSE32BITS(out) && CANUSE32BITS(inp); \
constauto code = static_cast<DLDataTypeCode>(inp.dtype.code); \
constspline_t spl = static_cast<spline_t>(spline); \
constbound_t bnd = static_cast<bound_t >(bound); \
switch (ndim) { \
case1: RS_ORDER(1, args); break; \
case2: RS_ORDER(2, args); break; \
case3: RS_ORDER(3, args); break; \
default: throwstd::invalid_argument( \
"Only 1D, 2D and 3D resize are supported"); \
}; \
/* Reached only when a valid dim/spline/bound had an unsupported \
dtype: the RS_DTYPE switch fell through without returning. */ \
throwstd::invalid_argument( \
"Unsupported data type for resample (only float32/float64)"); \
}
voidresample(
DLTensor & out_ ,
const DLTensor & inp_ ,
int8_t spline ,
int8_t bound ,
double shift ,
constdouble * scale ,
int ndim ,
intptr_t/* stream <unused> */
)
{
// Normalise a NULL strides field (compact row-major) so the dispatch and
// impl loops below can dereference strides unconditionally.
ContiguousStrides _out(out_), _inp(inp_);
DLTensor & out = _out.t;
DLTensor & inp = _inp.t;
constint32_t nbatch = out.ndim - ndim;
CHECK_NO_LANES (out)
CHECK_SAME_DTYPE(out, inp)
CHECK_SAME (out.ndim, inp.ndim, "Tensors do not have the same number of dimensions")
if (nbatch < 0)
throwstd::invalid_argument("ndim is larger than the tensor rank");
CHECK_SAME_BATCH(out, inp, nbatch)
DISPATCH_RESIZE(
static_cast<int64_t>(nbatch),
VOIDPTR(out),
VOIDPTR(inp),
shift,
scale,
out.shape,
inp.shape,
out.strides,
inp.strides
)
}
FF_NAMESPACE_END(FF_DEVICE)
FF_NAMESPACE_END(FF)