#include <arm_compute/core/Types.h>
#include <backend/ITensorBuilder.h>
#include "model/OperandIndexMap.h"
+#include "AclMemoryManager.h"
+#include "util/config/ConfigManager.h"
+#include "cpp14/memory.h"
namespace neurun
{
std::shared_ptr<backend::operand::IObject> wrapTensor(const model::OperandIndex &ind) override;
void iterate(const IterateFunction &fn) override;
+ // TODO Pass MemoryManager to Executor instead of TensorBuilder
+ // std::unique_ptr<IMemoryManager> releaseMemoryManager(void) override;
+
std::shared_ptr<T_ITensor> at(const ::neurun::model::OperandIndex &ind);
/**
* @brief Check child tensor is allocated as subtensor of parent tensor
model::OperandIndexMap<model::OperandInfo> _tensor_info_map;
model::OperandIndexMap<compiler::SubTensorInfo> _subtensor_info_map;
model::OperandIndexMap<bool> _apply_dim_correction_map;
- model::OperandIndexMap<std::shared_ptr<T_Tensor>> _tensors;
- model::OperandIndexMap<std::shared_ptr<T_SubTensor>> _subtensors;
- model::OperandIndexMap<std::shared_ptr<T_Object>> _objects;
graph::operand::Layout _layout;
+
+ std::unique_ptr<AclMemoryManager<T_Tensor, T_SubTensor, T_Object>> _mem_mgr;
};
} // namespace acl_common
template <typename T_ITensor, typename T_Tensor, typename T_SubTensor, typename T_Object>
TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::TemplTensorBuilder()
{
- // DO NOTHING
+ _mem_mgr = nnfw::cpp14::make_unique<AclMemoryManager<T_Tensor, T_SubTensor, T_Object>>();
+ assert(_mem_mgr);
+
+// TODO After LinearAclMemoryManager lands, apply below code
+#if 0
+ const std::string executor_str =
+ config::ConfigManager::instance().get<std::string>(config::EXECUTOR);
+ if (executor_str == "Linear")
+ _mem_mgr = nnfw::cpp14::make_unique<LinearAclMemoryManager<T_Tensor, T_SubTensor, T_Object>>();
+ else
+ _mem_mgr = nnfw::cpp14::make_unique<AclMemoryManager<T_Tensor, T_SubTensor, T_Object>>();
+#endif
}
template <typename T_ITensor, typename T_Tensor, typename T_SubTensor, typename T_Object>
const model::OperandIndex &ind, const model::OperandInfo &info,
const graph::operand::Layout &layout)
{
- assert(_tensors.size() == 0);
+ assert(_mem_mgr->tensors().size() == 0);
_tensor_info_map.insert({ind, info});
_apply_dim_correction_map.insert({ind, true});
void TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::registerSubTensorInfo(
const model::OperandIndex &ind, const compiler::SubTensorInfo &info)
{
- assert(_tensors.size() == 0);
+ assert(_mem_mgr->tensors().size() == 0);
_subtensor_info_map.insert({ind, info});
_apply_dim_correction_map.insert({ind, true});
template <typename T_ITensor, typename T_Tensor, typename T_SubTensor, typename T_Object>
void TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::prepare(void)
{
- assert(_tensors.size() == 0);
+ assert(_mem_mgr->tensors().size() == 0);
// TODO Handle SubTensor(subsumption)
// Currently this TemplTensorBuilder does not have subsumption info yet
// Allocated subtensor will be mapped to _subtensors instead of _tensors
- assert(_subtensors.size() == 0);
+ assert(_mem_mgr->subtensors().size() == 0);
for (auto &entry : _tensor_info_map)
{
const auto &info = entry.second;
auto tensor_info =
asTensorInfo(info.shape(), info.typeInfo(), _layout, _apply_dim_correction_map[ind]);
- auto tensor = std::make_shared<T_Tensor>(tensor_info);
- _tensors[ind] = tensor;
+ _mem_mgr->buildTensor(ind, tensor_info);
}
// To make subtensor, parent tensor must be made first
// 3-2) If parent tensor is not made, we can't make child tensor yet
// Push parent tensor index to stack and return to 4)
// 4) If stack is empty, return to 1), else return to 2)
+ auto &tensors = _mem_mgr->tensors();
+ auto &subtensors = _mem_mgr->subtensors();
for (auto &entry : _subtensor_info_map)
{
model::OperandIndex ind = entry.first;
const auto &info = _subtensor_info_map.at(current);
// Already generated SubTensor
- if (_subtensors.find(current) != _subtensors.end())
+ if (subtensors.find(current) != subtensors.end())
{
stack.pop();
continue;
auto parent = info.parent();
std::shared_ptr<T_ITensor> parent_tensor;
- if (_tensors.find(parent) != _tensors.end())
+ if (tensors.find(parent) != tensors.end())
{
// Parent is allocated as tensor
- parent_tensor = _tensors[parent];
+ parent_tensor = tensors[parent];
}
- else if (_subtensors.find(parent) != _subtensors.end())
+ else if (subtensors.find(parent) != subtensors.end())
{
// Parent is allocated as subtensor
- parent_tensor = _subtensors[parent];
+ parent_tensor = subtensors[parent];
}
else
{
auto shape = asTensorShape(info.shape(), _layout, _apply_dim_correction_map[current]);
::arm_compute::Coordinates coordinates = asTensorCoordinate(info.offset(), _layout);
auto tensor = std::make_shared<T_SubTensor>(parent_tensor.get(), shape, coordinates, true);
- _subtensors[current] = tensor;
+ subtensors[current] = tensor;
stack.pop();
}
}
template <typename T_ITensor, typename T_Tensor, typename T_SubTensor, typename T_Object>
void TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::allocate(void)
{
- assert(_tensor_info_map.size() == _tensors.size());
+ assert(_tensor_info_map.size() == _mem_mgr->tensors().size());
- for (const auto &tensor_entry : _tensors)
- {
- auto tensor = tensor_entry.second;
- tensor->allocator()->allocate();
- }
+ _mem_mgr->allocate();
}
template <typename T_ITensor, typename T_Tensor, typename T_SubTensor, typename T_Object>
TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::tensorAt(
const model::OperandIndex &ind)
{
- if (_tensors.find(ind) != _tensors.end())
+ auto &tensors = _mem_mgr->tensors();
+ if (tensors.find(ind) != tensors.end())
{
- return _tensors.at(ind);
+ return tensors.at(ind);
}
else
{
- return _subtensors.at(ind);
+ return _mem_mgr->subtensors().at(ind);
}
}
TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::wrapTensor(
const model::OperandIndex &ind)
{
- if (_objects.find(ind) != _objects.end())
- {
- return _objects.at(ind);
- }
- else
- {
- if (_tensors.find(ind) != _tensors.end())
- {
- return _objects[ind] = std::make_shared<T_Object>(_tensors.at(ind));
- }
- else
- {
- return _objects[ind] = std::make_shared<T_Object>(_subtensors.at(ind));
- }
- }
+ return _mem_mgr->wrapTensor(ind);
}
template <typename T_ITensor, typename T_Tensor, typename T_SubTensor, typename T_Object>
void TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::iterate(
const IterateFunction &fn)
{
- for (auto it : _tensors)
+ for (auto it : _mem_mgr->tensors())
{
fn(it.first);
}
- for (auto it : _subtensors)
+ for (auto it : _mem_mgr->subtensors())
{
fn(it.first);
}
std::shared_ptr<T_ITensor> TemplTensorBuilder<T_ITensor, T_Tensor, T_SubTensor, T_Object>::at(
const ::neurun::model::OperandIndex &ind)
{
- if (_tensors.find(ind) != _tensors.end())
+ auto &tensors = _mem_mgr->tensors();
+ if (tensors.find(ind) != tensors.end())
{
- return _tensors.at(ind);
+ return tensors.at(ind);
}
else
{
- return _subtensors.at(ind);
+ return _mem_mgr->subtensors().at(ind);
}
}
return false;
}
- if (_subtensors.find(child) == _subtensors.end())
+ auto &subtensors = _mem_mgr->subtensors();
+ if (subtensors.find(child) == subtensors.end())
{
return false;
}