Skip to content

Commit f3c9109

Browse files
committed
fixed CR comments
1 parent ade99d1 commit f3c9109

11 files changed

Lines changed: 46 additions & 30 deletions

File tree

arm_compute/core/Coordinates.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#include <algorithm>
3535
#include <array>
36+
#include <vector>
3637
#include <cstddef>
3738

3839
namespace arm_compute
@@ -55,7 +56,7 @@ class Coordinates : public Dimensions<int>
5556
* @param[in] coords Vector containing the values to initialize the dimensions.
5657
*/
5758
template <typename T>
58-
constexpr Coordinates(std::vector<T> coords) : Dimensions(coords)
59+
constexpr Coordinates(const std::vector<T>& coords) : Dimensions(coords)
5960
{
6061
}
6162

arm_compute/core/Dimensions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include <algorithm>
3434
#include <array>
35+
#include <vector>
3536
#include <functional>
3637
#include <limits>
3738
#include <numeric>

arm_compute/core/IReducibleTensor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
#include "arm_compute/core/SparseTensor.h"
2929

30+
#include <memory>
31+
3032
namespace arm_compute
3133
{
3234
/** Forward declaration of COOTensor and CSRTensor class */

arm_compute/core/SparseTensor.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@
2929
#include "arm_compute/core/Types.h"
3030

3131
#include <functional>
32+
#include <memory>
3233
#include <stdexcept>
3334

34-
typedef std::function<bool(const void *)> predicate_t;
35-
3635
namespace arm_compute
3736
{
37+
using predicate_t = std::function<bool(const void *)>;
38+
3839
/** Common base class for all sparse tensors */
3940
class SparseTensor : public ITensor
4041
{
@@ -63,7 +64,7 @@ class SparseTensor : public ITensor
6364
/** Returns the ratio of non-zero elements to the total number of elements */
6465
float density() const;
6566
/** Returns the dense volume */
66-
uint32_t dense_volume(size_t sparse_dim) const;
67+
size_t dense_volume(size_t sparse_dim) const;
6768
/** Returns the number of non zero elements */
6869
virtual size_t nnz() const = 0;
6970
/** Converts the sparse tensor to a dense tensor */

arm_compute/core/TensorFormat.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
* SOFTWARE.
2323
*/
24-
#ifndef ACL_ARM_COMPUTE_RUNTIME_TENSOR_FORMAT_H
25-
#define ACL_ARM_COMPUTE_RUNTIME_TENSOR_FORMAT_H
24+
#ifndef ACL_ARM_COMPUTE_CORE_TENSOR_FORMAT_H
25+
#define ACL_ARM_COMPUTE_CORE_TENSOR_FORMAT_H
26+
27+
namespace arm_compute
28+
{
2629

2730
enum class TensorFormat
2831
{
@@ -31,4 +34,6 @@ enum class TensorFormat
3134
CSR = 2,
3235
};
3336

34-
#endif // ACL_ARM_COMPUTE_RUNTIME_TENSOR_FORMAT_H
37+
} //namespace arm_compute
38+
39+
#endif // ACL_ARM_COMPUTE_CORE_TENSOR_FORMAT_H

arm_compute/runtime/CSRTensor.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,13 @@ class CSRTensor final : public SparseTensor, public IMemoryManageable
6464
/** The size of each index element */
6565
static constexpr size_t index_size = sizeof(int32_t);
6666

67-
/** Convert a dense tensor to sparse tensor with specified sparse dimensions using COO format.
67+
/** Convert a dense tensor to a CSR sparse tensor.
6868
*
69-
* @param[in] tensor
70-
* @param[in] sparse_dim It should belong to [1, tensor->info->num_dimensions()]
69+
* @note Only 2D tensors with NCHW layout are supported. @p sparse_dim is
70+
* ignored; CSR always uses sparse_dim = 2.
71+
*
72+
* @param[in] tensor Source dense tensor. Must be 2D and NCHW.
73+
* @param[in] sparse_dim Unused. Reserved for future use; must be 2.
7174
*/
7275
CSRTensor(const ITensor *tensor, size_t sparse_dim);
7376
/** Convert a dense tensor to a *fully* sparse tensor.

src/core/SparseTensor.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
#include "arm_compute/core/Error.h"
2727

28+
#include <numeric>
29+
2830
namespace arm_compute
2931
{
3032
SparseTensor::SparseTensor(size_t dim, size_t sparse_dim) : _total_dim(dim), _sparse_dim(sparse_dim)
@@ -48,7 +50,7 @@ float SparseTensor::sparsity() const
4850

4951
float SparseTensor::density() const
5052
{
51-
return static_cast<float>(nnz()) / static_cast<float>(info()->total_size());
53+
return static_cast<float>(nnz()) / static_cast<float>(info()->tensor_shape().total_size());
5254
}
5355

5456
size_t SparseTensor::dim() const
@@ -61,10 +63,10 @@ bool SparseTensor::is_hybrid() const
6163
return dense_dim() > 0;
6264
}
6365

64-
uint32_t SparseTensor::dense_volume(size_t sparse_dim) const
66+
size_t SparseTensor::dense_volume(size_t sparse_dim) const
6567
{
6668
const auto &ts = info()->tensor_shape();
67-
return std::accumulate(ts.begin() + sparse_dim, ts.end(), 1, std::multiplies<int>());
69+
return std::accumulate(ts.begin() + sparse_dim, ts.end(), size_t{1}, std::multiplies<size_t>());
6870
}
6971

7072
bool SparseTensor::has_non_zero_elements(uint8_t *arr, size_t len, size_t element_size, predicate_t is_non_zero) const

src/runtime/COOTensor.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ COOTensor::COOTensor(const ITensor *tensor, size_t sparse_dim) : SparseTensor(te
5252
"argument must be in [1,%zu] range. %zu is given",
5353
dim(), sparse_dim);
5454

55-
const uint8_t *data = tensor->buffer();
55+
const uint8_t *data = tensor->buffer() + info->offset_first_element_in_bytes();
5656
const size_t dense_dims = dense_dim();
5757
const auto is_nonzero = make_is_nonzero_predicate(info->data_type());
5858

@@ -75,18 +75,18 @@ COOTensor::COOTensor(const ITensor *tensor, size_t sparse_dim) : SparseTensor(te
7575
const size_t slice_size = step * element_size;
7676

7777
size_t value_byte_size = 0;
78-
size_t indices_bytes = 0;
7978
for(size_t i = 0; i < max_iter; i++)
8079
{
8180
const size_t offset = i * slice_size;
8281
if(has_non_zero_elements(const_cast<uint8_t *>(data + offset), slice_size, element_size, is_nonzero))
8382
{
8483
value_byte_size += slice_size;
85-
indices_bytes += dim() * sizeof(int32_t);
8684
}
8785
}
8886

89-
_allocator.init(coo_tensor_info(info), value_byte_size, indices_bytes);
87+
// Indices are stored in _indices (host vector); no index data is written into
88+
// the allocator buffer yet, so pass 0 for indices_bytes.
89+
_allocator.init(coo_tensor_info(info), value_byte_size, 0);
9090
_allocator.allocate();
9191

9292
for(size_t i = 0; i < max_iter; i++)
@@ -181,7 +181,7 @@ std::unique_ptr<ITensor> COOTensor::to_dense()
181181
for(size_t j = 0; j < dense_vol; ++j)
182182
{
183183
const void *value_ptr = block_ptr + j * element_size;
184-
uint8_t *base_ptr = tensor->buffer() + final_offset + j * element_size;
184+
uint8_t *base_ptr = tensor->buffer() + first_elem_offset + final_offset + j * element_size;
185185

186186
std::memcpy(base_ptr, value_ptr, element_size);
187187
}
@@ -211,13 +211,14 @@ const uint8_t *COOTensor::get_value(Coordinates coords) const
211211
for(size_t i = 0; i < _indices.size(); ++i)
212212
{
213213
const Coordinates &c = _indices[i];
214-
bool match = false;
214+
bool match = true;
215215

216216
for(size_t d = 0; d < coords.num_dimensions(); ++d)
217217
{
218-
if(c[d] == coords[d])
218+
if(c[d] != coords[d])
219219
{
220-
match = true;
220+
match = false;
221+
break;
221222
}
222223
}
223224
if(match)

src/runtime/CSRTensor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
* SOFTWARE.
2323
*/
24-
#include "arm_compute/runtime/COOTensor.h"
24+
#include "arm_compute/runtime/CSRTensor.h"
2525

2626
#include "arm_compute/core/CoreTypes.h"
2727
#include "arm_compute/core/Error.h"
@@ -75,7 +75,7 @@ CSRTensor::CSRTensor(const ITensor *tensor, size_t sparse_dim) : SparseTensor(te
7575
if(is_nonzero(data + element_offset))
7676
{
7777
_col_bytes += index_size;
78-
value_byte_size += element_size * dense_volume(sparse_dim);
78+
value_byte_size += element_size * dense_volume(this->sparse_dim());
7979
}
8080
}
8181
}

src/runtime/SparseTensorAllocator.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ SparseTensorAllocator &SparseTensorAllocator::operator=(SparseTensorAllocator &&
7070
_memory = std::move(o._memory);
7171
o._memory = Memory();
7272

73-
_values_bytes = o._values_bytes;
74-
_values_bytes = 0;
73+
_values_bytes = o._values_bytes;
74+
o._values_bytes = 0;
7575

76-
_indices_bytes = o._indices_bytes;
77-
_indices_bytes = 0;
76+
_indices_bytes = o._indices_bytes;
77+
o._indices_bytes = 0;
7878

7979
ITensorAllocator::operator=(std::move(o));
8080
}
@@ -145,7 +145,7 @@ Status SparseTensorAllocator::import_memory(void *memory)
145145
ARM_COMPUTE_RETURN_ERROR_ON(_associated_memory_group != nullptr);
146146
ARM_COMPUTE_RETURN_ERROR_ON(alignment() != 0 && !arm_compute::utility::check_aligned(memory, alignment()));
147147

148-
_memory.set_owned_region(std::make_unique<MemoryRegion>(memory, info().total_size()));
148+
_memory.set_owned_region(std::make_unique<MemoryRegion>(memory, size_bytes()));
149149
info().set_is_resizable(false);
150150

151151
return Status{};

0 commit comments

Comments
 (0)