Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[luci-interpreter] Add StaticMemoryManager #7735

Merged
merged 3 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2021 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.
*/

#ifndef LUCI_INTERPRETER_STATIC_MEMORY_MANAGER_H
#define LUCI_INTERPRETER_STATIC_MEMORY_MANAGER_H

#include "luci_interpreter/MemoryManager.h"

namespace luci_interpreter
{

// Used for allocations in static buffer, using offsets defined in luci model.
class StaticMemoryManager : public IMemoryManager
{
public:
StaticMemoryManager() = delete;

explicit StaticMemoryManager(uint8_t *buffer_ptr) : _buffer_ptr(buffer_ptr)
{ /* Do nothing */
Copy link
Contributor

@m-bronnikov m-bronnikov Sep 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks strange, but Im okay with it =)

}

void allocate_memory(luci_interpreter::Tensor &tensor) final;
void release_memory(luci_interpreter::Tensor &tensor) final;

private:
// Stores a pointer to the beginning of the allocated memory buffer.
uint8_t *_buffer_ptr;
};

} // namespace luci_interpreter

#endif // LUCI_INTERPRETER_STATIC_MEMORY_MANAGER_H
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ class Tensor

bool is_data_allocated() const { return _data_allocated; }

int32_t get_offset() const { return _offset; }

void set_offset(int32_t offset) { _offset = offset; }

private:
DataType _element_type;
Shape _shape;
Expand All @@ -172,6 +176,9 @@ class Tensor
// Kernel configuration could disable allocation of some tensors if they are not needed for
// particular operation.
bool _is_allocatable = true;
// Used by static memory manager.
// Stores the offset from the beginning of the allocated memory buffer.
int32_t _offset = -1;
};

} // namespace luci_interpreter
Expand Down
3 changes: 2 additions & 1 deletion compiler/luci-interpreter/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ set(SOURCES
"${LUCI_INTERPRETER_INCLUDE_DIR}/luci_interpreter/Interpreter.h"
Interpreter.cpp "${LUCI_INTERPRETER_INCLUDE_DIR}/luci_interpreter/SimpleMemoryManager.h" SimpleMemoryManager.cpp
"${LUCI_INTERPRETER_INCLUDE_DIR}/luci_interpreter/TestMemoryManager.h" TestMemoryManager.cpp
"${LUCI_INTERPRETER_INCLUDE_DIR}/luci_interpreter/BuddyMemoryManager.h" BuddyMemoryManager.cpp)
"${LUCI_INTERPRETER_INCLUDE_DIR}/luci_interpreter/BuddyMemoryManager.h" BuddyMemoryManager.cpp
"${LUCI_INTERPRETER_INCLUDE_DIR}/luci_interpreter/StaticMemoryManager.h" StaticMemoryManager.cpp)

if (NOT LUCI_INTERPRETER_STATIC)
add_library(${LUCI_INTERPRETER_BINARY} SHARED ${SOURCES})
Expand Down
39 changes: 39 additions & 0 deletions compiler/luci-interpreter/src/StaticMemoryManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2021 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 "luci_interpreter/StaticMemoryManager.h"

namespace luci_interpreter
{

void StaticMemoryManager::allocate_memory(luci_interpreter::Tensor &tensor)
{
if (!tensor.is_allocatable())
{
return;
}
int32_t offset = tensor.get_offset();
assert(offset >= 0);
auto tensor_ptr = _buffer_ptr + offset;
tensor.set_data_buffer(tensor_ptr);
}

void StaticMemoryManager::release_memory(luci_interpreter::Tensor &tensor)
{
tensor.set_data_buffer(nullptr);
}

} // namespace luci_interpreter