Repository navigation
Expand file tree
/
Copy pathtensor.cc
More file actions
198 lines (168 loc) · 6.26 KB
/
Copy pathtensor.cc
File metadata and controls
198 lines (168 loc) · 6.26 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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "arrow/tensor.h"
#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
#include <numeric>
#include <string>
#include <type_traits>
#include <vector>
#include "arrow/status.h"
#include "arrow/type.h"
#include "arrow/type_traits.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/logging.h"
#include "arrow/visitor_inline.h"
namespace arrow {
using internal::checked_cast;
static void ComputeRowMajorStrides(const FixedWidthType& type,
const std::vector<int64_t>& shape,
std::vector<int64_t>* strides) {
int64_t remaining = type.bit_width() / 8;
for (int64_t dimsize : shape) {
remaining *= dimsize;
}
if (remaining == 0) {
strides->assign(shape.size(), type.bit_width() / 8);
return;
}
for (int64_t dimsize : shape) {
remaining /= dimsize;
strides->push_back(remaining);
}
}
static void ComputeColumnMajorStrides(const FixedWidthType& type,
const std::vector<int64_t>& shape,
std::vector<int64_t>* strides) {
int64_t total = type.bit_width() / 8;
for (int64_t dimsize : shape) {
if (dimsize == 0) {
strides->assign(shape.size(), type.bit_width() / 8);
return;
}
}
for (int64_t dimsize : shape) {
strides->push_back(total);
total *= dimsize;
}
}
/// Constructor with strides and dimension names
Tensor::Tensor(const std::shared_ptr<DataType>& type, const std::shared_ptr<Buffer>& data,
const std::vector<int64_t>& shape, const std::vector<int64_t>& strides,
const std::vector<std::string>& dim_names)
: type_(type), data_(data), shape_(shape), strides_(strides), dim_names_(dim_names) {
ARROW_CHECK(is_tensor_supported(type->id()));
if (shape.size() > 0 && strides.size() == 0) {
ComputeRowMajorStrides(checked_cast<const FixedWidthType&>(*type_), shape, &strides_);
}
}
Tensor::Tensor(const std::shared_ptr<DataType>& type, const std::shared_ptr<Buffer>& data,
const std::vector<int64_t>& shape, const std::vector<int64_t>& strides)
: Tensor(type, data, shape, strides, {}) {}
Tensor::Tensor(const std::shared_ptr<DataType>& type, const std::shared_ptr<Buffer>& data,
const std::vector<int64_t>& shape)
: Tensor(type, data, shape, {}, {}) {}
const std::string& Tensor::dim_name(int i) const {
static const std::string kEmpty = "";
if (dim_names_.size() == 0) {
return kEmpty;
} else {
ARROW_CHECK_LT(i, static_cast<int>(dim_names_.size()));
return dim_names_[i];
}
}
int64_t Tensor::size() const {
return std::accumulate(shape_.begin(), shape_.end(), 1LL, std::multiplies<int64_t>());
}
bool Tensor::is_contiguous() const { return is_row_major() || is_column_major(); }
bool Tensor::is_row_major() const {
std::vector<int64_t> c_strides;
const auto& fw_type = checked_cast<const FixedWidthType&>(*type_);
ComputeRowMajorStrides(fw_type, shape_, &c_strides);
return strides_ == c_strides;
}
bool Tensor::is_column_major() const {
std::vector<int64_t> f_strides;
const auto& fw_type = checked_cast<const FixedWidthType&>(*type_);
ComputeColumnMajorStrides(fw_type, shape_, &f_strides);
return strides_ == f_strides;
}
Type::type Tensor::type_id() const { return type_->id(); }
bool Tensor::Equals(const Tensor& other, const EqualOptions& opts) const {
return TensorEquals(*this, other, opts);
}
namespace {
template <typename TYPE>
int64_t StridedTensorCountNonZero(int dim_index, int64_t offset, const Tensor& tensor) {
using c_type = typename TYPE::c_type;
c_type const zero = c_type(0);
int64_t nnz = 0;
if (dim_index == tensor.ndim() - 1) {
for (int64_t i = 0; i < tensor.shape()[dim_index]; ++i) {
auto const* ptr = tensor.raw_data() + offset + i * tensor.strides()[dim_index];
auto& elem = *reinterpret_cast<c_type const*>(ptr);
if (elem != zero) ++nnz;
}
return nnz;
}
for (int64_t i = 0; i < tensor.shape()[dim_index]; ++i) {
nnz += StridedTensorCountNonZero<TYPE>(dim_index + 1, offset, tensor);
offset += tensor.strides()[dim_index];
}
return nnz;
}
template <typename TYPE>
int64_t ContiguousTensorCountNonZero(const Tensor& tensor) {
using c_type = typename TYPE::c_type;
auto* data = reinterpret_cast<c_type const*>(tensor.raw_data());
return std::count_if(data, data + tensor.size(),
[](c_type const& x) { return x != 0; });
}
template <typename TYPE>
inline int64_t TensorCountNonZero(const Tensor& tensor) {
if (tensor.is_contiguous()) {
return ContiguousTensorCountNonZero<TYPE>(tensor);
} else {
return StridedTensorCountNonZero<TYPE>(0, 0, tensor);
}
}
struct NonZeroCounter {
NonZeroCounter(const Tensor& tensor, int64_t* result)
: tensor_(tensor), result_(result) {}
template <typename TYPE>
typename std::enable_if<!is_number_type<TYPE>::value, Status>::type Visit(
const TYPE& type) {
ARROW_CHECK(!is_tensor_supported(type.id()));
return Status::NotImplemented("Tensor of ", type.ToString(), " is not implemented");
}
template <typename TYPE>
typename std::enable_if<is_number_type<TYPE>::value, Status>::type Visit(
const TYPE& type) {
*result_ = TensorCountNonZero<TYPE>(tensor_);
return Status::OK();
}
const Tensor& tensor_;
int64_t* result_;
};
} // namespace
Status Tensor::CountNonZero(int64_t* result) const {
NonZeroCounter counter(*this, result);
return VisitTypeInline(*type(), &counter);
}
} // namespace arrow