forked from artyom-beilis/pytorch_dlprim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointwise_ops.cpp
1167 lines (1045 loc) · 46.8 KB
/
pointwise_ops.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 <torch/torch.h>
#include <ATen/ATen.h>
#include "CLTensor.h"
#include "utils.h"
#include <dlprim/core/ip.hpp>
#include <dlprim/core/bn.hpp>
#include <dlprim/core/util.hpp>
#include <dlprim/core/conv.hpp>
#include <dlprim/core/bias.hpp>
#include <dlprim/core/pool.hpp>
#include <dlprim/core/loss.hpp>
#include <dlprim/core/pointwise.hpp>
#include <dlprim/core/activation.hpp>
#include <iostream>
namespace ptdlprim {
using namespace torch;
using torch::autograd::tensor_list;
using torch::autograd::AutogradContext;
using c10::Device;
using c10::DeviceType;
bool isCPUScalar(Tensor const &other,double &value)
{
if(other.device() == Device(c10::kCPU) && other.numel()==1) {
switch(other.dtype().toScalarType()){
case c10::kFloat:
value = *static_cast<float const *>(other.data_ptr());
break;
case c10::kDouble:
value = *static_cast<double const *>(other.data_ptr());
break;
case c10::kLong:
value = *static_cast<int64_t const *>(other.data_ptr());
break;
default:
TORCH_CHECK(false,"Unsupported cpu data type");
}
return true;
}
return false;
}
Tensor & relu_(Tensor & self)
{
GUARD;
dlprim::Tensor X = todp(self);
dlprim::ExecutionContext q = getExecutionContext(self);
dlprim::core::activation_forward(X,X,dlprim::StandardActivations::relu,q);
sync_if_needed(self.device());
return self;
}
template<dlprim::StandardActivations Act>
class act_cls : public torch::autograd::Function<act_cls<Act> > {
public:
static torch::Tensor forward(AutogradContext *ctx, torch::Tensor x)
{
GUARD;
at::AutoDispatchBelowADInplaceOrView g;
Tensor x_c = x.contiguous();
dlprim::Tensor X = todp(x_c);
torch::Tensor result = new_tensor_as(X.shape(),x);
ctx->save_for_backward({result});
dlprim::Tensor Y = todp(result);
dlprim::ExecutionContext q = getExecutionContext(x);
dlprim::core::activation_forward(X,Y,Act,q);
sync_if_needed(x.device());
return result;
}
static tensor_list backward(AutogradContext *ctx, tensor_list grad_outputs) {
GUARD;
auto grad_output = grad_outputs[0].contiguous();
torch::Tensor result = ctx->get_saved_variables()[0];
dlprim::Tensor dy=todp(grad_output);
dlprim::Tensor y=todp(result);
torch::Tensor grad_input = new_tensor_as(dy.shape(),grad_output);
dlprim::Tensor dx = todp(grad_input);
dlprim::core::activation_backward(dx,dy,y,Act,0.0,getExecutionContext(grad_output));
sync_if_needed(grad_output.device());
return {grad_input};
}
};
template<dlprim::StandardActivations Act>
torch::Tensor act_autograd(torch::Tensor const &x) {
GUARD;
return act_cls<Act>::apply(x);
}
#if 0
// Don't know how to fix it yet
template<dlprim::StandardActivations Act>
class act_inplace_cls : public torch::autograd::Function<act_inplace_cls<Act> > {
public:
static torch::Tensor &forward(AutogradContext *ctx, torch::Tensor &x)
{
at::AutoDispatchBelowADInplaceOrView g;
TORCH_CHECK(x.is_contiguous(),"OpenCL requireds contiguous output");
dlprim::Tensor X = todp(x);
ctx->save_for_backward({x});
dlprim::ExecutionContext q = getExecutionContext(x);
dlprim::core::activation_forward(X,X,Act,q);
sync_if_needed(x.device());
return x;
}
static tensor_list backward(AutogradContext *ctx, tensor_list grad_outputs) {
TORCH_CHECK(grad_outputs[0].is_contiguous(),"OpenCL requireds contiguous output");
auto &grad_output = grad_outputs[0];
torch::Tensor result = ctx->get_saved_variables()[0];
dlprim::Tensor dy=todp(grad_output);
dlprim::Tensor y=todp(result);
dlprim::core::activation_backward(dy,dy,y,Act,0.0,getExecutionContext(grad_output));
sync_if_needed(grad_output.device());
return {grad_output};
}
};
template<dlprim::StandardActivations Act>
torch::Tensor &act_inplace_autograd(torch::Tensor &x) {
return act_inplace_cls<Act>::apply(x);
}
#endif
Tensor & mul_scalar_(Tensor & self, const Scalar & other)
{
GUARD;
dlprim::Tensor x0=todp(self);
float scale = other.to<double>();
dlprim::core::pointwise_operation({x0},{x0},{scale},
"y0 = x0*w0;",
getExecutionContext(self));
sync_if_needed(self.device());
return self;
}
// {"schema": "aten::add.out(Tensor self, Tensor other, *, Scalar alpha=1, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & add_out(const Tensor & self, const Tensor & other, const Scalar & alpha, Tensor & out)
{
GUARD;
Tensor self_c = self.contiguous();
dlprim::Tensor x0=todp(self_c);
dlprim::Tensor y0=todp(out);
double value=0;
if(isCPUScalar(other,value)) {
float w0 = alpha.toDouble() * value;
dlprim::core::pointwise_operation({x0},{y0},{w0},
"y0 = x0 + w0;",
getExecutionContext(self));
}
else {
Tensor other_c = other.contiguous();
dlprim::Tensor x1=todp(other_c);
float w0 = alpha.toDouble();
dlprim::core::pointwise_operation_broadcast({x0,x1},{y0},{w0},
"y0 = x0 + x1 * w0;",
getExecutionContext(self));
}
sync_if_needed(self.device());
return out;
}
// {"schema": "aten::exp.out(Tensor self, *, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & exp_out(const Tensor & self, Tensor & out)
{
GUARD;
Tensor self_c=self.contiguous();
dlprim::core::pointwise_operation({todp(self_c)},{todp(out)},{},
"y0 = exp(x0);",getExecutionContext(self));
sync_if_needed(self.device());
return out;
}
// {"schema": "aten::log.out(Tensor self, *, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & log_out(const Tensor & self, Tensor & out)
{
GUARD;
Tensor self_c=self.contiguous();
dlprim::core::pointwise_operation({todp(self_c)},{todp(out)},{},
"y0 = log(x0);",getExecutionContext(self));
sync_if_needed(self.device());
return out;
}
// {"schema": "aten::sub.out(Tensor self, Tensor other, *, Scalar alpha=1, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & sub_out(const Tensor & self, const Tensor & other, const Scalar & alpha, Tensor & out)
{
GUARD;
return add_out(self,other,Scalar(alpha.toDouble()*-1),out);
}
// {"schema": "aten::addcmul.out(Tensor self, Tensor tensor1, Tensor tensor2, *, Scalar value=1, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & addcmul_out(const Tensor & self, const Tensor & tensor1, const Tensor & tensor2, const Scalar & value, Tensor & out)
{
GUARD;
Tensor self_c = self.contiguous(),
tensor1_c = tensor1.contiguous(),
tensor2_c = tensor2.contiguous();
dlprim::Tensor x0=todp(self_c);
dlprim::Tensor x1=todp(tensor1_c);
dlprim::Tensor x2=todp(tensor2_c);
dlprim::Tensor y0=todp(out);
float w0 = value.toDouble();
dlprim::core::pointwise_operation_broadcast({x0,x1,x2},{y0},{w0},
"y0 = x0 + w0 * x1 * x2;",
getExecutionContext(self));
sync_if_needed(self.device());
return out;
}
Tensor & comp_out(const Tensor & self, const Scalar & other, Tensor & out,std::string const &op)
{
GUARD;
Tensor self_c = self.contiguous();
dlprim::Tensor x0=todp(self_c);
dlprim::Tensor y0=todp(out);
float w0 = other.toDouble();
dlprim::core::pointwise_operation_broadcast({x0},{y0},{w0},
"y0 = x0 " + op + " w0 ? 1 : 0;",
getExecutionContext(self));
sync_if_needed(self.device());
return out;
}
// {"schema": "aten::le.Scalar_out(Tensor self, Scalar other, *, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & le_out(const Tensor & self, const Scalar & other, Tensor & out)
{
return comp_out(self,other,out,"<=");
}
// {"schema": "aten::ge.Scalar_out(Tensor self, Scalar other, *, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & ge_out(const Tensor & self, const Scalar & other, Tensor & out)
{
return comp_out(self,other,out,">=");
}
// {"schema": "aten::lt.Scalar_out(Tensor self, Scalar other, *, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & lt_out(const Tensor & self, const Scalar & other, Tensor & out)
{
return comp_out(self,other,out,"<");
}
// {"schema": "aten::gt.Scalar_out(Tensor self, Scalar other, *, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & gt_out(const Tensor & self, const Scalar & other, Tensor & out)
{
return comp_out(self,other,out,">");
}
// {"schema": "aten::neg.out(Tensor self, *, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & neg_out(const Tensor & self, Tensor & out)
{
GUARD;
Tensor self_c = self.contiguous();
dlprim::Tensor x0=todp(self_c);
dlprim::Tensor y0=todp(out);
dlprim::core::pointwise_operation_broadcast({x0},{y0},{},"y0=-x0;",getExecutionContext(self));
sync_if_needed(self.device());
return out;
}
// {"schema": "aten::reciprocal.out(Tensor self, *, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & reciprocal_out(const Tensor & self, Tensor & out)
{
GUARD;
Tensor self_c = self.contiguous();
dlprim::Tensor x0=todp(self_c);
dlprim::Tensor y0=todp(out);
dlprim::core::pointwise_operation_broadcast({x0},{y0},{},"y0=1.0/x0;",getExecutionContext(self));
sync_if_needed(self.device());
return out;
}
// {"schema": "aten::sqrt.out(Tensor self, *, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & sqrt_out(const Tensor & self, Tensor & out)
{
GUARD;
Tensor self_c = self.contiguous();
dlprim::Tensor x0=todp(self_c);
dlprim::Tensor y0=todp(out);
dlprim::core::pointwise_operation({x0},{y0},{},
"y0 = sqrt(x0);",
getExecutionContext(self));
sync_if_needed(self.device());
return out;
}
// {"schema": "aten::div.out(Tensor self, Tensor other, *, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & div_out(const Tensor & self, const Tensor & other, Tensor & out)
{
GUARD;
Tensor self_c = self.contiguous();
dlprim::Tensor x0=todp(self_c);
dlprim::Tensor y0=todp(out);
double value=0;
if(isCPUScalar(other,value)) {
dlprim::core::pointwise_operation({x0},{y0},{double(1.0/value)},
"y0 = x0*w0;",
getExecutionContext(self));
}
else {
Tensor other_c = other.contiguous();
dlprim::Tensor x1=todp(other_c);
dlprim::core::pointwise_operation_broadcast({x0,x1},{y0},{},
"y0 = x0/x1;",
getExecutionContext(self));
}
sync_if_needed(self.device());
return out;
}
Tensor & mul_out(const Tensor & self, const Tensor & other, Tensor & out)
{
GUARD;
double scale=0;
Tensor self_c = self.contiguous();
dlprim::Tensor x0=todp(self_c);
dlprim::Tensor y0=todp(out);
if(isCPUScalar(other,scale)) {
dlprim::core::pointwise_operation({x0},{y0},{float(scale)},
"y0 = x0*w0;",
getExecutionContext(self));
}
else {
Tensor other_c = other.contiguous();
dlprim::Tensor x1=todp(other_c);
dlprim::core::pointwise_operation_broadcast({x0,x1},{y0},{},
"y0 = x0*x1;",
getExecutionContext(self));
}
sync_if_needed(self.device());
return out;
}
// {"schema": "aten::addcdiv.out(Tensor self, Tensor tensor1, Tensor tensor2, *, Scalar value=1, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & addcdiv_out(const Tensor & self, const Tensor & tensor1, const Tensor & tensor2, const Scalar & value, Tensor & out)
{
GUARD;
Tensor self_c = self.contiguous(),
tensor1_c = tensor1.contiguous(),
tensor2_c = tensor2.contiguous();
dlprim::Tensor x0 = todp(self_c);
dlprim::Tensor x1 = todp(tensor1_c);
dlprim::Tensor x2 = todp(tensor2_c);
dlprim::Tensor y0 = todp(out);
float w0 = value.toDouble();
dlprim::core::pointwise_operation_broadcast({x0,x1,x2},{y0},{w0},
"y0 = x0 + w0 * (x1/x2);",
getExecutionContext(self));
sync_if_needed(self.device());
return out;
}
// {"schema": "aten::threshold_backward.grad_input(Tensor grad_output, Tensor self, Scalar threshold, *, Tensor(a!) grad_input) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & threshold_backward_out(const Tensor & grad_output, const Tensor & self, const Scalar & threshold, Tensor & grad_input)
{
GUARD;
Tensor grad_output_c = grad_output.contiguous(),
self_c = self.contiguous();
dlprim::Tensor dy=todp(grad_output_c);
dlprim::Tensor dx=todp(grad_input);
dlprim::Tensor Y=todp(self_c);
float th = threshold.toDouble();
dlprim::core::pointwise_operation({Y,dy},{dx},{th},"y0 = (x0 > w0) ? x1 : 0;",getExecutionContext(self));
sync_if_needed(self.device());
return grad_input;
}
std::pair<dlprim::Shape,dlprim::Shape> squeeze_dim(dlprim::Shape s,OptionalIntArrayRef odim,bool keepdim)
{
GUARD;
std::vector<size_t> full,squeezed;
std::vector<int> dims;
if(odim)
dims.assign(odim->begin(),odim->end());
if(dims.empty()) {
for(int i=0;i<s.size();i++)
dims.push_back(i);
}
for(auto &axis : dims) {
if (axis < 0) {
axis = axis + s.size();
}
}
std::sort(dims.begin(),dims.end());
int pos = 0;
for(int i=0;i<s.size();i++) {
if(pos < int(dims.size()) && i==dims[pos]) {
full.push_back(1);
if(keepdim)
squeezed.push_back(1);
pos++;
}
else {
full.push_back(s[i]);
squeezed.push_back(s[i]);
}
}
TORCH_CHECK(pos == int(dims.size()),"Looks like invalid dims");
auto full_shape = dlprim::Shape::from_range(full.begin(),full.end());
auto squeezed_shape = dlprim::Shape::from_range(squeezed.begin(),squeezed.end());
if(squeezed_shape.size() == 0) {
squeezed_shape = dlprim::Shape(1);
}
return std::make_pair(full_shape,squeezed_shape);
}
Tensor & sum_mean_out(const Tensor & self, OptionalIntArrayRef dim, bool keepdim, c10::optional<ScalarType> /*dtype*/, Tensor & out,bool mean)
{
GUARD;
Tensor self_c = self.contiguous();
dlprim::Tensor X = todp(self_c);
auto r = squeeze_dim(X.shape(),dim,keepdim);
dlprim::Tensor Y = todp(out);
TORCH_CHECK(r.second == Y.shape(),"Invalid output shape");
Y.reshape(r.first);
double scale = mean ? double(Y.shape().total_size()) / double(X.shape().total_size()) : 1;
auto q = getExecutionContext(self);
dlprim::Context ctx(q);
auto op = dlprim::core::PointwiseOperationBroadcastReduce::create(
ctx,
{X.specs()},{Y.specs()},
0,dlprim::float_data,
"y0=x0;",
"reduce_y0 = 0;",
"reduce_y0 += y0;");
WSGuard wsg(op->workspace(),self.device());
op->enqueue({X},{Y},wsg.ws,{},{scale},{0},q);
sync_if_needed(self.device());
return out;
}
// {"schema": "aten::mean.out(Tensor self, int[1]? dim, bool keepdim=False, *, ScalarType? dtype=None, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & mean_out(const Tensor & self, OptionalIntArrayRef dim, bool keepdim, c10::optional<ScalarType> dtype, Tensor & out)
{
GUARD;
return sum_mean_out(self,dim,keepdim,dtype,out,true);
}
// {"schema": "aten::sum.IntList_out(Tensor self, int[1]? dim, bool keepdim=False, *, ScalarType? dtype=None, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & sum_out(const Tensor & self, OptionalIntArrayRef dim, bool keepdim, c10::optional<ScalarType> dtype, Tensor & out)
{
GUARD;
return sum_mean_out(self,dim,keepdim,dtype,out,false);
}
// {"schema": "aten::hardtanh_(Tensor(a!) self, Scalar min_val=-1, Scalar max_val=1) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor hardtanh(Tensor const &self, const Scalar & min_val, const Scalar & max_val)
{
GUARD;
dlprim::Tensor X=todp(self);
Tensor out = new_tensor_as(X.shape(),self);
dlprim::Tensor Y(todp(out));
double w0 = min_val.toDouble();
double w1 = max_val.toDouble();
dlprim::core::pointwise_operation({X},{Y},{w0,w1},"y0=max(w0,min(w1,x0));",getExecutionContext(self));
sync_if_needed(self.device());
return out;
}
// {"schema": "aten::hardtanh_(Tensor(a!) self, Scalar min_val=-1, Scalar max_val=1) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & hardtanh_(Tensor & self, const Scalar & min_val, const Scalar & max_val)
{
GUARD;
dlprim::Tensor X=todp(self);
double w0 = min_val.toDouble();
double w1 = max_val.toDouble();
dlprim::core::pointwise_operation({X},{X},{w0,w1},"y0=max(w0,min(w1,x0));",getExecutionContext(self));
sync_if_needed(self.device());
return self;
}
// {"schema": "aten::hardtanh_backward(Tensor grad_output, Tensor self, Scalar min_val, Scalar max_val) -> Tensor", "dispatch": "True", "default": "False"}
Tensor hardtanh_backward(const Tensor & grad_output, const Tensor & self, const Scalar & min_val, const Scalar & max_val)
{
GUARD;
dlprim::Tensor dY = todp(grad_output);
dlprim::Tensor X = todp(self);
Tensor result = new_tensor_as(X.shape(),self);
dlprim::Tensor dX = todp(result);
double w0 = min_val.toDouble();
double w1 = max_val.toDouble();
dlprim::core::pointwise_operation({X,dY},{dX},{w0,w1},"y0 = (w0 <= x0 && x0 <= w1) ? x1 : 0;",getExecutionContext(self));
sync_if_needed(self.device());
return result;
}
// {"schema": "aten::abs.out(Tensor self, *, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor abs(const Tensor & self)
{
GUARD;
Tensor self_c = self.contiguous();
dlprim::Tensor x=todp(self_c);
Tensor out = new_tensor_as(x.shape(),self);
dlprim::Tensor y=todp(out);
dlprim::core::pointwise_operation({x},{y},{},"y0 = x0 < 0 ? -x0 : x0;",getExecutionContext(self));
sync_if_needed(self.device());
return out;
}
// {"schema": "aten::abs.out(Tensor self, *, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & abs_out(const Tensor & self, Tensor & out)
{
GUARD;
Tensor self_c = self.contiguous();
dlprim::Tensor x=todp(self_c);
dlprim::Tensor y=todp(out);
dlprim::core::pointwise_operation({x},{y},{},"y0 = x0 < 0 ? -x0 : x0;",getExecutionContext(self));
sync_if_needed(self.device());
return out;
}
// {"schema": "aten::sgn.out(Tensor self, *, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & sgn_out(const Tensor & self, Tensor & out)
{
GUARD;
Tensor self_c = self.contiguous();
dlprim::Tensor x=todp(self_c);
dlprim::Tensor y=todp(out);
dlprim::core::pointwise_operation({x},{y},{},"y0 = x0 < 0 ? -1 : (x0 > 0 ? 1 : 0) ;",getExecutionContext(self));
sync_if_needed(self.device());
return out;
}
template<typename TL>
Tensor &cat_internal(TL const &tensors, int64_t dim, Tensor &out,bool reuse)
{
GUARD;
std::vector<dlprim::Tensor> list;
std::vector<Tensor> list_c;
for(auto const &t:tensors) {
list_c.push_back(t.contiguous());
list.push_back(todp(list_c.back()));
}
TORCH_CHECK(!list_c.empty());
Tensor &ref_tensor=list_c.front();
size_t total_shape = 0;
dlprim::Shape ref;
for(size_t i=0;i<list.size();i++) {
TORCH_CHECK(0<=dim && dim < list[i].shape().size(),"dim does not match shape")
if(i==0) {
ref = list[i].shape();
}
else {
dlprim::Shape s1 = ref, s2 = list[i].shape();
s1[dim]=1; s2[dim]=1;
TORCH_CHECK(s1==s2,"Shapes do not match");
}
total_shape+=list[i].shape()[dim];
}
ref[dim]=total_shape;
dlprim::Tensor Y;
if(reuse) {
Y = todp(out);
TORCH_CHECK(Y.shape() == ref);
}
else {
out = new_tensor_as(ref,ref_tensor);
Y = todp(out);
}
dlprim::ExecutionContext q(getExecutionContext(ref_tensor));
dlprim::Context ctx(q);
dlprim::core::SliceCopy cp(ctx,todp(ref_tensor.dtype()));
for(size_t i=0,pos=0;i<list.size();i++) {
size_t slice = list[i].shape()[dim];
cp.tensor_slice_copy(dim,slice,
Y,pos,
list[i],0,
0.0,q);
pos += slice;
}
sync_if_needed(ref_tensor.device());
return out;
}
// {"schema": "aten::cat.out(Tensor[] tensors, int dim=0, *, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & cat_out(const ITensorListRef & tensors, int64_t dim, Tensor & out)
{
cat_internal(tensors,dim,out,true);
return out;
}
// {"schema": "aten::_cat(Tensor[] tensors, int dim=0) -> Tensor", "dispatch": "True", "default": "False"}
Tensor _cat(TensorList tensors, int64_t dim)
{
GUARD;
Tensor out;
cat_internal(tensors,dim,out,false);
return out;
}
// {"schema": "aten::hardswish_(Tensor(a!) self)
Tensor & hardswish_(Tensor & self)
{
GUARD;
Tensor self_c = self.contiguous();
dlprim::Tensor x=todp(self_c);
dlprim::core::pointwise_operation({x},{x},{},"y0 = x0 <= -3 ? 0 : (x0>=3 ? x0 : x0*(x0+3)/6);",getExecutionContext(self));
sync_if_needed(self.device());
return self;
}
// {"schema": "aten::hardsigmoid.out(Tensor self, *, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & hardsigmoid_out(const Tensor & self, Tensor & out)
{
GUARD;
Tensor self_c = self.contiguous();
dlprim::Tensor x=todp(self_c);
dlprim::Tensor y=todp(out);
dlprim::core::pointwise_operation({x},{y},{},"y0 = x0 <= -3 ? 0 : (x0>=3 ? 1 : x0/6 + 0.5);",getExecutionContext(self));
sync_if_needed(self.device());
return out;
}
// {"schema": "aten::hardsigmoid_backward.grad_input(Tensor grad_output, Tensor self, *, Tensor(a!) grad_input) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & hardsigmoid_backward_out(const Tensor & grad_output, const Tensor & self, Tensor & grad_input)
{
GUARD;
Tensor self_c = self.contiguous();
dlprim::Tensor x=todp(self_c);
dlprim::Tensor dx=todp(grad_input);
dlprim::Tensor dy=todp(grad_output);
dlprim::core::pointwise_operation({x,dy},{dx},{},"y0 = (-3 < x0 && x0 < 3) ? x1 / 6 : 0;",getExecutionContext(self));
sync_if_needed(self.device());
return grad_input;
}
// {"schema": "aten::hardswish_backward(Tensor grad_output, Tensor self) -> Tensor", "dispatch": "True", "default": "False"}
Tensor hardswish_backward(const Tensor & grad_output, const Tensor & self)
{
GUARD;
dlprim::Tensor dy=todp(grad_output);
Tensor out = new_tensor_as(dy.shape(),grad_output);
dlprim::Tensor dx=todp(out);
dlprim::Tensor x =todp(self);
dlprim::core::pointwise_operation({x,dy},{dx},{},
R"xxx(
if (x0 < -3) {
y0 = 0;
} else if (x0 <= 3) {
y0 = x1 * ((x0 / 3) + 0.5);
} else {
y0 = x1;
}
)xxx",
getExecutionContext(self));
sync_if_needed(self.device());
return out;
}
// {"schema": "aten::sigmoid.out(Tensor self, *, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & sigmoid_out(const Tensor & self, Tensor & out)
{
GUARD;
Tensor self_c = self.contiguous();
dlprim::Tensor x=todp(self_c);
dlprim::Tensor y=todp(out);
dlprim::core::activation_forward(x,y,dlprim::StandardActivations::sigmoid,getExecutionContext(self));
sync_if_needed(self.device());
return out;
}
// {"schema": "aten::sigmoid(Tensor self) -> Tensor", "dispatch": "True", "default": "True"}
Tensor sigmoid(const Tensor & self)
{
GUARD;
Tensor self_c = self.contiguous();
dlprim::Tensor x=todp(self_c);
Tensor out = new_tensor_as(x.shape(),self);
dlprim::Tensor y=todp(out);
dlprim::core::activation_forward(x,y,dlprim::StandardActivations::sigmoid,getExecutionContext(self));
sync_if_needed(self.device());
return out;
}
// {"schema": "aten::sigmoid_(Tensor(a!) self) -> Tensor(a!)", "dispatch": "True", "default": "True"}
Tensor & sigmoid_(Tensor & self)
{
GUARD;
Tensor self_c = self.contiguous();
dlprim::Tensor X=todp(self_c);
dlprim::core::activation_forward(X,X,dlprim::StandardActivations::sigmoid,getExecutionContext(self));
if(!self.is_contiguous())
self.copy_(self_c);
sync_if_needed(self.device());
return self;
}
// {"schema": "aten::sigmoid_backward.grad_input(Tensor grad_output, Tensor output, *, Tensor(a!) grad_input) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & sigmoid_backward_out(const Tensor & grad_output, const Tensor & output, Tensor & grad_input)
{
GUARD;
Tensor output_c = output.contiguous();
dlprim::Tensor y=todp(output_c);
dlprim::Tensor dy=todp(grad_output);
dlprim::Tensor dx=todp(grad_input);
dlprim::core::activation_backward(dx,dy,y,dlprim::StandardActivations::sigmoid,0,getExecutionContext(grad_output));
sync_if_needed(grad_output.device());
return grad_input;
}
// {"schema": "aten::tanh.out(Tensor self, *, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & tanh_out(const Tensor & self, Tensor & out)
{
GUARD;
Tensor self_c = self.contiguous();
dlprim::Tensor x=todp(self_c);
dlprim::Tensor y=todp(out);
dlprim::core::activation_forward(x,y,dlprim::StandardActivations::tanh,getExecutionContext(self));
sync_if_needed(self.device());
return out;
}
// {"schema": "aten::tanh_backward.grad_input(Tensor grad_output, Tensor output, *, Tensor(a!) grad_input) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & tanh_backward_out(const Tensor & grad_output, const Tensor & output, Tensor & grad_input)
{
GUARD;
Tensor grad_output_c = grad_output.contiguous(),
output_c = output.contiguous();
dlprim::Tensor dY=todp(grad_output_c);
dlprim::Tensor Y=todp(output_c);
dlprim::Tensor dX=todp(grad_input);
dlprim::core::activation_backward(dX,dY,Y,dlprim::StandardActivations::tanh,0.0,getExecutionContext(grad_output));
sync_if_needed(grad_output.device());
return grad_input;
}
// {"schema": "aten::silu.out(Tensor self, *, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & silu_out(const Tensor & self, Tensor & out)
{
GUARD;
Tensor self_c = self.contiguous();
dlprim::Tensor x=todp(self_c);
dlprim::Tensor y=todp(out);
dlprim::core::pointwise_operation({x},{y},{},"y0 = x0 / (1 + exp(-x0));",getExecutionContext(self));
sync_if_needed(self.device());
return out;
}
// {"schema": "aten::silu_backward.grad_input(Tensor grad_output, Tensor self, *, Tensor(a!) grad_input) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & silu_backward_out(const Tensor & grad_output, const Tensor & self, Tensor & grad_input)
{
GUARD;
Tensor self_c = self.contiguous(),
grad_output_c = grad_output.contiguous();
dlprim::Tensor x=todp(self_c);
dlprim::Tensor dy=todp(grad_output_c);
dlprim::Tensor dx=todp(grad_input);
dlprim::core::pointwise_operation({x,dy},{dx},{},
R"xxx(
y0 = 1 / (1 + exp(-x0));
y0 = x1 * y0 * ( 1 + x0 * (1 - y0));
)xxx",
getExecutionContext(self));
sync_if_needed(self.device());
return grad_input;
}
// {"schema": "aten::tanh(Tensor self) -> Tensor", "dispatch": "True", "default": "True"}
Tensor tanh(const Tensor & self)
{
GUARD;
Tensor self_c = self.contiguous();
dlprim::Tensor x=todp(self_c);
Tensor out = new_tensor_as(x.shape(),self);
dlprim::Tensor y=todp(out);
dlprim::core::activation_forward(x,y,dlprim::StandardActivations::tanh,getExecutionContext(self));
sync_if_needed(self.device());
return out;
}
// {"schema": "aten::tanh_(Tensor(a!) self) -> Tensor(a!)", "dispatch": "True", "default": "True"}
Tensor & tanh_(Tensor & self)
{
GUARD;
Tensor self_c = self.contiguous();
dlprim::Tensor X=todp(self_c);
dlprim::core::activation_forward(X,X,dlprim::StandardActivations::tanh,getExecutionContext(self));
if(!self.is_contiguous())
self.copy_(self_c);
sync_if_needed(self.device());
return self;
}
// {"schema": "aten::leaky_relu.out(Tensor self, Scalar negative_slope=0.01, *, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & leaky_relu_out(const Tensor & self, const Scalar & negative_slope, Tensor & out)
{
GUARD;
double slope = negative_slope.to<double>();
Tensor self_c = self.contiguous();
dlprim::Tensor x=todp(self_c);
dlprim::Tensor y=todp(out);
dlprim::core::pointwise_operation({x},{y},{slope},"y0 = x0 > 0 ? x0 : w0 * x0;",getExecutionContext(self));
sync_if_needed(self.device());
return out;
}
// {"schema": "aten::leaky_relu_backward.grad_input(Tensor grad_output, Tensor self, Scalar negative_slope, bool self_is_result, *, Tensor(a!) grad_input) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & leaky_relu_backward_out(const Tensor & grad_output, const Tensor & self, const Scalar & negative_slope, bool /*self_is_result*/, Tensor & grad_input)
{
GUARD;
double slope = negative_slope.to<double>();
Tensor self_c = self.contiguous(),grad_output_c = grad_output.contiguous();
dlprim::Tensor y=todp(self_c);
dlprim::Tensor dy=todp(grad_output_c);
dlprim::Tensor dx=todp(grad_input);
dlprim::core::pointwise_operation({y,dy},{dx},{slope},"y0 = x0 > 0 ? x1 : w0 * x1;",getExecutionContext(self));
sync_if_needed(self.device());
return grad_input;
}
// {"schema": "aten::argmax.out(Tensor self, int? dim=None, bool keepdim=False, *, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & argmax_out(const Tensor & self, c10::optional<int64_t> dim, bool keepdim, Tensor & out)
{
GUARD;
Tensor self_c = self.contiguous();
dlprim::Tensor X = todp(self_c);
dlprim::Tensor Yind = todp(out);
std::vector<int64_t> dims;
if(dim) {
dims.push_back(*dim);
}
else {
for(int i=0;i<X.shape().size();i++)
dims.push_back(i);
}
c10::IntArrayRef sqdims(dims.data(),dims.size());
auto r = squeeze_dim(X.shape(),sqdims,keepdim);
TORCH_CHECK(r.second == Yind.shape(),"Invalid output shape");
Yind.reshape(r.first);
WSGuard tmp_guard(Yind.shape().total_size()*dlprim::size_of_data_type(X.dtype()),
self.device());
dlprim::Tensor Yval = tmp_guard.ws.sub_tensor(0,Yind.shape(),X.dtype());
dlprim::ExecutionContext q=getExecutionContext(self);
dlprim::Context ctx(q);
std::string min_val = dlprim::data_type_to_opencl_numeric_limit(X.dtype(),dlprim::dt_min_val);
auto op = dlprim::core::PointwiseOperationBroadcastReduce::create(
ctx,
{X.specs()},{Yval.specs(),Yind.specs()},
0,dlprim::float_data,
"y0=x0; y1=reduce_item;",
"reduce_y0 = " + min_val + "; reduce_y1 = -1;",
R"xxx(
if(y0 > reduce_y0) {
reduce_y0 = y0;
reduce_y1 = y1;
}
)xxx"
);
WSGuard ws_guard(op->workspace(),self.device());
op->enqueue({X},{Yval,Yind},ws_guard.ws,{},{1,1},{0,0},q);
sync_if_needed(self.device());
return out;
}
static Tensor min_or_max(const Tensor & self,bool is_min)
{
GUARD;
Tensor self_cont = self.contiguous();
dlprim::Tensor X = todp(self_cont);
Tensor result = new_tensor_as(dlprim::Shape(),self);
dlprim::Tensor Y = todp(result);
std::string y0 = dlprim::data_type_to_opencl_numeric_limit(X.dtype(),(is_min ? dlprim::dt_max_val : dlprim::dt_min_val));
dlprim::ExecutionContext q=getExecutionContext(self);
dlprim::Context ctx(q);
auto op = dlprim::core::PointwiseOperationBroadcastReduce::create(
ctx,
{X.specs()},{Y.specs()},
0,X.dtype(),
"y0=x0;",
std::string("reduce_y0 = ") + y0 + ";",
std::string("reduce_y0 = y0 ") + (is_min ? "<" : ">") + " reduce_y0 ? y0 : reduce_y0;"
);
WSGuard ws_guard(op->workspace(),self.device());
op->enqueue({X},{Y},ws_guard.ws,{},{1},{0},q);
sync_if_needed(self.device());
return result;
}
// {"schema": "aten::min(Tensor self) -> Tensor", "dispatch": "True", "default": "False"}
Tensor min(const Tensor & self)
{
return min_or_max(self,true);
}
// {"schema": "aten::max(Tensor self) -> Tensor", "dispatch": "True", "default": "False"}
Tensor max(const Tensor & self)
{
return min_or_max(self,false);
}
// {"schema": "aten::dot(Tensor self, Tensor tensor) -> Tensor", "dispatch": "True", "default": "False"}
Tensor dot(const Tensor & self, const Tensor & tensor)
{
GUARD;
Tensor self_c = self.contiguous();
Tensor tensor_c = tensor.contiguous();
dlprim::Tensor x0=todp(self_c);
dlprim::Tensor x1=todp(tensor_c);
Tensor result = new_tensor_as(dlprim::Shape(),self_c);
dlprim::Tensor y=todp(result);
auto q = getExecutionContext(self);
dlprim::Context ctx(q);
auto op = dlprim::core::PointwiseOperationBroadcastReduce::create(
ctx,
{x0.specs(),x1.specs()},{y.specs()},
0,dlprim::float_data,
"y0=x0*x1;",
"reduce_y0 = 0;",
"reduce_y0 += y0;");
WSGuard wsg(op->workspace(),self.device());
op->enqueue({x0,x1},{y},wsg.ws,{},{1},{0},q);
sync_if_needed(self.device());
return result;
}
// {"schema": "aten::ne.Scalar_out(Tensor self, Scalar other, *, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & ne_out(const Tensor & self, const Scalar & other, Tensor & out)
{
GUARD;
Tensor self_c = self.contiguous();
dlprim::Tensor x(todp(self_c));
dlprim::Tensor y(todp(out));
dlprim::core::pointwise_operation_broadcast({x},{y},{other.to<double>()},{x.dtype()},
"y0 = x0 != w0;",
getExecutionContext(self));
sync_if_needed(self.device());
return out;
}
// {"schema": "aten::ne.Tensor_out(Tensor self, Tensor other, *, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & ne_out_tensor(const Tensor & self, const Tensor & other, Tensor & out)
{
GUARD;
Tensor self_c = self.contiguous();
Tensor other_c = other.contiguous();
dlprim::Tensor x0(todp(self_c));
dlprim::Tensor x1(todp(other_c));
dlprim::Tensor y(todp(out));
dlprim::core::pointwise_operation_broadcast({x0,x1},{y},{},
"y0 = x0 != x1;",
getExecutionContext(self_c));
sync_if_needed(self.device());
return out;
}
// {"schema": "aten::eq.Scalar_out(Tensor self, Scalar other, *, Tensor(a!) out) -> Tensor(a!)", "dispatch": "True", "default": "False"}
Tensor & eq_out(const Tensor & self, const Scalar & other, Tensor & out)
{
GUARD;
Tensor self_c = self.contiguous();
dlprim::Tensor x(todp(self_c));
dlprim::Tensor y(todp(out));
dlprim::core::pointwise_operation_broadcast({x},{y},{other.to<double>()},{x.dtype()},
"y0 = x0 == w0;",