Skip to content

Moved OpenCL memory allocation to runtime. #1958

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

Merged
merged 1 commit into from
Nov 8, 2018
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
39 changes: 39 additions & 0 deletions include/glow/Backends/BackendUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* 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 GLOW_BACKENDS_BACKENDUTILS_H
#define GLOW_BACKENDS_BACKENDUTILS_H

#include "glow/Backends/CompiledFunction.h"
#include "glow/IR/IR.h"

namespace glow {
/// At compile time condense constants to a single block of memory.
/// This allows the graph to go away after compile time.
/// Allocates a block of memory of size \p constantMaxSize then walks the given
/// function \p F and and copies weights to their address as specified by
/// offsets contained in \p symbolTable.
uint8_t *collectConstants(
const IRFunction *F, uint64_t constantMaxSize,
const std::unordered_map<std::string, runtime::RuntimeSymbolInfo>
&symbolTable);
/// Helper function to retrieve offset for Value: \p v from \p symbolTable.
size_t
getValueOffset(Value *v,
const std::unordered_map<std::string, runtime::RuntimeSymbolInfo>
&symbolTable);
} // end namespace glow

#endif // GLOW_BACKENDS_BACKENDUTILS_H
53 changes: 53 additions & 0 deletions lib/Backends/BackendUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* 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 "glow/Backends/BackendUtils.h"
#include "glow/IR/Instrs.h"

using namespace glow;
using llvm::cast;
using llvm::isa;

uint8_t *glow::collectConstants(
const IRFunction *F, uint64_t constantMaxSize,
const std::unordered_map<std::string, runtime::RuntimeSymbolInfo>
&symbolTable) {

// At compile time condense constants to a single block of memory.
// This allows the graph to go away after compile time.
uint8_t *baseConstantWeightVarsStore =
(uint8_t *)alignedAlloc(constantMaxSize, TensorAlignment);
for (auto &v : F->getGraph()->getParent()->getConstants()) {
assert(isa<WeightVar>(F->getWeightForNode(v)));
auto *w = cast<WeightVar>(F->getWeightForNode(v));
auto payload = v->getPayload().getUnsafePtr();
auto numBytes = w->getSizeInBytes();
auto it = symbolTable.find(std::string(w->getName()));
assert(it != symbolTable.end() && "Symbol not found.");
auto addr = it->second.offset;
// Copy weight to offset.
memcpy(baseConstantWeightVarsStore + addr, payload, numBytes);
}
return baseConstantWeightVarsStore;
}

/// Helper function, gets offset of \p v from \p symbolTable.
size_t glow::getValueOffset(
Value *v, const std::unordered_map<std::string, runtime::RuntimeSymbolInfo>
&symbolTable) {
auto it = symbolTable.find(std::string(v->getName()));
assert(it != symbolTable.end() && "Symbol not found.");
return it->second.offset;
}
7 changes: 7 additions & 0 deletions lib/Backends/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
add_library(Backends Backends.cpp)

add_library(BackendUtils BackendUtils.cpp)

target_link_libraries(BackendUtils
PRIVATE
IR)

add_subdirectory(Interpreter)

if(GLOW_WITH_OPENCL)
Expand All @@ -15,6 +21,7 @@ endif()

target_link_libraries(Backends
PRIVATE
BackendUtils
Interpreter
${linked_backends}
Base
Expand Down
Loading