-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathTHSTorch.cpp
More file actions
544 lines (456 loc) · 13.1 KB
/
THSTorch.cpp
File metadata and controls
544 lines (456 loc) · 13.1 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
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
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. See LICENSE in the project root for license information.
#include "THSTorch.h"
#include "torch/torch.h"
#include "torch/cuda.h"
#include <c10/core/CachingDeviceAllocator.h>
#if defined(USE_CUDA)
#include <c10/cuda/CUDACachingAllocator.h>
#include <c10/cuda/CUDAGuard.h>
#include <c10/cuda/CUDAFunctions.h>
#endif
void THSTorch_manual_seed(const int64_t seed)
{
torch::manual_seed(seed);
}
Generator THSGenerator_manual_seed(const int64_t seed)
{
auto gen = at::globalContext().defaultGenerator(at::DeviceType::CPU);
gen.set_current_seed(seed);
return new at::Generator(gen.getIntrusivePtr());
}
void THSCuda_manual_seed(const int64_t seed)
{
CATCH(torch::cuda::manual_seed(seed);)
}
void THSCuda_manual_seed_all(const int64_t seed)
{
CATCH(torch::cuda::manual_seed_all(seed);)
}
bool THSBackend_cublas_get_allow_tf32()
{
auto result = false;
CATCH(result = at::globalContext().allowTF32CuBLAS(););
return result;
}
void THSBackend_cublas_set_allow_tf32(const bool flag)
{
CATCH(at::globalContext().setAllowTF32CuBLAS(flag););
}
bool THSBackend_cudnn_get_allow_tf32()
{
auto result = false;
CATCH(result = at::globalContext().allowTF32CuDNN(););
return result;
}
void THSBackend_cudnn_set_allow_tf32(const bool flag)
{
CATCH(at::globalContext().setAllowTF32CuDNN(flag););
}
bool THSBackend_cuda_get_allow_fp16_reduced_precision_reduction()
{
auto result = false;
CATCH(result = at::globalContext().allowFP16ReductionCuBLAS() == at::CuBLASReductionOption::AllowReducedPrecisionWithSplitK;);
return result;
}
void THSBackend_cuda_set_allow_fp16_reduced_precision_reduction(const bool flag)
{
CATCH(at::globalContext().setAllowFP16ReductionCuBLAS(flag, true););
}
bool THSBackend_cuda_get_enable_flash_sdp()
{
auto result = false;
CATCH(result = at::globalContext().userEnabledFlashSDP(););
return result;
}
void THSBackend_cuda_set_enable_flash_sdp(const bool flag)
{
CATCH(at::globalContext().setSDPUseFlash(flag););
}
bool THSBackend_cuda_get_enable_math_sdp()
{
auto result = false;
CATCH(result = at::globalContext().userEnabledMathSDP(););
return result;
}
void THSBackend_cuda_set_enable_math_sdp(const bool flag)
{
CATCH(at::globalContext().setSDPUseMath(flag););
}
void THSGenerator_gen_manual_seed(const Generator generator, const int64_t seed)
{
generator->set_current_seed(seed);
}
Generator THSGenerator_default_generator()
{
auto gen = at::globalContext().defaultGenerator(at::DeviceType::CPU);
return new at::Generator(gen.getIntrusivePtr());
}
int64_t THSGenerator_initial_seed(const Generator gen)
{
return gen->current_seed();
}
Tensor THSGenerator_get_rng_state(const Generator gen)
{
CATCH_TENSOR(gen->get_state());
}
void THSGenerator_set_rng_state(const Generator gen, const Tensor tensor)
{
gen->set_state(*tensor);
}
Generator THSGenerator_new(uint64_t seed, int64_t device, int64_t index)
{
// TODO: Support creation of GPU RNGs. 'device' and 'index' are in the
// function signature in preparation thereof.
return new at::Generator(at::detail::createCPUGenerator(seed));
}
void THSGenerator_dispose(const Generator generator)
{
delete generator;
}
int THSTorchCuda_is_available()
{
return torch::cuda::is_available();
}
int THSTorchCuda_cudnn_is_available()
{
return torch::cuda::cudnn_is_available();
}
int THSTorchCuda_device_count()
{
return (int)torch::cuda::device_count();
}
void THSTorchCuda_synchronize(const int64_t device_index)
{
CATCH(torch::cuda::synchronize(device_index);)
}
#if defined(USE_CUDA)
// Helper to safely resolve CUDA device index, defaulting to device 0 when
// no device context has been established yet (c10::cuda::current_device()
// can throw "Invalid device argument" in that case).
static c10::DeviceIndex resolve_cuda_device_index(int64_t device_index)
{
if (device_index >= 0)
return static_cast<c10::DeviceIndex>(device_index);
c10::DeviceIndex current = -1;
auto err = c10::cuda::GetDevice(¤t);
if (err != cudaSuccess || current < 0) {
// No CUDA device context has been established yet; default to device 0.
cudaGetLastError(); // clear any error
return static_cast<c10::DeviceIndex>(0);
}
return current;
}
void THSTorchCuda_empty_cache()
{
CATCH(c10::cuda::CUDACachingAllocator::emptyCache();)
}
size_t THSTorchCuda_memory_allocated(const int64_t device_index)
{
size_t res = 0;
CATCH(
auto device = resolve_cuda_device_index(device_index);
res = c10::cuda::CUDACachingAllocator::currentMemoryAllocated(device);
)
return res;
}
size_t THSTorchCuda_max_memory_allocated(const int64_t device_index)
{
size_t res = 0;
CATCH(
auto device = resolve_cuda_device_index(device_index);
res = c10::cuda::CUDACachingAllocator::maxMemoryAllocated(device);
)
return res;
}
void THSTorchCuda_reset_peak_memory_stats(const int64_t device_index)
{
CATCH(
auto device = resolve_cuda_device_index(device_index);
c10::cuda::CUDACachingAllocator::resetPeakStats(device);
)
}
size_t THSTorchCuda_memory_reserved(const int64_t device_index)
{
size_t res = 0;
CATCH(
auto device = resolve_cuda_device_index(device_index);
res = c10::cuda::CUDACachingAllocator::currentMemoryReserved(device);
)
return res;
}
size_t THSTorchCuda_max_memory_reserved(const int64_t device_index)
{
size_t res = 0;
CATCH(
auto device = resolve_cuda_device_index(device_index);
res = c10::cuda::CUDACachingAllocator::maxMemoryReserved(device);
)
return res;
}
void THSTorchCuda_mem_get_info(const int64_t device_index, size_t* free, size_t* total)
{
CATCH(
auto device = resolve_cuda_device_index(device_index);
c10::cuda::CUDAGuard guard(device);
C10_CUDA_CHECK(cudaMemGetInfo(free, total));
)
}
void THSTorchCuda_set_device(const int64_t device_index)
{
CATCH(c10::cuda::set_device(static_cast<c10::DeviceIndex>(device_index));)
}
int64_t THSTorchCuda_current_device()
{
int64_t res = -1;
CATCH(res = static_cast<int64_t>(c10::cuda::current_device());)
return res;
}
#else
// Helper to resolve device index using torch::cuda APIs available without USE_CUDA
static c10::DeviceIndex resolve_cuda_device(int64_t device_index)
{
if (device_index < 0) {
// Use device 0 as default when CUDA headers aren't available
return 0;
}
return static_cast<c10::DeviceIndex>(device_index);
}
void THSTorchCuda_empty_cache()
{
CATCH(
if (torch::cuda::is_available()) {
auto* allocator = c10::getDeviceAllocator(c10::DeviceType::CUDA);
allocator->emptyCache();
}
)
}
size_t THSTorchCuda_memory_allocated(const int64_t device_index)
{
size_t res = 0;
CATCH(
if (torch::cuda::is_available()) {
auto device = resolve_cuda_device(device_index);
auto* allocator = c10::getDeviceAllocator(c10::DeviceType::CUDA);
auto stats = allocator->getDeviceStats(device);
res = static_cast<size_t>(stats.allocated_bytes[static_cast<size_t>(c10::CachingAllocator::StatType::AGGREGATE)].current);
}
)
return res;
}
size_t THSTorchCuda_max_memory_allocated(const int64_t device_index)
{
size_t res = 0;
CATCH(
if (torch::cuda::is_available()) {
auto device = resolve_cuda_device(device_index);
auto* allocator = c10::getDeviceAllocator(c10::DeviceType::CUDA);
auto stats = allocator->getDeviceStats(device);
res = static_cast<size_t>(stats.allocated_bytes[static_cast<size_t>(c10::CachingAllocator::StatType::AGGREGATE)].peak);
}
)
return res;
}
void THSTorchCuda_reset_peak_memory_stats(const int64_t device_index)
{
CATCH(
if (torch::cuda::is_available()) {
auto device = resolve_cuda_device(device_index);
auto* allocator = c10::getDeviceAllocator(c10::DeviceType::CUDA);
allocator->resetPeakStats(device);
}
)
}
size_t THSTorchCuda_memory_reserved(const int64_t device_index)
{
size_t res = 0;
CATCH(
if (torch::cuda::is_available()) {
auto device = resolve_cuda_device(device_index);
auto* allocator = c10::getDeviceAllocator(c10::DeviceType::CUDA);
auto stats = allocator->getDeviceStats(device);
res = static_cast<size_t>(stats.reserved_bytes[static_cast<size_t>(c10::CachingAllocator::StatType::AGGREGATE)].current);
}
)
return res;
}
size_t THSTorchCuda_max_memory_reserved(const int64_t device_index)
{
size_t res = 0;
CATCH(
if (torch::cuda::is_available()) {
auto device = resolve_cuda_device(device_index);
auto* allocator = c10::getDeviceAllocator(c10::DeviceType::CUDA);
auto stats = allocator->getDeviceStats(device);
res = static_cast<size_t>(stats.reserved_bytes[static_cast<size_t>(c10::CachingAllocator::StatType::AGGREGATE)].peak);
}
)
return res;
}
void THSTorchCuda_mem_get_info(const int64_t device_index, size_t* free, size_t* total)
{
*free = 0;
*total = 0;
CATCH(
if (torch::cuda::is_available()) {
auto device = resolve_cuda_device(device_index);
auto* allocator = c10::getDeviceAllocator(c10::DeviceType::CUDA);
auto info = allocator->getMemoryInfo(device);
*free = info.first;
*total = info.second;
}
)
}
void THSTorchCuda_set_device(const int64_t device_index)
{
}
int64_t THSTorchCuda_current_device()
{
return -1;
}
#endif
const char * THSTorch_get_and_reset_last_err()
{
char *tmp = torch_last_err;
torch_last_err = nullptr;
return tmp;
}
int THSTorch_get_num_threads()
{
CATCH_RETURN_RES(int, -1, res = torch::get_num_threads());
}
void THSTorch_set_num_threads(const int threads)
{
torch::set_num_threads(threads);
}
int THSTorch_get_num_interop_threads()
{
CATCH_RETURN_RES(int, -1, res = torch::get_num_interop_threads());
}
void THSTorch_set_num_interop_threads(const int threads)
{
torch::set_num_interop_threads(threads);
}
int THSTorch_can_cast(const int type1, const int type2)
{
CATCH_RETURN_RES(int, -1, res = (int)torch::can_cast((c10::ScalarType)type1, (c10::ScalarType)type2));
}
int THSTorch_promote_types(const int type1, const int type2)
{
CATCH_RETURN_RES(int, -1, res = (int)torch::promote_types((c10::ScalarType)type1, (c10::ScalarType)type2));
}
Scalar THSTorch_int8_to_scalar(int8_t value)
{
return new torch::Scalar(value);
}
Scalar THSTorch_uint8_to_scalar(uint8_t value)
{
return new torch::Scalar(value);
}
Scalar THSTorch_int16_to_scalar(int16_t value)
{
return new torch::Scalar(value);
}
Scalar THSTorch_int32_to_scalar(int32_t value)
{
return new torch::Scalar(value);
}
Scalar THSTorch_int64_to_scalar(int64_t value)
{
return new torch::Scalar(value);
}
Scalar THSTorch_float32_to_scalar(float value)
{
return new torch::Scalar(value);
}
Scalar THSTorch_float64_to_scalar(double value)
{
return new torch::Scalar(value);
}
Scalar THSTorch_float16_to_scalar(float value)
{
return new torch::Scalar((c10::Half)value);
}
Scalar THSTorch_bfloat16_to_scalar(float value)
{
return new torch::Scalar((c10::BFloat16)value);
}
Scalar THSTorch_bool_to_scalar(bool value)
{
return new torch::Scalar(value);
}
Scalar THSTorch_complex32_to_scalar(float real, float imaginary)
{
return new torch::Scalar(c10::complex<float>(real, imaginary));
}
Scalar THSTorch_complex64_to_scalar(double real, double imaginary)
{
return new torch::Scalar(c10::complex<double>(real, imaginary));
}
int8_t THSTorch_scalar_to_int8(Scalar value)
{
return value->toChar();
}
uint8_t THSTorch_scalar_to_uint8(Scalar value)
{
return value->toByte();
}
int16_t THSTorch_scalar_to_int16(Scalar value)
{
return value->toShort();
}
int32_t THSTorch_scalar_to_int32(Scalar value)
{
return value->toInt();
}
int64_t THSTorch_scalar_to_int64(Scalar value)
{
return value->toLong();
}
float THSTorch_scalar_to_float32(Scalar value)
{
return value->toFloat();
}
double THSTorch_scalar_to_float64(Scalar value)
{
return value->toDouble();
}
void THSTorch_scalar_to_bfloat16(Scalar value, unsigned short* res)
{
*res = value->toBFloat16().x;
}
void THSTorch_scalar_to_float16(Scalar value, unsigned short *res)
{
*res = value->toHalf().x;
}
void THSTorch_scalar_to_complex32(Scalar value, float* real, float* imaginary)
{
auto result = value->toComplexFloat();
*real = result.real();
*imaginary = result.imag();
}
void THSTorch_scalar_to_complex64(Scalar value, double* real, double* imaginary)
{
auto result = value->toComplexDouble();
*real = result.real();
*imaginary = result.imag();
}
bool THSTorch_scalar_to_bool(Scalar value)
{
return value->toBool();
}
int8_t THSTorch_scalar_type(Scalar value)
{
return (int8_t)value->type();
}
void THSTorch_dispose_scalar(Scalar scalar)
{
delete scalar;
}
double THSSpecial_erf_scalar(const double x)
{
return erf(x);
}
double THSSpecial_erfc_scalar(const double x)
{
return erfc(x);
}