2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "luci_interpreter/core/Tensor.h"
22 namespace luci_interpreter
25 Tensor::Tensor(DataType element_type, Shape shape, AffineQuantization quantization,
27 : _element_type(element_type), _shape(std::move(shape)), _quantization(std::move(quantization)),
28 _name(std::move(name)), _data_allocated(false)
32 void Tensor::readData(void *data_ptr, size_t data_size) const
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)
38 throw std::invalid_argument("Invalid data size.");
40 assert(data_ptr != nullptr);
41 std::memcpy(data_ptr, data<void>(), data_size);
44 void Tensor::writeData(const void *data_ptr, size_t data_size)
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)
50 throw std::invalid_argument("Invalid data size.");
52 assert(data_ptr != nullptr);
53 std::memcpy(data<void>(), data_ptr, data_size);
56 void Tensor::resize(const Shape &new_shape) { _shape = new_shape; }
58 } // namespace luci_interpreter