MCPcopy Create free account
hub / github.com/apache/arrow / ComputeTensorSize

Function ComputeTensorSize

cpp/src/arrow/tensor.cc:112–140  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

110}
111
112Result<int64_t> ComputeTensorSize(std::span<const int64_t> shape,
113 std::span<const int64_t> strides, int64_t elem_size) {
114 // Check the largest offset can be computed without overflow
115 const size_t ndim = shape.size();
116 int64_t largest_offset = elem_size;
117 for (size_t i = 0; i < ndim; ++i) {
118 if (shape[i] == 0) continue;
119 if (strides[i] < 0) {
120 // TODO(mrkn): Support negative strides for sharing views
121 return Status::Invalid("negative strides not supported");
122 }
123
124 int64_t dim_offset = 0;
125 if (!internal::MultiplyWithOverflow(shape[i] - 1, strides[i], &dim_offset)) {
126 if (!internal::AddWithOverflow(largest_offset, dim_offset, &largest_offset)) {
127 continue;
128 }
129 }
130
131 return Status::Invalid(
132 "offsets computed from shape and strides would not fit in 64-bit integer");
133 }
134
135 // A dimension with no element means empty for which the preceding does not apply.
136 if (std::find(shape.begin(), shape.end(), 0) != shape.end()) {
137 return 0;
138 }
139 return largest_offset;
140}
141
142namespace {
143bool IsTensorStridesRowMajor(const std::shared_ptr<DataType>& type,

Callers 1

ComputeNumElementsMethod · 0.85

Calls 6

MultiplyWithOverflowFunction · 0.85
AddWithOverflowFunction · 0.85
InvalidFunction · 0.70
sizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected