-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathTHSTorch.cpp
More file actions
329 lines (265 loc) · 6.92 KB
/
THSTorch.cpp
File metadata and controls
329 lines (265 loc) · 6.92 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
// 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"
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;
}
bool THSTorchCuda_is_available()
{
return torch::cuda::is_available();
}
bool 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);)
}
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);
}