-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtensor.h
More file actions
3584 lines (3187 loc) · 130 KB
/
tensor.h
File metadata and controls
3584 lines (3187 loc) · 130 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
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
// tensor.h
// Multi-dimensional tensor class
#ifndef TINYTENSOR_TENSOR_H_
#define TINYTENSOR_TENSOR_H_
#include <tt/device.h>
#include <tt/exception.h>
#include <tt/export.h>
#include <tt/index.h>
#include <tt/random.h>
#include <tt/scalar.h>
#include <tt/shape.h>
#include <cstddef>
#include <cstdint>
#include <format>
#include <functional>
#include <initializer_list>
#include <iostream>
#include <memory>
#include <optional>
#include <sstream>
#include <string>
#include <type_traits>
#include <vector>
namespace tinytensor {
// Forward declarations
class Tensor;
class StorageBase;
template <typename T>
class CheckedVec;
using TensorList = CheckedVec<Tensor>;
namespace autograd {
struct SharedGrad;
template <typename T>
struct TensorFunction;
// Hook to apply on the computed gradient
using GradHook = std::function<void(Tensor &grad)>;
auto build_dag(const Tensor &tensor) -> TensorList;
void backward(Tensor &tensor, const Tensor &grad, bool retain_graph);
void calc_grad_input(Tensor &tensor, bool retain_graph);
void add_grad(Tensor &tensor, const Tensor &grad);
} // namespace autograd
// Auxiliary options for Tensors, enables chaining of setting options and
// storing as a variable for use with multiple Tensor creations
class TINYTENSOR_EXPORT TensorOptions {
public:
TensorOptions() = default;
// Converting constructors
explicit TensorOptions(Device device)
: device_(device) {}
explicit TensorOptions(ScalarType dtype)
: dtype_(dtype) {}
explicit TensorOptions(bool requires_grad)
: requires_grad_(requires_grad) {}
auto dtype(ScalarType dtype) -> TensorOptions & {
dtype_ = dtype;
return *this;
}
[[nodiscard]] auto dtype() const -> ScalarType {
return dtype_;
}
auto device(Device device) -> TensorOptions & {
device_ = device;
return *this;
}
[[nodiscard]] auto device() const -> Device {
return device_;
}
auto requires_grad(bool requires_grad) -> TensorOptions & {
requires_grad_ = requires_grad;
return *this;
}
[[nodiscard]] auto requires_grad() const -> bool {
return requires_grad_;
}
private:
ScalarType dtype_ = kF32;
Device device_ = kCPU;
bool requires_grad_ = false;
};
// Auxiliary options for clamp
class TINYTENSOR_EXPORT ClampOptions {
public:
template <IsScalarType T>
auto min(T min_value) -> ClampOptions & {
_min = Scalar(min_value);
return *this;
}
template <IsScalarType T>
auto max(T max_value) -> ClampOptions & {
_max = Scalar(max_value);
return *this;
}
[[nodiscard]] inline auto min() const -> std::optional<Scalar> {
return _min;
}
[[nodiscard]] inline auto max() const -> std::optional<Scalar> {
return _max;
}
[[nodiscard]] auto min_to(ScalarType dtype) const -> std::optional<Scalar>;
[[nodiscard]] auto max_to(ScalarType dtype) const -> std::optional<Scalar>;
private:
std::optional<Scalar> _min;
std::optional<Scalar> _max;
};
// Multi-Dimensional Tensor class
class TINYTENSOR_EXPORT Tensor {
public:
/**
* Construct from a vector
* @param data The vector data
* @param shape The shape to represent the data
* @param device The device for the Tensor
* @param requires_grad Flag if autograd operations should be recorded
*/
template <typename T>
requires(IsScalarType<T> || std::is_same_v<T, bool>)
Tensor(const std::vector<T> &data, Shape shape, Device device, bool requires_grad = false);
/**
* Construct from a vector
* @param data The vector data
* @param shape The shape to represent the data
* @param device The device for the Tensor
* @param requires_grad Flag if autograd operations should be recorded
*/
template <typename T>
requires(IsScalarType<T> || std::is_same_v<T, bool>)
Tensor(std::vector<T> &&data, Shape shape, Device device, bool requires_grad = false);
/**
* Construct from an initializer_list
* @param data The initializer list of data
* @param shape The shape to represent the data
* @param device The device for the Tensor
* @param requires_grad Flag if autograd operations should be recorded
*/
template <typename T>
requires(IsScalarType<T> || std::is_same_v<T, bool>)
Tensor(const std::initializer_list<T> &data, Shape shape, Device device, bool requires_grad = false)
: Tensor(std::vector<T>(data), shape, device, requires_grad) {}
/**
* Construct from a vector, with inferred flattened shape
* @param data The vector data
* @param device The device for the Tensor
* @param requires_grad Flag if autograd operations should be recorded
*/
template <typename T>
requires(IsScalarType<T> || std::is_same_v<T, bool>)
Tensor(const std::vector<T> &data, Device device, bool requires_grad = false)
: Tensor(data, {static_cast<int>(data.size())}, device, requires_grad) {}
/**
* Construct from a vector, with inferred flattened shape
* @param data The vector data
* @param device The device for the Tensor
* @param requires_grad Flag if autograd operations should be recorded
*/
template <typename T>
requires(IsScalarType<T> || std::is_same_v<T, bool>)
Tensor(std::vector<T> &&data, Device device, bool requires_grad = false);
/**
* Construct from an initializer list, with inferred flattened shape
* @param data The initializer list of data
* @param device The device for the Tensor
* @param requires_grad Flag if autograd operations should be recorded
*/
template <typename T>
requires(IsScalarType<T> || std::is_same_v<T, bool>)
Tensor(const std::initializer_list<T> &data, Device device, bool requires_grad = false)
: Tensor(std::vector<T>(data), device, requires_grad) {}
/**
* Construct from a Scalar value
* @param scalar The scalar value
* @param device The device for the Tensor
*/
Tensor(Scalar scalar, Device device, bool requires_grad = false);
// Internal constructor for backends
Tensor(std::shared_ptr<StorageBase> storage, ScalarType dtype, Shape shape, Device device);
/**
* Set the internal data from another tensor
* @note This is useful to change the underlying state while keeping external
* references valid
* @param other The other Tensor to set from
*/
void set_from(const Tensor &other);
~Tensor() = default;
/**
* Serialize the current tensor into the given stream
*/
auto serialize() const -> std::vector<char>;
/**
* Deserialize the given data to replace the contents of the current tensor
* @note This change will NOT reflect to other references/views of the current
* tensor
* @note The underlying dtype of the tensor may also change, depending on the
* contents of the serialized data, but the underlying device will NOT change
* @param serialized_data The serialized tensor
*/
void deserialize(const std::vector<char> &serialized_data);
/**
* Check if Tensor has underlying storage
* @note this is used for debugging
*/
[[nodiscard]] auto has_storage() const -> bool;
/**
* Get the dtype of the Tensor
*/
[[nodiscard]] auto dtype() const -> ScalarType;
/**
* Get the device of the Tensor
*/
[[nodiscard]] auto device() const -> Device;
/**
* Get the storage offset of the Tensor
*/
[[nodiscard]] auto offset() const -> int;
/**
* Get the shape of the Tensor
*/
[[nodiscard]] auto shape() const -> const Shape &;
/**
* Get the stride of the Tensor
*/
[[nodiscard]] auto stride() const -> const Shape &;
/**
* Get the total number of elements the Tensors contains
*/
[[nodiscard]] auto numel() const -> int;
/**
* Get the number of dimensions the Tensor represents
*/
[[nodiscard]] auto dim() const -> int;
/**
* Get the size of a dimension
* @param dim The dimension
*/
[[nodiscard]] auto size(int dim) const -> int;
/**
* Check if the underlying storage of the Tensor represents is contiguous
*/
[[nodiscard]] auto is_contiguous() const -> bool;
/**
* Return a copy with the same values but contiguous storage
* @note If the Tensor is already contiguous, a shallow copy of self is
* performed with the resulting Tensor containing the same underlying storage
*/
[[nodiscard]] auto contiguous() const -> Tensor;
/**
* Get the underlying storage derived class as a result of a cast
* @note Used in backend methods to recover type from the pointer
*/
template <typename T>
[[nodiscard]] auto get_storage() const -> T & {
return *static_cast<T *>(storage_.get());
}
/**
* Gets the first value in the Tensor type erased into a Scalar
*/
[[nodiscard]] auto item() const -> Scalar;
/**
* Gets the first value in the Tensor casted to the templated type
*/
template <typename T>
requires(IsScalarType<T> || std::is_same_v<T, bool>)
[[nodiscard]] auto item() const -> T {
return item().to<T>();
}
/**
* Gets the first value in the Tensor casted to the templated scalar type
*/
template <ScalarType T>
[[nodiscard]] auto item() const -> to_ctype_t<T> {
return item().to<std::conditional_t<T == kBool, bool, to_ctype_t<T>>>();
}
/**
* Cast the underlying storage to the given scalar type
* @note If the Tensor is already the given scalar type, a shallow copy of
* self is performed with the resulting Tensor containing the same underlying
* storage
* @param dtype The scalar type to cast the storage to
* @return The casted Tensor
*/
[[nodiscard]] auto to(ScalarType dtype) const -> Tensor;
/**
* Create a copy of the underlying Tensor on the given device
* @note If the device matches the current device, a shallow copy is made
* @param device The device to copy the tensor to
* @return The Tensor
*/
[[nodiscard]] auto to(Device device) const -> Tensor;
/**
* Return a deep clone of the Tensor with same values but separate underlying
* storage
* @note This is an autograd-aware operation
*/
[[nodiscard]] auto clone() const -> Tensor;
/**
* Return a new tensor that is removed from the computation graph
* @note This does not share the underlying data, so inplace operations are
* safe on the returned tensor
*/
[[nodiscard]] auto detach() const -> Tensor;
/**
* Get address of the underlying pointer, helpful to check if tensor points to
* the same storage after various operations
*/
[[nodiscard]] auto data_ptr() const -> uintptr_t;
/**
* Get the version count of the Tensor
* Inplace operations will increment the version count, with Tensors created
* with version_count = 0
* @TODO: This may not actually be needed as we do disable inplace ops on
* tensors requiring grads
*/
[[nodiscard]] auto version_count() const -> int;
/**
* Get the underlying values as a flattened vector
* @note This will cast the underlying data element by element to the casted
* type, irrespective of the underlying storage type
*/
template <typename T>
requires(IsScalarType<T> || std::is_same_v<T, bool>)
[[nodiscard]] auto to_vec() const -> std::vector<T>;
// Copy, shallow by default
Tensor(const Tensor &rhs) = default;
Tensor(Tensor &&rhs) = default;
// Assignment
/**
* Assign all values of the Tensor to the given value
*/
template <typename T>
requires(IsScalarType<T> || std::is_same_v<T, bool>)
auto operator=(T rhs) -> Tensor & {
this->operator=(Scalar(rhs, scalar_type_));
return *this;
}
/**
* Assign all values of the Tensor to the given value
*/
template <typename T>
requires(IsScalarType<T> || std::is_same_v<T, bool>)
auto fill_(T rhs) -> Tensor & {
this->operator=(Scalar(rhs, scalar_type_));
return *this;
}
/**
* Assign all values of the Tensor to the given Scalar
* @note Since this is an in-place operation, this will throw an exception on
* tensors requiring grads
*/
auto operator=(const Scalar &rhs) -> Tensor &;
/**
* Assign all values of the Tensor to the given Scalar
* @note Since this is an in-place operation, this will throw an exception on
* tensors requiring grads
*/
auto fill_(const Scalar &rhs) -> Tensor &;
/**
* Assignment on lvalues behaves like a copy constructor
*/
auto operator=(const Tensor &rhs) & -> Tensor &;
auto operator=(Tensor &&rhs) & -> Tensor &;
/**
* Assignment on rvalues will assign the rhs data into the self view, to allow
* indexing with assignment If RHS is a different type than self, it will be
* casted to match the self scalar type
* @note Since this is an in-place operation, this will throw an exception on
* tensors requiring grads
*/
auto operator=(const Tensor &rhs) && -> Tensor &;
auto operator=(Tensor &&rhs) && -> Tensor &;
/**
* Element-wise inplace arithmetic
* This will cast RHS to the type of LHS
* @note Since this is an in-place operation, this will throw an exception on
* tensors requiring grads
*/
template <IsScalarType T>
auto operator+=(T rhs) -> Tensor & {
return this->operator+=(Scalar(rhs, scalar_type_));
}
auto operator+=(const Scalar &rhs) -> Tensor &;
auto operator+=(const Tensor &rhs) -> Tensor &;
template <IsScalarType T>
auto operator-=(T rhs) -> Tensor & {
return this->operator-=(Scalar(rhs, scalar_type_));
}
auto operator-=(const Scalar &rhs) -> Tensor &;
auto operator-=(const Tensor &rhs) -> Tensor &;
template <IsScalarType T>
auto operator*=(T rhs) -> Tensor & {
return this->operator*=(Scalar(rhs, scalar_type_));
}
auto operator*=(const Scalar &rhs) -> Tensor &;
auto operator*=(const Tensor &rhs) -> Tensor &;
template <IsScalarType T>
auto operator/=(T rhs) -> Tensor & {
return this->operator/=(Scalar(rhs, scalar_type_));
}
auto operator/=(const Scalar &rhs) -> Tensor &;
auto operator/=(const Tensor &rhs) -> Tensor &;
/**
* Index tensor with a boolean mask
* @note This returns a new Tensor with separate underlying storage
* @note The result is a flat Tensor
* @param mask The boolean mask
* @return The indexed Tensor
*/
[[nodiscard]] auto operator[](const Tensor &mask) const -> Tensor;
/**
* Index tensor with either an integer index or Slice
* @note This will always return a view of the Tensor, sharing the underlying
* storage
* @note Indexing a 1D Tensor will return another 1D Tensor, unlike Torch
* which returns scalar values
* @index The Index (integer or Slice)
* @return The indexed Tensor
*/
[[nodiscard]] auto operator[](const indexing::Index &index) const -> Tensor;
/**
* Index tensor with a mixed sequence of integer indices or Slices
* @note This will always return a view of the Tensor, sharing the underlying
* storage
* @note Indexing a 1D Tensor will return another 1D Tensor, unlike Torch
* which returns scalar values
* @indices The indices (integers and/or Slices)
* @return The indexed Tensor
*/
[[nodiscard]] auto operator[](const std::vector<indexing::Index> &indices) const -> Tensor;
// ------------------------------------------------
// Range based loop iterator support
// ------------------------------------------------
class Iterator {
public:
Iterator(Tensor &tensor, int idx)
: tensor_(tensor), idx_(idx) {}
auto operator!=(const Iterator &other) const -> bool;
auto operator++() -> Iterator;
auto operator*() const -> Tensor;
private:
Tensor &tensor_; // NOLINT(*-ref-data-members)
int idx_;
};
auto begin() -> Iterator;
auto end() -> Iterator;
// ------------------------------------------------
// Autograd related operations
// ------------------------------------------------
/**
* Check if gradient tracking is set for this Tensor
* @return True if gradients are being tracked, false otherwise
*/
[[nodiscard]] auto requires_grad() const -> bool;
/**
* Set the flag for requiring the Tensor's gradient to be computed
* @note: Only floating point type Tensors support gradient tracking
* @param set_grad True to track gradients for this Tensor, false otherwise
*/
void set_requires_grad(bool set_grad);
/**
* Get the optionally stored gradient for the underlying Tensor
* @return The gradient Tensor if exists, std::nullopt otherwise
*/
[[nodiscard]] auto grad() const -> const std::optional<Tensor> &;
/**
* Clears the Tensor's stored gradient
*/
void clear_grad();
/**
* Accumulate the given gradient to the Tensor's gradient
* @param grad The grad to accumulate with
*/
void add_grad(const Tensor &grad);
/**
* Register a hook to apply to gradient after being computed
* @hook Function-like hook
*/
void register_hook(const autograd::GradHook &hook);
/**
* Perform backward pass computing gradients starting from this Tensor
* @param grad The gradient of the function with respect to the current
* Tensor. If omitted, it will default to ones_like(self)
* @param retain_graph If false, will free the underlying computation graph
* after performing backward pass
*/
void backward(const std::optional<Tensor> &grad = {}, bool retain_graph = false);
/**
* Check if the current Tensor is a leaf
* Tensors with requires_grad=False are leaf tensors
* Tensors with requires_grad=True and are not results of autograd ops are
* also leaf tensors
*/
[[nodiscard]] auto is_leaf() const -> bool;
// ------------------------------------------------
// Shape Modification operators
// ------------------------------------------------
/**
* Expands the Tensor to the given shape, which can include a larger number of
* dimensions New dimensions are appended to the front, and any non-singleton
* existing dimension must match the expanded-to dimension
* @note: This will always return a view of the Tensor, sharing the underlying
* storage
* @note: This is equivalent to calling expand(tensor, shape)
* @note: See
* https://pytorch.org/docs/stable/generated/torch.Tensor.expand.html
* @param shape The shape to expand to
* @return View of the expanded Tensor
*/
[[nodiscard]] auto broadcast_to(const Shape &shape) const -> Tensor;
/**
* Expands the Tensor to the given shape, which can include a larger number of
* dimensions New dimensions are appended to the front, and any non-singleton
* existing dimension must match the expanded-to dimension
* @note: This will always return a view of the Tensor, sharing the underlying
* storage
* @note: See
* https://pytorch.org/docs/stable/generated/torch.Tensor.expand.html
* @param shape The shape to expand to
* @return View of the expanded Tensor
*/
[[nodiscard]] auto expand(const Shape &shape) const -> Tensor;
/**
* Removes a dimension of size one at a given dimension location to the Tensor
* @note: This has the same view or copy properties as reshape
* @note: If the given dimension to squeeze is not of size 1, the Tensor is
* unchanged
* @param dim The index to remove the singleton dimension
* @return View of the Tensor with removed dimension
*/
[[nodiscard]] auto squeeze(int dim) const -> Tensor;
/**
* Inserts a dimension of size one at a given dimension location to the Tensor
* @note: This has the same view or copy properties as reshape
* @param dim The index to insert the singleton dimension
* @return View of the Tensor with inserted dimension
*/
[[nodiscard]] auto unsqueeze(int dim) const -> Tensor;
/**
* Reshapes the Tensor to the specified shape, keeping the same underlying
* data and number of elements
* @note: This will return a view of the Tensor if possible (i.e. contiguous),
* or a copy of the underlying storage
* @param shape The shape of the resulting Tensor to reshape to
* @return The reshaped Tensor
*/
[[nodiscard]] auto reshape(const Shape &shape) const -> Tensor;
/**
* Flattens the Tensor into a one-dimensional Tensor, equivalent to
* reshape({tensor.numel()}),
* @note: If start_dim and end_dim are given, only dimensions between
* start/end are flattened
* @note: This has the same view or copy properties as reshape
* @param start_dim Starting dim to flatten
* @param end_dim Ending dim to flatten
* @return View of flattened Tensor
*/
[[nodiscard]] auto flatten(int start_dim = 0, int end_dim = -1) const -> Tensor;
/**
* Permutes the Tensor along its dimensions
* @note: This will always return a view of the Tensor, sharing the underlying
* storage
* @param dims The permutation of the input tensor dimensions the new Tensor
* should take
* @return View of the permuted Tensor
*/
[[nodiscard]] auto permute(const std::vector<int> &dims) const -> Tensor;
// ------------------------------------------------
// Inplace Unary operations
// ------------------------------------------------
/**
* Performs element-wise absolute of the self Tensor
* @return reference to self
*/
auto abs_() -> Tensor &;
/**
* Performs element-wise negation of the self Tensor
* @return reference to self
*/
auto negate_() -> Tensor &;
/**
* Performs element-wise logical not on the self Tensor
* @note The dtype of the self Tensor must be boolean
* @note This function does not have autograd support
* @return reference to self
*/
auto logical_not_() -> Tensor &;
/**
* Performs element-wise sign of the self Tensor
* @note see https://en.wikipedia.org/wiki/Sign_function
* @return reference to self
*/
auto sign_() -> Tensor &;
/**
* Performs element-wise natural logarithm of the self Tensor
*/
auto log_() -> Tensor &;
/**
* Performs element-wise base 10 logarithm of the self Tensor
* @return reference to self
*/
auto log10_() -> Tensor &;
/**
* Performs element-wise base 2 logarithm of the self Tensor
* @return reference to self
*/
auto log2_() -> Tensor &;
/**
* Performs element-wise natural logarithm of one plus the self Tensor
* @return reference to self
*/
auto log1p_() -> Tensor &;
/**
* Performs element-wise exponential of the self Tensor
* @return reference to self
*/
auto exp_() -> Tensor &;
/**
* Performs element-wise base 2 exponential of the input Tensor
* @return reference to self
*/
auto exp2_() -> Tensor &;
/**
* Performs element-wise exp(x)-1 of the input Tensor
* @return reference to self
*/
auto expm1_() -> Tensor &;
/**
* Performs element-wise square root of the input Tensor
* @return reference to self
*/
auto sqrt_() -> Tensor &;
/**
* Performs element-wise sine of the input Tensor
* @return reference to self
*/
auto sin_() -> Tensor &;
/**
* Performs element-wise cosine of the input Tensor
* @return reference to self
*/
auto cos_() -> Tensor &;
/**
* Performs element-wise tangent of the input Tensor
* @return reference to self
*/
auto tan_() -> Tensor &;
/**
* Performs element-wise trigonometric inverse sine of the input Tensor
* @return reference to self
*/
auto asin_() -> Tensor &;
/**
* Performs element-wise trigonometric inverse cosine of the input Tensor
* @return reference to self
*/
auto acos_() -> Tensor &;
/**
* Performs element-wise trigonometric inverse tangent of the input Tensor
* @return reference to self
*/
auto atan_() -> Tensor &;
/**
* Performs element-wise hyperbolic sine of the input Tensor
* @return reference to self
*/
auto sinh_() -> Tensor &;
/**
* Performs element-wise hyperbolic cosine of the input Tensor
* @return reference to self
*/
auto cosh_() -> Tensor &;
/**
* Performs element-wise hyperbolic tangent of the input Tensor
* @return reference to self
*/
auto tanh_() -> Tensor &;
/**
* Performs element-wise inverse hyperbolic sine of the input Tensor
* @return reference to self
*/
auto asinh_() -> Tensor &;
/**
* Performs element-wise inverse hyperbolic cosine of the input Tensor
* @return reference to self
*/
auto acosh_() -> Tensor &;
/**
* Performs element-wise inverse hyperbolic tangent of the input Tensor
* @return reference to self
*/
auto atanh_() -> Tensor &;
/**
* Performs element-wise error function of the input Tensor
* @note https://en.wikipedia.org/wiki/Error_function
* @return reference to self
*/
auto erf_() -> Tensor &;
/**
* Performs element-wise complementary error function of the input Tensor
* @note https://en.wikipedia.org/wiki/Error_function
* @return reference to self
*/
auto erfc_() -> Tensor &;
/**
* Performs element-wise gamma function of the input Tensor
* @note https://en.wikipedia.org/wiki/Gamma_function
* @return reference to self
*/
auto tgamma_() -> Tensor &;
/**
* Performs element-wise logarithm of the gamma function of the input Tensor
* @note https://en.wikipedia.org/wiki/Gamma_function
* @return reference to self
*/
auto lgamma_() -> Tensor &;
/**
* Performs element-wise digamma function (derivative of log gamma) of the
* input Tensor
* @note https://en.wikipedia.org/wiki/Digamma_function
* @note This function does not currently have autograd support
* @return reference to self
*/
auto digamma_() -> Tensor &;
/**
* Performs element-wise ceiling of the input Tensor
* @note This function does not have autograd support
* @return reference to self
*/
auto ceil_() -> Tensor &;
/**
* Performs element-wise floor of the input Tensor
* @note This function does not have autograd support
* @return reference to self
*/
auto floor_() -> Tensor &;
/**
* Performs element-wise rounding to nearest integer of the input Tensor
* @note Rounding halfway cases away from zero
* @note This function does not have autograd support
* @return reference to self
*/
auto round_() -> Tensor &;
// ------------------------------------------------
// Inplace Activation functions
// ------------------------------------------------
/**
* Performs element-wise sigmoid of the input Tensor
* @note https://en.wikipedia.org/wiki/Sigmoid_function
* @return reference to self
*/
auto sigmoid_() -> Tensor &;
/**
* Performs element-wise log-sigmoid of the input Tensor
* @note https://en.wikipedia.org/wiki/Sigmoid_function
* @return reference to self
*/
auto log_sigmoid_() -> Tensor &;
/**
* Performs element-wise Hardsigmoid of the input Tensor
* @note https://pytorch.org/docs/stable/generated/torch.nn.Hardsigmoid.html
* @return reference to self
*/
auto hardsigmoid_() -> Tensor &;
/**
* Performs element-wise softplus of the input Tensor
* @note Softplus is a smooth approximation of the ReLU function (always
* positive)
* @note https://pytorch.org/docs/stable/generated/torch.nn.Softplus.html
* @param beta The beta value for Softplus
* @param threshold Values above this revert to a linear function
* @return reference to self
*/
auto softplus_(double beta = 1, double threshold = 20) -> Tensor &;
/**
* Performs element-wise rectified linear unit function of the input Tensor
* @note https://pytorch.org/docs/stable/generated/torch.nn.ReLU.html
* @return reference to self
*/
auto relu_() -> Tensor &;
/**
* Performs element-wise ReLU6 of the input Tensor
* @note https://pytorch.org/docs/stable/generated/torch.nn.ReLU6.html
* @return reference to self
*/
auto relu6_() -> Tensor &;
/**
* Performs element-wise LeakyReLU of the input Tensor
* @note https://pytorch.org/docs/stable/generated/torch.nn.LeakyReLU.html
* @param negative_slop The cangle of the negative slope, used for negative
* inputs
* @return reference to self
*/
auto leaky_relu_(double negative_slope = 0.01) -> Tensor &;
/**
* Performs element-wise Exponential Linear Unit (ELU) of the input Tensor
* @note https://pytorch.org/docs/stable/generated/torch.nn.ELU.html
* @param alpha The alpha value for the ELU formulation
* @return reference to self
*/
auto elu_(double alpha = 1) -> Tensor &;
/**
* Performs element-wise SELU of the input Tensor
* @note https://pytorch.org/docs/stable/generated/torch.nn.SELU.html
* @return reference to self
*/
auto selu_() -> Tensor &;
/**
* Performs element-wise Sigmoid Linear Unit (SiLU) of the input Tensor
* @note https://pytorch.org/docs/stable/generated/torch.nn.SiLU.html
* @return reference to self
*/
auto silu_() -> Tensor &;
/**
* Performs element-wise HardTanh of the input Tensor
* @note https://pytorch.org/docs/stable/generated/torch.nn.Hardtanh.html
* @param min Minimum value of the linear region range
* @param max Maximum value of the linear region range
* @return reference to self
*/
auto hardtanh_(double min = -1, double max = 1) -> Tensor &;
/**
* Performs element-wise Softsign of the input Tensor
* @note https://pytorch.org/docs/stable/generated/torch.nn.Softsign.html
* @return reference to self
*/
auto softsign_() -> Tensor &;
/**
* Performs element-wise softmax over a dimension of the input Tensor
* @note The elements along the given dimension are rescaled so that they lie
* in the range [0,1] and sum to 1
* @note https://pytorch.org/docs/stable/generated/torch.nn.Softmax.html
* @param dim The dimension along which the Softmax will be computed
* @return reference to self
*/
auto softmax_(int dim) -> Tensor &;
/**
* Performs element-wise log-softmax over a dimension of the input Tensor
* @note https://pytorch.org/docs/stable/generated/torch.nn.LogSoftmax.html
* @param dim The dimension along which the log-softmax will be computed
* @return reference to self
*/
auto log_softmax_(int dim) -> Tensor &;
// ------------------------------------------------
// Inplace Binary operations
// ------------------------------------------------
/**
* Performs element-wise addition
* @param rhs The other tensor
* @return reference to self
*/
auto add_(const Tensor &rhs) -> Tensor &;
/**
* Performs element-wise addition
* @param rhs The other scalar value
* @return reference to self
*/
template <IsScalarType T>
auto add_(T rhs) -> Tensor & {
return add_(Scalar(rhs, scalar_type_));
}
/**
* Performs element-wise addition
* @param rhs The other scalar value
* @return reference to self