-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper_manual.cpp
2121 lines (1680 loc) · 74.1 KB
/
wrapper_manual.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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <jni.h>
#include <iostream>
#include <exception>
#include <stdlib.h>
#include <string.h>
#include <ATen/Functions.h>
#include <c10/core/CPUAllocator.h>
#include "wrapper_manual.h"
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#if defined(WITHOUTCUDA)
#else
#include <c10/cuda/CUDACachingAllocator.h>
#include <c10/cuda/CUDAException.h>
#include <ATen/cuda/PinnedMemoryAllocator.h>
#include <torch/csrc/cuda/nccl.h>
#endif
using namespace std;
using namespace at;
static jint JNI_VERSION = JNI_VERSION_1_1;
jclass tensorClass;
jfieldID tensorPointerFid;
jclass ncclCommClass;
jfieldID ncclCommPointerFid;
jmethodID ncclCommCtor;
jclass tensorOptionsClass;
jmethodID tensorOptionsCtor;
jfieldID tensorOptionsPointerFid;
jclass longClass;
jmethodID longCtor;
// int64_t reinterpret_unsigned_to_signed(uint64_t x) {
// int64_t tmp;
// std::memcpy(&tmp, &x, sizeof(tmp));
// const int64_t y = tmp;
// return y;
// }
// uint64_t reinterpret_signed_to_unsigned(int64_t x) {
// uint64_t tmp;
// std::memcpy(&tmp, &x, sizeof(tmp));
// const uint64_t y = tmp;
// return y;
// }
jint JNI_OnLoad(JavaVM* vm, void* reserved) {
JNIEnv* env;
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION) != JNI_OK) {
cout << "jni abort" << endl;
return JNI_ERR;
}
jclass tempLocalClassRef;
tempLocalClassRef = env->FindClass("aten/Tensor");
tensorClass = (jclass) env->NewGlobalRef(tempLocalClassRef);
env->DeleteLocalRef(tempLocalClassRef);
tensorPointerFid = env->GetFieldID( tensorClass, "pointer", "J");
tempLocalClassRef = env->FindClass("aten/TensorOptions");
tensorOptionsClass = (jclass) env->NewGlobalRef(tempLocalClassRef);
env->DeleteLocalRef(tempLocalClassRef);
tensorOptionsCtor = env->GetMethodID( tensorOptionsClass, "<init>", "(J)V");
tensorOptionsPointerFid = env->GetFieldID( tensorOptionsClass, "pointer", "J");
tempLocalClassRef = env->FindClass("aten/NcclComm");
ncclCommClass = (jclass) env->NewGlobalRef(tempLocalClassRef);
env->DeleteLocalRef(tempLocalClassRef);
ncclCommCtor = env->GetMethodID( ncclCommClass, "<init>", "(J)V");
ncclCommPointerFid = env->GetFieldID( ncclCommClass, "pointer", "J");
tempLocalClassRef = env->FindClass("java/lang/Long");
longClass = (jclass) env->NewGlobalRef(tempLocalClassRef);
env->DeleteLocalRef(tempLocalClassRef);
longCtor = env->GetMethodID( longClass, "<init>", "(J)V");
return JNI_VERSION;
}
void JNI_OnUnload(JavaVM *vm, void *reserved) {
JNIEnv* env;
vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION);
env->DeleteGlobalRef(tensorClass);
env->DeleteGlobalRef(tensorOptionsClass);
}
jlongArray vecToJni(JNIEnv *env, std::vector<int64_t> vec){
int len = vec.size();
jlongArray ret = env->NewLongArray( len);
int64_t* buf = vec.data();
env->SetLongArrayRegion(ret,0,len,(jlong*)buf);
return ret;
}
jlong allocateTensor(JNIEnv *env, Tensor tensor) {
Tensor* result_on_heapreturnable_result = new Tensor(tensor);
jlong addr = reinterpret_cast<jlong>(result_on_heapreturnable_result);
return addr;
}
jobject allocateTensorOptions(JNIEnv *env, TensorOptions* tensorOptions) {
jlong addr = reinterpret_cast<jlong>(tensorOptions);
jobject ret_obj = env->NewObject( tensorOptionsClass, tensorOptionsCtor, addr);
return ret_obj;
}
jint throwRuntimeException( JNIEnv *env, const char *message )
{
jclass exClass = env->FindClass( "java/lang/RuntimeException" );
if ( exClass == NULL ) {
return -1;
}
return env->ThrowNew( exClass, message );
}
// Offset mmap
class OffsettableMMap {
public:
OffsettableMMap(const char *filename, size_t size, size_t offset, bool pin);
void close();
static at::DataPtr makeDataPtr(const char *filename, size_t size, size_t offset, bool pin);
void* pointer() const { return base_ptr_; }
~OffsettableMMap() {
close();
c10::reportMemoryUsageToProfiler(base_ptr_, -size_,0,0, c10::Device(c10::DeviceType::CPU));
}
private:
bool closed_ = false;
bool pinned_ = false;
ptrdiff_t size_;
void *base_ptr_ = nullptr;
};
OffsettableMMap::OffsettableMMap( const char *filename, size_t size, size_t offset, bool pin)
: size_(size) // to be filled later
, base_ptr_(nullptr)
{
if (size == 0) {
throw std::runtime_error("trying to mmap non positive size");
}
int fd;
struct stat file_stat;
pinned_ = pin;
if ((fd = open(filename, O_RDONLY)) == -1) {
AT_ERROR("unable to open file <", filename, "> in read-only mode");
}
if (fstat(fd, &file_stat) == -1) {
::close(fd);
AT_ERROR("unable to stat the file <", filename, ">");
}
if (size > file_stat.st_size-offset) {
::close(fd);
throw std::runtime_error("trying to mmap more than file size");
}
base_ptr_ = mmap(nullptr, size_, PROT_READ, MAP_PRIVATE, fd, offset);
if (pin) {
if(mlock(base_ptr_,size_)) {
AT_ERROR("Error locking page: ", strerror(errno), " (", errno, ")");
}
}
if (base_ptr_ == MAP_FAILED) {
::close(fd);
base_ptr_ = nullptr; /* let's be sure it is NULL */
AT_ERROR("unable to mmap ", size_, " bytes from file <", filename, ">: ", strerror(errno), " (", errno, ")");
}
if (::close(fd) == -1) {
AT_ERROR("Error closing file <", filename, ">");
}
c10::reportMemoryUsageToProfiler(base_ptr_, size_, 0, 0, c10::Device(c10::DeviceType::CPU));
}
void OffsettableMMap::close() {
if (closed_) {
return;
}
closed_ = true;
if (base_ptr_ == nullptr) {
return;
}
if (pinned_ && munlock(base_ptr_, size_)) {
AT_ERROR("could not unlock the pages");
}
if (munmap(base_ptr_, size_)) {
AT_ERROR("could not unmap the file");
}
}
static void deleteOffsettableMMap(void* ptr) {
delete static_cast<OffsettableMMap*>(ptr);
}
at::DataPtr OffsettableMMap::makeDataPtr(const char *filename, size_t size, size_t offset, bool pin) {
auto* allocatorOnHeap = new OffsettableMMap(filename, size, offset, pin);
return {allocatorOnHeap->pointer(), allocatorOnHeap, &deleteOffsettableMMap, at::DeviceType::CPU};
}
// Offset mmap end
extern "C" {
JNIEXPORT jobject JNICALL Java_aten_TensorOptions_cuda_1index(JNIEnv *env, jobject thisObj, jshort index) {
try {
jclass cls = tensorOptionsClass;
TensorOptions* tensorOptions = reinterpret_cast<TensorOptions*>(env->GetLongField( thisObj, tensorOptionsPointerFid));
TensorOptions* t2 = new TensorOptions(tensorOptions->device_index(index));
return allocateTensorOptions(env,t2);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jobject JNICALL Java_aten_TensorOptions_cuda(JNIEnv *env, jobject thisObj) { try{
jclass cls = tensorOptionsClass;
TensorOptions* tensorOptions = reinterpret_cast<TensorOptions*>(env->GetLongField( thisObj, tensorOptionsPointerFid));
TensorOptions* t2 = new TensorOptions(tensorOptions->device(at::kCUDA));
return allocateTensorOptions(env,t2);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jobject JNICALL Java_aten_TensorOptions_device(JNIEnv *env, jobject thisObj, jbyte deviceType, jbyte deviceIndex) { try{
jclass cls = tensorOptionsClass;
TensorOptions* tensorOptions = reinterpret_cast<TensorOptions*>(env->GetLongField( thisObj, tensorOptionsPointerFid));
TensorOptions* t2 = new TensorOptions(tensorOptions->device(static_cast<c10::DeviceType>(deviceType), deviceIndex));
return allocateTensorOptions(env,t2);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jobject JNICALL Java_aten_TensorOptions_cpu(JNIEnv *env, jobject thisObj) { try{
jclass cls = tensorOptionsClass;
TensorOptions* tensorOptions = reinterpret_cast<TensorOptions*>(env->GetLongField( thisObj, tensorOptionsPointerFid));
TensorOptions* t2 = new TensorOptions(tensorOptions->device(at::kCPU));
return allocateTensorOptions(env,t2);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jbyte JNICALL Java_aten_Tensor_scalarTypeByte(JNIEnv *env, jobject thisObj) { try{
jclass cls = tensorClass;
Tensor* tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
int8_t tpe = (int8_t)c10::typeMetaToScalarType(tensor->options().dtype());
return tpe;
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return 0;
}
JNIEXPORT jbyte JNICALL Java_aten_TensorOptions_scalarTypeByte(JNIEnv *env, jobject thisObj) { try{
jclass cls = tensorOptionsClass;
TensorOptions* tensorOptions = reinterpret_cast<TensorOptions*>(env->GetLongField( thisObj, tensorOptionsPointerFid));
int8_t tpe = (int8_t)c10::typeMetaToScalarType(tensorOptions->dtype());
return tpe;
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return 0;
}
JNIEXPORT jboolean JNICALL Java_aten_TensorOptions_isCPU(JNIEnv *env, jobject thisObj) { try{
jclass cls = tensorOptionsClass;
TensorOptions* tensorOptions = reinterpret_cast<TensorOptions*>(env->GetLongField( thisObj, tensorOptionsPointerFid));
bool tpe = tensorOptions->device().is_cpu();
return tpe;
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return 0;
}
JNIEXPORT jboolean JNICALL Java_aten_TensorOptions_isCuda(JNIEnv *env, jobject thisObj) { try{
jclass cls = tensorOptionsClass;
TensorOptions* tensorOptions = reinterpret_cast<TensorOptions*>(env->GetLongField( thisObj, tensorOptionsPointerFid));
bool tpe = tensorOptions->device().is_cuda();
return tpe;
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return 0;
}
JNIEXPORT jboolean JNICALL Java_aten_TensorOptions_isMps(JNIEnv *env, jobject thisObj) { try{
jclass cls = tensorOptionsClass;
TensorOptions* tensorOptions = reinterpret_cast<TensorOptions*>(env->GetLongField( thisObj, tensorOptionsPointerFid));
bool tpe = tensorOptions->device().is_mps();
return tpe;
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return 0;
}
JNIEXPORT jboolean JNICALL Java_aten_TensorOptions_isSparse(JNIEnv *env, jobject thisObj) { try{
jclass cls = tensorOptionsClass;
TensorOptions* tensorOptions = reinterpret_cast<TensorOptions*>(env->GetLongField( thisObj, tensorOptionsPointerFid));
bool tpe = tensorOptions->is_sparse();
return tpe;
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return 0;
}
JNIEXPORT jlong JNICALL Java_aten_Tensor_getNumGPUs(JNIEnv *env, jobject thisObj) { try{
jlong ret = at::detail::getCUDAHooks().getNumGPUs();
return ret;
} catch (exception& e) {
return 0;
}
return 0;
}
JNIEXPORT jboolean JNICALL Java_aten_Tensor_hasCuda(JNIEnv *env, jobject thisObj) { try{
bool ret = at::Context::hasCUDA();
return ret;
} catch (exception& e) {
return false;
}
return 0;
}
JNIEXPORT jboolean JNICALL Java_aten_Tensor_hasMps(JNIEnv *env, jobject thisObj) { try{
bool ret = at::Context::hasMPS();
return ret;
} catch (exception& e) {
return false;
}
return 0;
}
JNIEXPORT void JNICALL Java_aten_Tensor_manual_1seed(JNIEnv *env, jobject thisObj, jlong seed) { try{
at::manual_seed(seed);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
}
JNIEXPORT void JNICALL Java_aten_Tensor_manual_1seed_1cuda(JNIEnv *env, jobject thisObj, jlong seed, jint device) { try{
auto cuda_gen = globalContext().defaultGenerator(Device(at::kCUDA, device));
{
std::lock_guard<std::mutex> lock(cuda_gen.mutex());
cuda_gen.set_current_seed(seed);
}
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
}
JNIEXPORT void JNICALL Java_aten_Tensor_manual_1seed_1cpu(JNIEnv *env, jobject thisObj, jlong seed) { try{
auto gen = globalContext().defaultGenerator(DeviceType::CPU);
{
std::lock_guard<std::mutex> lock(gen.mutex());
gen.set_current_seed(seed);
}
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
}
JNIEXPORT void JNICALL Java_aten_Tensor_manual_1seed_1mps(JNIEnv *env, jobject thisObj, jlong seed) { try{
auto gen = globalContext().defaultGenerator(DeviceType::MPS);
{
std::lock_guard<std::mutex> lock(gen.mutex());
gen.set_current_seed(seed);
}
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
}
JNIEXPORT jint JNICALL Java_aten_TensorOptions_deviceIndex(JNIEnv *env, jobject thisObj) { try{
jclass cls = tensorOptionsClass;
TensorOptions* tensorOptions = reinterpret_cast<TensorOptions*>(env->GetLongField( thisObj, tensorOptionsPointerFid));
int32_t tpe = tensorOptions->device().index();
return tpe;
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return 0;
}
JNIEXPORT jint JNICALL Java_aten_TensorOptions_deviceType(JNIEnv *env, jobject thisObj) { try{
jclass cls = tensorOptionsClass;
TensorOptions* tensorOptions = reinterpret_cast<TensorOptions*>(env->GetLongField( thisObj, tensorOptionsPointerFid));
int32_t tpe = static_cast<int32_t>(tensorOptions->device().type());
return tpe;
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return 0;
}
JNIEXPORT jobject JNICALL Java_aten_TensorOptions_toDouble(JNIEnv *env, jobject thisObj) { try{
jclass cls = tensorOptionsClass;
TensorOptions* tensorOptions = reinterpret_cast<TensorOptions*>(env->GetLongField( thisObj, tensorOptionsPointerFid));
TensorOptions* t2 = new TensorOptions(tensorOptions->dtype(kDouble));
return allocateTensorOptions(env,t2);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jobject JNICALL Java_aten_TensorOptions_toBF16(JNIEnv *env, jobject thisObj) { try{
jclass cls = tensorOptionsClass;
TensorOptions* tensorOptions = reinterpret_cast<TensorOptions*>(env->GetLongField( thisObj, tensorOptionsPointerFid));
TensorOptions* t2 = new TensorOptions(tensorOptions->dtype(kBFloat16));
return allocateTensorOptions(env,t2);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jobject JNICALL Java_aten_TensorOptions_toInt(JNIEnv *env, jobject thisObj) { try{
jclass cls = tensorOptionsClass;
TensorOptions* tensorOptions = reinterpret_cast<TensorOptions*>(env->GetLongField( thisObj, tensorOptionsPointerFid));
TensorOptions* t2 = new TensorOptions(tensorOptions->dtype(kInt));
return allocateTensorOptions(env,t2);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jobject JNICALL Java_aten_TensorOptions_toShort(JNIEnv *env, jobject thisObj) { try{
jclass cls = tensorOptionsClass;
TensorOptions* tensorOptions = reinterpret_cast<TensorOptions*>(env->GetLongField( thisObj, tensorOptionsPointerFid));
TensorOptions* t2 = new TensorOptions(tensorOptions->dtype(kShort));
return allocateTensorOptions(env,t2);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jobject JNICALL Java_aten_TensorOptions_toLong(JNIEnv *env, jobject thisObj) { try{
jclass cls = tensorOptionsClass;
TensorOptions* tensorOptions = reinterpret_cast<TensorOptions*>(env->GetLongField( thisObj, tensorOptionsPointerFid));
TensorOptions* t2 = new TensorOptions(tensorOptions->dtype(kLong));
return allocateTensorOptions(env,t2);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jobject JNICALL Java_aten_TensorOptions_toFloat(JNIEnv *env, jobject thisObj) { try{
jclass cls = tensorOptionsClass;
TensorOptions* tensorOptions = reinterpret_cast<TensorOptions*>(env->GetLongField( thisObj, tensorOptionsPointerFid));
TensorOptions* t2 = new TensorOptions(tensorOptions->dtype(kFloat));
return allocateTensorOptions(env,t2);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jobject JNICALL Java_aten_TensorOptions_toHalf(JNIEnv *env, jobject thisObj) { try{
jclass cls = tensorOptionsClass;
TensorOptions* tensorOptions = reinterpret_cast<TensorOptions*>(env->GetLongField( thisObj, tensorOptionsPointerFid));
TensorOptions* t2 = new TensorOptions(tensorOptions->dtype(kHalf));
return allocateTensorOptions(env,t2);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jobject JNICALL Java_aten_TensorOptions_toByte(JNIEnv *env, jobject thisObj) { try{
jclass cls = tensorOptionsClass;
TensorOptions* tensorOptions = reinterpret_cast<TensorOptions*>(env->GetLongField( thisObj, tensorOptionsPointerFid));
// in libtorch kChar is signed 8 bit integer
// in java Byte is signed 8 bit integer
TensorOptions* t2 = new TensorOptions(tensorOptions->dtype(kChar));
return allocateTensorOptions(env,t2);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jobject JNICALL Java_aten_TensorOptions_dtypeFloat(JNIEnv *env, jobject thisObj) {try{
TensorOptions* tensorOptions =new TensorOptions((ScalarType)6);
return allocateTensorOptions(env,tensorOptions);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jobject JNICALL Java_aten_TensorOptions_dtypeInt(JNIEnv *env, jobject thisObj) {try{
TensorOptions* tensorOptions =new TensorOptions((ScalarType)3);
return allocateTensorOptions(env,tensorOptions);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jobject JNICALL Java_aten_TensorOptions_dtypeShort(JNIEnv *env, jobject thisObj) {try{
TensorOptions* tensorOptions =new TensorOptions((ScalarType)2);
return allocateTensorOptions(env,tensorOptions);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jobject JNICALL Java_aten_TensorOptions_dtypeLong(JNIEnv *env, jobject thisObj) {try{
TensorOptions* tensorOptions =new TensorOptions((ScalarType)4);
return allocateTensorOptions(env,tensorOptions);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jobject JNICALL Java_aten_TensorOptions_dtypeDouble(JNIEnv *env, jobject thisObj) {
try{
TensorOptions* tensorOptions =new TensorOptions((ScalarType)7);
return allocateTensorOptions(env,tensorOptions);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jobject JNICALL Java_aten_TensorOptions_dtypeBF16(JNIEnv *env, jobject thisObj) {
try{
TensorOptions* tensorOptions =new TensorOptions((ScalarType)15);
return allocateTensorOptions(env,tensorOptions);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jobject JNICALL Java_aten_TensorOptions_dtypeHalf(JNIEnv *env, jobject thisObj) {
try{
TensorOptions* tensorOptions =new TensorOptions((ScalarType)5);
return allocateTensorOptions(env,tensorOptions);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jobject JNICALL Java_aten_TensorOptions_dtypeByte(JNIEnv *env, jobject thisObj) {
try{
TensorOptions* tensorOptions =new TensorOptions((ScalarType)1);
return allocateTensorOptions(env,tensorOptions);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jobject JNICALL Java_aten_Tensor_options(JNIEnv *env, jobject thisObj) {
try{
jclass cls = tensorClass;
Tensor* tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
TensorOptions* opt = new TensorOptions(tensor->options());
return allocateTensorOptions(env,opt);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jint JNICALL Java_aten_Tensor_dim(JNIEnv *env, jobject thisObj) {
try{
jclass cls = tensorClass;
Tensor *tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
return tensor->dim();
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return 0;
}
JNIEXPORT jboolean JNICALL Java_aten_Tensor_defined(JNIEnv *env, jobject thisObj) {
try{
jclass cls = tensorClass;
Tensor* tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
return tensor->defined();
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return false;
}
JNIEXPORT jboolean JNICALL Java_aten_Tensor_isCuda(JNIEnv *env, jobject thisObj) {
try{
jclass cls = tensorClass;
Tensor* tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
return tensor->is_cuda();
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return false;
}
JNIEXPORT jboolean JNICALL Java_aten_Tensor_isMps(JNIEnv *env, jobject thisObj) {
try{
jclass cls = tensorClass;
Tensor* tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
return tensor->is_mps();
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return false;
}
JNIEXPORT jlong JNICALL Java_aten_Tensor_useCount(JNIEnv *env, jobject thisObj) {
try{
jclass cls = tensorClass;
Tensor* tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
return tensor->use_count();
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return 0;
}
JNIEXPORT jlong JNICALL Java_aten_Tensor_weakUseCount(JNIEnv *env, jobject thisObj) {
try{
jclass cls = tensorClass;
Tensor* tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
return tensor->weak_use_count();
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return 0;
}
JNIEXPORT jlong JNICALL Java_aten_Tensor_numel(JNIEnv *env, jobject thisObj) {
try{
jclass cls = tensorClass;
Tensor* tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
return tensor->numel();
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return 0;
}
JNIEXPORT jlong JNICALL Java_aten_Tensor_elementSize(JNIEnv *env, jobject thisObj) {
try{
jclass cls = tensorClass;
Tensor* tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
return tensor->element_size();
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return 0;
}
JNIEXPORT jbyte JNICALL Java_aten_Tensor_scalarType(JNIEnv *env, jobject thisObj) {
try{
jclass cls = tensorClass;
Tensor* tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
return static_cast<jbyte>(tensor->scalar_type());
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return 0;
}
JNIEXPORT jstring JNICALL Java_aten_Tensor_nativeToString(JNIEnv *env, jobject thisObj) {
try{
jclass cls = tensorClass;
Tensor* tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
return env->NewStringUTF(tensor->toString().c_str());
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jstring JNICALL Java_aten_TensorOptions_nativeToString(JNIEnv *env, jobject thisObj) {
try{
TensorOptions* op = reinterpret_cast<TensorOptions*>(env->GetLongField( thisObj, tensorOptionsPointerFid));
return env->NewStringUTF(c10::toString(*op).c_str());
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jlongArray JNICALL Java_aten_Tensor_sizes(JNIEnv *env, jobject thisObj) {
try{
jclass cls = tensorClass;
Tensor* tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
std::vector<int64_t> s = tensor->sizes().vec();
return vecToJni(env,s);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT jlongArray JNICALL Java_aten_Tensor_strides(JNIEnv *env, jobject thisObj) {
try{
jclass cls = tensorClass;
Tensor* tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
std::vector<int64_t> s = tensor->strides().vec();
return vecToJni(env,s);
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return nullptr;
}
JNIEXPORT void JNICALL Java_aten_Tensor_copyFrom(JNIEnv *env, jobject thisObj, jobject other, jboolean nonblocking) {
try{
jclass cls = tensorClass;
Tensor* tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
Tensor* otherTensor = reinterpret_cast<Tensor*>(env->GetLongField( other, tensorPointerFid));
tensor->copy_(*otherTensor,nonblocking);
return;
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return;
}
JNIEXPORT void JNICALL Java_aten_Tensor_print(JNIEnv *env, jobject thisObj) {try{
jclass cls = tensorClass;
Tensor* tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
tensor->print();
return;
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return ;
}
JNIEXPORT void JNICALL Java_aten_Tensor_mul_1(JNIEnv *env, jobject thisObj, jdouble d) {try{
jclass cls = tensorClass;
Tensor* tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
tensor->mul_(d);
return;
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return ;
}
JNIEXPORT void JNICALL Java_aten_Tensor_mul_1l_1(JNIEnv *env, jobject thisObj, jlong d) {try{
jclass cls = tensorClass;
Tensor* tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
tensor->mul_((int64_t)d);
return;
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return ;
}
JNIEXPORT jlong JNICALL Java_aten_Tensor_lowlevelexpand_1as(JNIEnv *env, jobject thisObj, jobject other) {try{
jclass cls = tensorClass;
Tensor* tensor1 = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
Tensor* tensor2 = reinterpret_cast<Tensor*>(env->GetLongField( other, tensorPointerFid));
Tensor tensor3 = tensor1->expand_as(*tensor2);
return reinterpret_cast<jlong>(new Tensor(tensor3));
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return NULL;
}
JNIEXPORT jlong JNICALL Java_aten_Tensor_lowlevelto_1dense(JNIEnv *env, jobject thisObj) {try{
jclass cls = tensorClass;
Tensor* tensor1 = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
Tensor tensor3 = tensor1->to_dense();
return reinterpret_cast<jlong>(new Tensor(tensor3));
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return NULL;
}
JNIEXPORT jlong JNICALL Java_aten_Tensor_lowlevelindices(JNIEnv *env, jobject thisObj) {try{
jclass cls = tensorClass;
Tensor* tensor1 = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
Tensor tensor3 = tensor1->indices();
return reinterpret_cast<jlong>(new Tensor(tensor3));
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return NULL;
}
JNIEXPORT jlong JNICALL Java_aten_Tensor_lowlevelvalues(JNIEnv *env, jobject thisObj) {try{
jclass cls = tensorClass;
Tensor* tensor1 = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
Tensor tensor3 = tensor1->values();
return reinterpret_cast<jlong>(new Tensor(tensor3));
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return NULL;
}
JNIEXPORT jlong JNICALL Java_aten_Tensor_lowlevelcoalesce(JNIEnv *env, jobject thisObj) {try{
jclass cls = tensorClass;
Tensor* tensor1 = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
Tensor tensor3 = tensor1->coalesce();
return reinterpret_cast<jlong>(new Tensor(tensor3));
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return NULL;
}
JNIEXPORT jlong JNICALL Java_aten_Tensor_lowlevelrepeat(JNIEnv *env, jobject thisObj, jlongArray repeat) {try{
jclass cls = tensorClass;
Tensor* tensor1 = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
int64_t* longs = (int64_t*)env->GetLongArrayElements(repeat, nullptr);
jsize length = env->GetArrayLength(repeat);
IntArrayRef intarrayref = IntArrayRef(longs,length);
Tensor tensor2 = tensor1->repeat(intarrayref);
env->ReleaseLongArrayElements(repeat,(jlong*)longs,0);
return reinterpret_cast<jlong>(new Tensor(tensor2));
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return NULL;
}
JNIEXPORT void JNICALL Java_aten_Tensor_add_1(JNIEnv *env, jobject thisObj, jdouble other, jdouble alpha) {try{
jclass cls = tensorClass;
Tensor* tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
tensor->add_(other,alpha);
return;
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return ;
}
JNIEXPORT void JNICALL Java_aten_Tensor_add_1l_1(JNIEnv *env, jobject thisObj, jlong other, jlong alpha) {try{
jclass cls = tensorClass;
Tensor* tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
tensor->add_((int64_t)other,(int64_t)alpha);
return;
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return ;
}
JNIEXPORT void JNICALL Java_aten_TensorOptions_releaseNative(JNIEnv *env, jobject thisObj) {try{
TensorOptions* op = reinterpret_cast<TensorOptions*>(env->GetLongField( thisObj, tensorOptionsPointerFid));
delete op;
return ;
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return ;
}
JNIEXPORT void JNICALL Java_aten_Tensor_releaseNative(JNIEnv *env, jobject thisObj) {try{
jclass cls = tensorClass;
Tensor* tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
delete tensor;
return;
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return ;
}
JNIEXPORT jboolean JNICALL Java_aten_Tensor_copyFromFloatArray(JNIEnv *env, jobject thisObj, jfloatArray datain) {try{
jclass cls = tensorClass;
Tensor* tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
long len = env->GetArrayLength(datain);
if (static_cast<int8_t>(tensor->scalar_type()) != 6 || !tensor->is_contiguous() || !tensor->is_non_overlapping_and_dense() || tensor->data_ptr() == nullptr || len != tensor->numel() || tensor->is_cuda() || tensor->is_sparse()) {
return false;
} else {
float* in = env->GetFloatArrayElements(datain, nullptr);
float* ptr = reinterpret_cast<float*>(tensor->data_ptr());
memcpy(ptr,in,len*4);
env->ReleaseFloatArrayElements(datain,in,0);
return true;
}
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return false;
}
JNIEXPORT jboolean JNICALL Java_aten_Tensor_copyFromFloatArrayAtOffset(JNIEnv *env, jobject thisObj, jfloatArray datain, jlong offset) {try{
jclass cls = tensorClass;
Tensor* tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid));
long len = env->GetArrayLength(datain);
if (static_cast<int8_t>(tensor->scalar_type()) != 6 || !tensor->is_contiguous() || !tensor->is_non_overlapping_and_dense() || tensor->data_ptr() == nullptr || len+offset > tensor->numel() || tensor->is_cuda() || tensor->is_sparse()) {
return false;
} else {
float* in = env->GetFloatArrayElements(datain, nullptr);
float* ptr = reinterpret_cast<float*>(tensor->data_ptr());
memcpy(&ptr[offset],in,len*4);
env->ReleaseFloatArrayElements(datain,in,0);
return true;
}
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return false;
}
JNIEXPORT jboolean JNICALL Java_aten_Tensor_copyToFloatArray(JNIEnv *env, jobject thisObj, jfloatArray datain) {try{
jclass cls = tensorClass;
Tensor tensor = reinterpret_cast<Tensor*>(env->GetLongField( thisObj, tensorPointerFid))->contiguous();
long len = env->GetArrayLength(datain);
if (static_cast<int8_t>(tensor.scalar_type()) != 6 || !tensor.is_contiguous() || !tensor.is_non_overlapping_and_dense() || tensor.data_ptr() == nullptr || len != tensor.numel() || tensor.is_cuda() || tensor.is_sparse()) {
return false;
} else {
float* in = env->GetFloatArrayElements(datain, nullptr);
float* ptr = reinterpret_cast<float*>(tensor.data_ptr());
memcpy(in,ptr,len*4);
env->ReleaseFloatArrayElements(datain,in,0);
return true;
}
} catch (exception& e) {
throwRuntimeException(env,e.what() );
}
return false;
}
JNIEXPORT jboolean JNICALL Java_aten_Tensor_copyFromLongArray(JNIEnv *env, jobject thisObj, jlongArray datain) {try{
jclass cls = tensorClass;