forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TensorFactories.cpp
728 lines (600 loc) · 21.7 KB
/
TensorFactories.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
// define constants like M_PI and C keywords for MSVC
#ifdef _MSC_VER
#define _USE_MATH_DEFINES
#include <math.h>
#endif
#include <ATen/ATen.h>
#include <ATen/CPUGenerator.h>
#include <ATen/CheckGenerator.h>
#include <ATen/Dispatch.h>
#include <ATen/NativeFunctions.h>
#include <ATen/LegacyTHFunctions.h>
#include <ATen/LegacyTHDispatcher.h>
#include <c10/core/ScalarType.h>
#include <ATen/core/Deprecated.h>
#include <ATen/native/Resize.h>
#include <ATen/native/TensorFactories.h>
#include <c10/core/TensorOptions.h>
#include <TH/THRandom.h>
#include <TH/THGenerator.hpp>
#include <c10/util/Exception.h>
#include <algorithm>
#include <cmath>
#include <cstddef>
namespace at {
namespace native {
namespace {
void window_function_checks(
const char* function_name,
const TensorOptions& options,
int64_t window_length) {
AT_CHECK(
options.layout() != kSparse,
function_name,
" is not implemented for sparse types, got: ",
options);
AT_CHECK(
at::isFloatingType(typeMetaToScalarType(options.dtype())),
function_name,
" expects floating point dtypes, got: ",
options);
AT_CHECK(
window_length >= 0,
function_name,
" requires non-negative window_length, got window_length=",
window_length);
}
// FIXME: point to LegacyTHDispatcher.
const TypeExtendedInterface& getFactoryType(const TensorOptions& options) {
return at::getType(options);
}
} // namespace
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ arange ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tensor arange(Scalar end, const TensorOptions& options) {
return native::arange(/*start=*/0, end, options);
}
Tensor arange(Scalar start, Scalar end, const TensorOptions& options) {
return native::arange(start, end, /*step=*/1, options);
}
Tensor arange(
Scalar start,
Scalar end,
Scalar step,
const TensorOptions& options) {
Tensor result = at::empty({0}, options); // to be filled by arange_out
return at::arange_out(result, start, end, step);
}
Tensor& arange_out(Tensor& result, Scalar end) {
return at::arange_out(result, /*start=*/0, end);
}
Tensor& arange_out(Tensor& result, Scalar start, Scalar end) {
return at::arange_out(result, start, end, /*step=*/1);
}
Tensor _dim_arange(const Tensor& like, int64_t dim) {
return at::arange(like.size(dim), like.options().dtype(at::kLong));
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ empty ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tensor empty_cpu(IntList size, const TensorOptions& options) {
AT_ASSERT(options.backend() == Backend::CPU);
AT_ASSERT(!options.is_variable()); // is_variable should have been 'unpacked' // TODO: remove this when Variable and Tensor are merged
auto* allocator = at::getCPUAllocator();
int64_t nelements = prod_intlist(size);
auto dtype = options.dtype();
auto storage_impl = c10::make_intrusive<StorageImpl>(
dtype,
nelements,
allocator->allocate(nelements * dtype.itemsize()),
allocator,
/*resizeable=*/true);
auto tensor = detail::make_tensor<TensorImpl>(storage_impl, at::CPUTensorId(), false);
// Default TensorImpl has size [0]
if (size.size() != 1 || size[0] != 0) {
tensor.unsafeGetTensorImpl()->set_sizes_contiguous(size);
}
return tensor;
}
Tensor empty_strided_cpu(IntList size, IntList stride, const TensorOptions& options) {
auto t = at::native::empty_cpu({0}, options);
at::native::resize_impl_cpu_(t.unsafeGetTensorImpl(), size, stride);
return t;
}
Tensor& empty_out(Tensor& result, IntList size) {
if (result.is_sparse()) {
result.sparse_resize_and_clear_(size, size.size(), 0);
} else {
result.resize_(size);
}
return result;
}
// Temporary type cast operators. These are needed to trace type-casts now since
// Type's are not supported in the IR. Instead, we call down to these
// specialized operators for each datatype.
// TODO: remove when we have Type support in the IR
#define DEFINE_CAST_OP(_1, n, _2) \
Tensor _cast_##n(const Tensor& self, bool non_blocking) { \
auto& target_type = self.type().toScalarType(ScalarType::n); \
if (self.type() == target_type) \
return self; \
return target_type.copy(self, non_blocking); \
}
AT_FORALL_SCALAR_TYPES(DEFINE_CAST_OP)
#undef DEFINE_CAST_OP
Tensor empty_like(const Tensor& self) {
return native::empty_like(self, self.options());
}
Tensor empty_like(const Tensor& self, const TensorOptions& options) {
if (options.layout() == kSparse && self.is_sparse()) {
auto res = at::empty({0}, options); // to be resized
res.sparse_resize_and_clear_(self.sizes(), self.sparse_dim(), self.dense_dim());
return res;
}
return at::empty(self.sizes(), options);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ eye ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tensor eye(int64_t n, const TensorOptions& options) {
return native::eye(n, -1, options);
}
Tensor eye(int64_t n, int64_t m, const TensorOptions& options) {
auto tensor = at::empty({0}, options); // to be resized
return at::eye_out(tensor, n, m);
}
Tensor& eye_out_cpu(Tensor& result, int64_t n) {
return native::eye_out_cpu(result, n, -1);
}
Tensor& eye_out_cpu(Tensor& result, int64_t n, int64_t m) {
AT_CHECK(n >= 0, "n must be greater or equal to 0, got ", n);
if(m < 0) {
m = n;
}
result.resize_({n, m});
result.zero_();
int64_t sz = std::min<int64_t>(n, m);
AT_DISPATCH_ALL_TYPES(result.type(), "eye", [&]() -> void {
scalar_t* result_data = result.data<scalar_t>();
for(int64_t i = 0; i < sz; i++) {
result_data[i*(result.strides()[0] + result.strides()[1])] = 1;
}
});
return result;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ full ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tensor full(IntList size, Scalar fill_value, const TensorOptions& options) {
if (options.layout() == kSparse) {
AT_ERROR("full(...) is not implemented for sparse layout");
}
auto result = at::empty(size, options);
return result.fill_(fill_value);
}
Tensor& full_out(Tensor& result, IntList size, Scalar fill_value) {
if (result.is_sparse()) {
AT_ERROR("full(...) is not implemented for sparse layout");
}
result.resize_(size);
return result.fill_(fill_value);
}
Tensor full_like(const Tensor& self, Scalar fill_value) {
return native::full_like(self, fill_value, self.options());
}
Tensor full_like(const Tensor& self, Scalar fill_value, const TensorOptions& options) {
return native::full(self.sizes(), fill_value, options);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ linspace ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tensor linspace(
Scalar start,
Scalar end,
int64_t steps,
const TensorOptions& options) {
Tensor result = at::empty({steps}, options);
return at::linspace_out(result, start, end, steps);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ logspace ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tensor logspace(
Scalar start,
Scalar end,
int64_t steps,
const TensorOptions& options) {
Tensor result = at::empty({steps}, options);
return at::logspace_out(result, start, end, steps);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ones ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tensor ones(IntList size, const TensorOptions& options) {
return native::full(size, /*fill_value=*/1, options);
}
Tensor& ones_out(Tensor& result, IntList size) {
return native::full_out(result, size, /*fill_value=*/1);
}
Tensor ones_like(const Tensor& self) {
return native::ones(self.sizes(), self.options());
}
Tensor ones_like(const Tensor& self, const TensorOptions& options) {
return native::ones(self.sizes(), options);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ scalar_tensor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tensor scalar_tensor(Scalar s, const TensorOptions& options) {
return at::empty({}, options).fill_(s);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ rand ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tensor rand(IntList size, const TensorOptions& options) {
return native::rand(size, nullptr, options);
}
Tensor rand(IntList size, Generator* generator, const TensorOptions& options) {
auto result = at::empty(size, options);
return result.uniform_(0, 1, generator);
}
Tensor& rand_out(Tensor& result, IntList size) {
return native::rand_out(result, size, nullptr);
}
Tensor& rand_out(Tensor& result, IntList size, Generator* generator) {
result.resize_(size);
return result.uniform_(0, 1, generator);
}
Tensor rand_like(const Tensor& self) {
return native::rand_like(self, self.options());
}
Tensor rand_like(const Tensor& self, const TensorOptions& options) {
return native::rand(self.sizes(), options);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ randint ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tensor randint(int64_t high, IntList size, const TensorOptions& options) {
return native::randint(high, size, nullptr, options);
}
Tensor randint(
int64_t high,
IntList size,
Generator* generator,
const TensorOptions& options) {
return native::randint(0, high, size, generator, options);
}
Tensor randint(
int64_t low,
int64_t high,
IntList size,
const TensorOptions& options) {
return native::randint(low, high, size, nullptr, options);
}
Tensor randint(
int64_t low,
int64_t high,
IntList size,
Generator* generator,
const TensorOptions& options) {
auto result = at::empty(size, options);
return result.random_(low, high, generator);
}
Tensor& randint_out(Tensor& result, int64_t high, IntList size) {
return native::randint_out(result, high, size, nullptr);
}
Tensor& randint_out(
Tensor& result,
int64_t high,
IntList size,
Generator* generator) {
result.resize_(size);
return result.random_(0, high, generator);
}
Tensor& randint_out(Tensor& result, int64_t low, int64_t high, IntList size) {
return native::randint_out(result, low, high, size, nullptr);
}
Tensor& randint_out(
Tensor& result,
int64_t low,
int64_t high,
IntList size,
Generator* generator) {
result.resize_(size);
return result.random_(low, high, generator);
}
Tensor randint_like(const Tensor& self, int64_t high) {
return native::randint_like(self, high, self.options());
}
Tensor randint_like(const Tensor& self, int64_t low, int64_t high) {
return native::randint_like(self, low, high, self.options());
}
Tensor randint_like(
const Tensor& self,
int64_t high,
const TensorOptions& options) {
return native::randint(high, self.sizes(), nullptr, options);
}
Tensor randint_like(
const Tensor& self,
int64_t low,
int64_t high,
const TensorOptions& options) {
return native::randint(low, high, self.sizes(), nullptr, options);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ randn ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tensor randn(IntList size, const TensorOptions& options) {
return native::randn(size, nullptr, options);
}
Tensor randn(IntList size, Generator* generator, const TensorOptions& options) {
auto result = at::empty(size, options);
return result.normal_(0, 1, generator);
}
Tensor& randn_out(Tensor& result, IntList size) {
return native::randn_out(result, size, nullptr);
}
Tensor& randn_out(Tensor& result, IntList size, Generator* generator) {
result.resize_(size);
return result.normal_(0, 1, generator);
}
Tensor randn_like(const Tensor& self) {
return native::randn_like(self, self.options());
}
Tensor randn_like(const Tensor& self, const TensorOptions& options) {
return native::randn(self.sizes(), nullptr, options);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ randperm ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
namespace {
template <typename scalar_t>
void randperm_cpu(Tensor& result, int64_t n, THGenerator* generator) {
std::lock_guard<std::mutex> lock(generator->mutex);
scalar_t *r__data = result.data<scalar_t>();
result.resize_({n});
int64_t r__stride_0 = result.stride(0);
for(int64_t i = 0; i < n; i++) {
r__data[i*r__stride_0] = static_cast<scalar_t>(i);
}
for(int64_t i = 0; i < n - 1; i++)
{
int64_t z = THRandom_random(generator) % (n-i);
scalar_t sav = r__data[i*r__stride_0];
r__data[i*r__stride_0] = r__data[(z+i)*r__stride_0];
r__data[(z+i)*r__stride_0] = sav;
}
}
} // namespace
THGenerator* get_generator(at::Generator* gen) {
auto default_gen = &at::globalContext().defaultGenerator(at::kCPU);
auto gen_ = at::check_generator<at::CPUGenerator>(gen, default_gen);
return gen_->generator;
}
Tensor randperm(int64_t n, const TensorOptions& options) {
return native::randperm(n, nullptr, options);
}
Tensor randperm(int64_t n, Generator* generator, const TensorOptions& options) {
auto tensor = at::empty(n, options);
return at::randperm_out(tensor, n, generator);
}
Tensor& randperm_out(Tensor& result, int64_t n) {
return at::randperm_out(result, n, nullptr);
}
Tensor& randperm_out_cpu(Tensor& result, int64_t n, Generator* generator) {
AT_CHECK(n >= 0, "n must be non-negative, got", n);
result.resize_({n});
auto gen = get_generator(generator);
AT_DISPATCH_ALL_TYPES(result.type(), "randperm", [&]() -> void {
randperm_cpu<scalar_t>(result, n, gen);
});
return result;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ range ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tensor range(
Scalar start,
Scalar end,
Scalar step,
const TensorOptions& options) {
Tensor result = at::empty({0}, options);
return at::range_out(result, start, end, step);
}
Tensor range(
Scalar start,
Scalar end,
const TensorOptions& options) {
return at::native::range(start, end, 1, options);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ triangle ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tensor tril_indices_cpu(
int64_t row, int64_t col, int64_t offset, const TensorOptions& options) {
check_args(row, col, options);
auto tril_size = get_tril_size(row, col, offset);
// create an empty Tensor with correct size
auto result = at::empty({2, tril_size}, options);
// The following three approaches result in very little performance
// differences. Hence, the 2nd option is taken for simpler code, and to return
// contiguous tensors. Refer to #14904 for more details.
//
// 1. sequential RAM access: fill row coordinates first, then columns. This
// results in two for-loop and more arithmetic operations.
//
// 2. interleaved RAM access: fill in index coordinates one by one, which
// jumps between the two output Tensor rows in every iteration.
//
// 3. sequential RAM + transpose: create an n X 2 Tensor, fill the Tensor
// sequentially, and then transpose it.
AT_DISPATCH_ALL_TYPES(result.type(), "tril_indices", [&]() -> void {
// fill the Tensor with correct values
scalar_t* result_data = result.data<scalar_t>();
int64_t i = 0;
scalar_t r = std::max<int64_t>(0, -offset), c = 0;
while (i < tril_size) {
result_data[i] = r;
result_data[tril_size + i++] = c;
// move to the next column and check if (r, c) is still in bound
c += 1;
if (c > r + offset || c >= col) {
r += 1;
c = 0;
// NOTE: not necessary to check if r is less than row here, because i
// and tril_size provide the guarantee
}
}
});
return result;
}
Tensor triu_indices_cpu(
int64_t row, int64_t col, int64_t offset, const TensorOptions& options) {
check_args(row, col, options);
auto triu_size = row * col - get_tril_size(row, col, offset - 1);
// create an empty Tensor with correct size
auto result = at::empty({2, triu_size}, options);
AT_DISPATCH_ALL_TYPES(result.type(), "triu_indices", [&]() -> void {
// fill the Tensor with correct values
scalar_t* result_data = result.data<scalar_t>();
int64_t i = 0;
// not typing std::max with scalar_t as it could be an unsigned type
// NOTE: no need to check if the returned value of std::max overflows
// scalar_t, as i and triu_size act as a guard.
scalar_t c = std::max<int64_t>(0, offset), r = 0;
while (i < triu_size) {
result_data[i] = r;
result_data[triu_size + i++] = c;
// move to the next column and check if (r, c) is still in bound
c += 1;
if (c >= col) {
r += 1;
// not typing std::max with scalar_t as it could be an unsigned type
// NOTE: not necessary to check if c is less than col or overflows here,
// because i and triu_size act as a guard.
c = std::max<int64_t>(0, r + offset);
}
}
});
return result;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ zeros ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tensor zeros(IntList size, const TensorOptions& options) {
auto result = at::empty(size, options);
return result.zero_();
}
Tensor& zeros_out(Tensor& result, IntList size) {
if (result.is_sparse()) {
result.sparse_resize_and_clear_(size, size.size(), 0);
return result;
} else {
result.resize_(size);
}
return result.zero_();
}
Tensor zeros_like(const Tensor& self) {
return native::zeros_like(self, self.options());
}
Tensor zeros_like(const Tensor& self, const TensorOptions& options) {
if (options.layout() == kSparse && self.is_sparse()) {
auto res = at::empty({0}, options); // to be resized
res.sparse_resize_and_clear_(self.sizes(), self.sparse_dim(), self.dense_dim());
return res;
}
return native::zeros(self.sizes(), options);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~ bartlett_window ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tensor bartlett_window(int64_t window_length, const TensorOptions& options) {
return native::bartlett_window(window_length, /*periodic=*/true, options);
}
Tensor bartlett_window(
int64_t window_length,
bool periodic,
const TensorOptions& options) {
window_function_checks("bartlett_window", options, window_length);
if (window_length == 0) {
return at::empty({0}, options);
}
if (window_length == 1) {
return native::ones({1}, options);
}
if (periodic) {
window_length += 1;
}
auto window = native::arange(window_length, options).mul_(2. / static_cast<double>(window_length - 1));
const int64_t first_half_size = ((window_length - 1) >> 1) + 1;
window.narrow(0, first_half_size, window_length - first_half_size).mul_(-1).add_(2);
return periodic ? window.narrow(0, 0, window_length - 1) : window;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~ blackman_window ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tensor blackman_window(int64_t window_length, const TensorOptions& options) {
return native::blackman_window(window_length, /*periodic=*/true, options);
}
Tensor blackman_window(
int64_t window_length,
bool periodic,
const TensorOptions& options) {
window_function_checks("blackman_window", options, window_length);
if (window_length == 1) {
return native::ones({1}, options);
}
if (periodic) {
window_length += 1;
}
// from https://en.wikipedia.org/wiki/Window_function#Blackman_window
auto window = native::arange(window_length, options).mul_(M_PI / static_cast<double>(window_length - 1));
window = window.mul(4).cos_().mul_(0.08) - window.mul(2).cos_().mul_(0.5) + 0.42;
return periodic ? window.narrow(0, 0, window_length - 1) : window;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ hamming_window ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tensor hamming_window(int64_t window_length, const TensorOptions& options) {
return native::hamming_window(window_length, /*periodic=*/true, options);
}
Tensor hamming_window(
int64_t window_length,
bool periodic,
const TensorOptions& options) {
return native::hamming_window(
window_length, periodic, /*alpha=*/0.54, options);
}
Tensor hamming_window(
int64_t window_length,
bool periodic,
double alpha,
const TensorOptions& options) {
return native::hamming_window(
window_length, periodic, alpha, /*beta=*/0.46, options);
}
Tensor hamming_window(
int64_t window_length,
bool periodic,
double alpha,
double beta,
const TensorOptions& options) {
window_function_checks("hamming_window", options, window_length);
if (window_length == 0) {
return at::empty({0}, options);
}
if (window_length == 1) {
return native::ones({1}, options);
}
if (periodic) {
window_length += 1;
}
auto window = native::arange(window_length, options);
window.mul_(M_PI * 2. / static_cast<double>(window_length - 1)).cos_().mul_(-beta).add_(alpha);
return periodic ? window.narrow(0, 0, window_length - 1) : window;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ hann_window ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tensor hann_window(int64_t window_length, const TensorOptions& options) {
return native::hann_window(window_length, /*periodic=*/true, options);
}
Tensor hann_window(
int64_t window_length,
bool periodic,
const TensorOptions& options) {
window_function_checks("hann_window", options, window_length);
return native::hamming_window(
window_length, periodic, /*alpha=*/0.5, /*beta=*/0.5, options);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tensor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
template <typename T>
Tensor tensor_cpu(ArrayRef<T> values, const TensorOptions& options) {
auto result = at::empty(values.size(), options);
AT_ASSERT(result.is_contiguous());
AT_DISPATCH_ALL_TYPES(result.type(), "tensor_cpu", [&] {
std::copy(values.begin(), values.end(), result.template data<scalar_t>());
});
return result;
}
template <typename T>
Tensor tensor_cuda(ArrayRef<T> values, const TensorOptions& options) {
auto cpu_tensor = tensor_cpu(values, options.device(DeviceType::CPU));
return cpu_tensor.to(options.device());
}
#define TENSOR(T, _1, _2) \
Tensor tensor(ArrayRef<T> values, const TensorOptions& options) { \
if (options.device().is_cuda()) { \
return tensor_cuda(values, options); \
} else { \
return tensor_cpu(values, options); \
} \
}
AT_FORALL_SCALAR_TYPES_EXCEPT_HALF(TENSOR)
#undef TENSOR
} // namespace native
} // namespace at