2 * Copyright (c) 2018 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 "backend/cpu_common/Tensor.h"
19 #include "ir/DataType.h"
20 #include "backend/cpu_common/MemoryManager.h"
31 size_t Tensor::calcOffset(const ir::Coordinates &coords) const
33 size_t rank = num_dimensions();
34 rank = rank == 0 ? 1 : rank;
36 for (size_t i = 0; i < rank; ++i)
38 offset = offset * dimension(i) + coords[i];
40 offset *= sizeOfDataType(data_type());
44 void Tensor::setShape(const ir::Shape &new_shape) { _info.shape(new_shape); }
46 bool Tensor::applyShape(const ir::Shape &new_shape)
48 bool previously_dynamic = is_dynamic();
50 auto allocTensorMem = [&](bool overwrite = false) {
51 auto capacity = total_size();
52 auto alloc = _dynamic_mem_mgr->allocate(this, capacity);
55 overwriteBuffer(alloc);
60 if (!previously_dynamic)
62 // TODO deallocate tensor->buffer()
63 // issue is that staticTensorManager might have allocate this memory
68 else if (buffer() == nullptr)
74 // when buffer was already allocated and new_shape requires different size
77 auto previous_size = total_size();
78 auto new_size = new_shape.num_elements() * ir::sizeOfDataType(data_type());
79 if (previous_size != new_size)
81 _dynamic_mem_mgr->deallocate(this);
88 { // when buffer with same size was already allocated, shape could differ
95 } // namespace cpu_common
96 } // namespace backend