Skip to content

Commit

Permalink
[runtime] DynamicTensor: adding DynamicTensorBuilder::alloc() (Samsun…
Browse files Browse the repository at this point in the history
…g#734)

* [runtime] DynamicTensor: adding DynamicTensorBuilder::alloc()

This introduces DynamicTensorBuilder::alloc().

Signed-off-by: Hyun Sik Yoon <[email protected]>

* move setShape() to ITensor.h for other's to use
  • Loading branch information
hyunsik-yoon authored May 14, 2020
1 parent c37e5c9 commit f1ef1a7
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 2 deletions.
15 changes: 14 additions & 1 deletion runtime/onert/backend/cpu/DynamicTensorManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,24 @@ namespace cpu
{

DynamicTensorManager::DynamicTensorManager(const std::shared_ptr<TensorRegistry> &reg)
: _tensors{reg}
: _dynamic_mem_mgr{new cpu_common::DynamicMemoryManager()}, _tensors{reg}
{
// DO NOTHING
}

void DynamicTensorManager::allocate(const ir::OperandIndex &ind, const ir::Shape &new_shape)
{
auto tensor = (*_tensors)[ind];
assert(tensor);

setShape(tensor.get(), new_shape);

auto capacity = tensor->total_size();
auto alloc = _dynamic_mem_mgr->allocate(ind, capacity);

tensor->setBuffer(alloc);
}

void DynamicTensorManager::buildTensor(const ir::OperandIndex &ind,
const ir::OperandInfo &tensor_info)
{
Expand Down
6 changes: 6 additions & 0 deletions runtime/onert/backend/cpu/DynamicTensorManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ class DynamicTensorManager : public backend::IDynamicTensorManager

virtual ~DynamicTensorManager() = default;

void allocate(const ir::OperandIndex &ind, const ir::Shape &new_shape) override;
void buildTensor(const ir::OperandIndex &ind, const ir::OperandInfo &tensor_info);

private:
/**
* @brief Memory manager for dynamic tensor.
* @todo DynamicMemoryManager is not optimized. Optimized one is needed
*/
std::shared_ptr<cpu_common::DynamicMemoryManager> _dynamic_mem_mgr;
const std::shared_ptr<TensorRegistry> _tensors;
};

Expand Down
16 changes: 16 additions & 0 deletions runtime/onert/backend/cpu/operand/Tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,22 @@ class Tensor : public ITensor
}
}

void dimension(size_t index, size_t dim) override
{
if (!(index < static_cast<size_t>(_info.shape().rank())))
{
throw std::runtime_error("index should be less than rank");
}

_info.shape().dim(index) = dim;
}

void num_dimensions(size_t rank) override
{
ir::Shape new_shape(rank); // all dims are initialized to 0 (invalid dim)
_info.shape(new_shape);
};

private:
ir::OperandInfo _info;
uint8_t *_buffer;
Expand Down
5 changes: 4 additions & 1 deletion runtime/onert/core/include/backend/IDynamicTensorManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ struct IDynamicTensorManager : public ITensorManager
virtual ~IDynamicTensorManager() = default;

public:
// TODO Add method for dynamic tensor manager, e.g., allocating memory for dynamic tensor
/**
* @brief Allocate memory for dynamic tensor
*/
virtual void allocate(const ir::OperandIndex &, const ir::Shape &) = 0;
};

} // namespace backend
Expand Down
15 changes: 15 additions & 0 deletions runtime/onert/core/include/backend/ITensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "ir/DataType.h"
#include "ir/Layout.h"
#include "ir/Shape.h"
#include "ir/Coordinates.h"

namespace onert
Expand Down Expand Up @@ -52,8 +53,22 @@ class ITensor
* kernel execution-time.
*/
virtual bool is_dynamic() const { throw std::runtime_error("not yet implemented"); }

// set dim when this tensor is dynamic
virtual void dimension(size_t /* index */, size_t /* dim */)
{
throw std::runtime_error("not yet implemented");
}

// set the rank when this tensor is dynamic.
virtual void num_dimensions(size_t /*rank*/) { throw std::runtime_error("not yet implemented"); }
};

/**
* @brief Set the shape of tenser to new_shape
*/
void setShape(ITensor *tensor, const ir::Shape &new_shape);

} // namespace backend
} // namespace onert

Expand Down
5 changes: 5 additions & 0 deletions runtime/onert/core/include/ir/OperandInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ class OperandInfo
* @return Tensor shape
*/
const Shape &shape() const { return _shape; }
/**
* @brief Return mutable tensor shape
* @return Tensor shape
*/
Shape &shape() { return _shape; }
/**
* @brief set shape
*/
Expand Down
32 changes: 32 additions & 0 deletions runtime/onert/core/src/backend/ITensor.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "backend/ITensor.h"

namespace onert
{
namespace backend
{

void setShape(ITensor *tensor, const ir::Shape &new_shape)
{
tensor->num_dimensions(new_shape.rank());
for (int i = 0; i < new_shape.rank(); i++)
tensor->dimension(i, new_shape.dim(i));
}

} // namespace backend
} // namespace onert

0 comments on commit f1ef1a7

Please sign in to comment.