Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / core / Tensor.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "luci_interpreter/core/Tensor.h"
18
19 #include <cstring>
20 #include <stdexcept>
21
22 namespace luci_interpreter
23 {
24
25 Tensor::Tensor(DataType element_type, Shape shape, AffineQuantization quantization,
26                std::string name)
27   : _element_type(element_type), _shape(std::move(shape)), _quantization(std::move(quantization)),
28     _name(std::move(name)), _data_allocated(false)
29 {
30 }
31
32 void Tensor::readData(void *data_ptr, size_t data_size) const
33 {
34   const size_t element_size = getDataTypeSize(element_type());
35   const int32_t num_elements = shape().num_elements();
36   if (data_size != num_elements * element_size)
37   {
38     throw std::invalid_argument("Invalid data size.");
39   }
40   assert(data_ptr != nullptr);
41   std::memcpy(data_ptr, data<void>(), data_size);
42 }
43
44 void Tensor::writeData(const void *data_ptr, size_t data_size)
45 {
46   const size_t element_size = getDataTypeSize(element_type());
47   const int32_t num_elements = shape().num_elements();
48   if (data_size != num_elements * element_size)
49   {
50     throw std::invalid_argument("Invalid data size.");
51   }
52   assert(data_ptr != nullptr);
53   std::memcpy(data<void>(), data_ptr, data_size);
54 }
55
56 void Tensor::resize(const Shape &new_shape) { _shape = new_shape; }
57
58 } // namespace luci_interpreter