-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensor.cpp
More file actions
140 lines (110 loc) · 3.52 KB
/
Copy pathtensor.cpp
File metadata and controls
140 lines (110 loc) · 3.52 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
#include "tensor.hpp"
#include <stdexcept>
#include <utility>
Tensor::Tensor()
: storage_(std::make_shared<std::vector<value_type>>()),
storage_offset_(0) {}
Tensor::Tensor(Shape shape)
: shape_(std::move(shape)),
strides_(make_contiguous_strides(shape_)),
storage_(std::make_shared<std::vector<value_type>>(compute_numel(shape_))),
storage_offset_(0) {}
Tensor::Tensor(Shape shape, std::vector<value_type> values)
: shape_(std::move(shape)),
strides_(make_contiguous_strides(shape_)),
storage_(std::make_shared<std::vector<value_type>>(std::move(values))),
storage_offset_(0) {
if (storage_->size() != compute_numel(shape_)) {
throw std::invalid_argument("Tensor values do not match shape");
}
}
Tensor::Tensor(Shape shape,
Strides strides,
std::shared_ptr<std::vector<value_type>> storage,
size_type storage_offset)
: shape_(std::move(shape)),
strides_(std::move(strides)),
storage_(std::move(storage)),
storage_offset_(storage_offset) {
require_valid_storage();
}
Tensor::size_type Tensor::ndim() const noexcept {
return shape_.size();
}
Tensor::size_type Tensor::numel() const noexcept {
return compute_numel(shape_);
}
bool Tensor::empty() const noexcept {
return numel() == 0;
}
const Tensor::Shape& Tensor::shape() const noexcept {
return shape_;
}
const Tensor::Strides& Tensor::strides() const noexcept {
return strides_;
}
Tensor::size_type Tensor::storage_offset() const noexcept {
return storage_offset_;
}
Tensor::value_type* Tensor::data() noexcept {
return storage_->data() + storage_offset_;
}
const Tensor::value_type* Tensor::data() const noexcept {
return storage_->data() + storage_offset_;
}
Tensor::value_type& Tensor::operator[](size_type index) {
if (index >= numel()) {
throw std::out_of_range("Tensor index out of range");
}
return data()[index];
}
const Tensor::value_type& Tensor::operator[](size_type index) const {
if (index >= numel()) {
throw std::out_of_range("Tensor index out of range");
}
return data()[index];
}
Tensor Tensor::squeeze() const {
throw std::logic_error("Tensor::squeeze() is not implemented yet");
}
Tensor Tensor::squeeze(size_type dim) const {
(void)dim;
throw std::logic_error("Tensor::squeeze(dim) is not implemented yet");
}
Tensor Tensor::unsqueeze(size_type dim) const {
(void)dim;
throw std::logic_error("Tensor::unsqueeze(dim) is not implemented yet");
}
Tensor Tensor::reshape(Shape new_shape) const {
(void)new_shape;
throw std::logic_error("Tensor::reshape(new_shape) is not implemented yet");
}
Tensor Tensor::view(Shape new_shape) const {
(void)new_shape;
throw std::logic_error("Tensor::view(new_shape) is not implemented yet");
}
Tensor::size_type Tensor::compute_numel(const Shape& shape) {
if (shape.empty()) {
return 0;
}
size_type total = 1;
for (size_type dim : shape) {
total *= dim;
}
return total;
}
Tensor::Strides Tensor::make_contiguous_strides(const Shape& shape) {
Strides strides(shape.size(), 1);
for (size_type i = shape.size(); i > 1; --i) {
strides[i - 2] = strides[i - 1] * shape[i - 1];
}
return strides;
}
void Tensor::require_valid_storage() const {
if (!storage_) {
throw std::invalid_argument("Tensor storage cannot be null");
}
if (storage_offset_ > storage_->size()) {
throw std::out_of_range("Tensor storage offset is out of range");
}
}