Skip to content

Commit e8d9562

Browse files
committed
Moved OpenCL memory allocation to runtime.
Created new BackendUtils library and put collectConstants there.. Added helper function to retrieve symbol offset by value from symbolTable
1 parent d5c25ac commit e8d9562

File tree

6 files changed

+286
-187
lines changed

6 files changed

+286
-187
lines changed

include/glow/Backends/BackendUtils.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright (c) 2017-present, Facebook, Inc.
3+
*
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
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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.
15+
*/
16+
#ifndef GLOW_BACKENDS_BACKENDUTILS_H
17+
#define GLOW_BACKENDS_BACKENDUTILS_H
18+
19+
#include "glow/Backends/CompiledFunction.h"
20+
#include "glow/IR/IR.h"
21+
22+
namespace glow {
23+
/// At compile time condense constants to a single block of memory.
24+
/// This allows the graph to go away after compile time.
25+
/// Allocates a block of memory of size \p constantMaxSize then walks the given
26+
/// function \p F and and copies weights to their address as specified by
27+
/// offsets contained in \p symbolTable.
28+
uint8_t *collectConstants(
29+
const IRFunction *F, uint64_t constantMaxSize,
30+
const std::unordered_map<std::string, runtime::RuntimeSymbolInfo>
31+
&symbolTable);
32+
/// Helper function to retrieve offset for Value: \p v from \p symbolTable.
33+
size_t
34+
getValueOffset(Value *v,
35+
const std::unordered_map<std::string, runtime::RuntimeSymbolInfo>
36+
&symbolTable);
37+
} // end namespace glow
38+
39+
#endif // GLOW_BACKENDS_BACKENDUTILS_H

lib/Backends/BackendUtils.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Copyright (c) 2017-present, Facebook, Inc.
3+
*
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
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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.
15+
*/
16+
#include "glow/Backends/BackendUtils.h"
17+
#include "glow/IR/Instrs.h"
18+
19+
using namespace glow;
20+
using llvm::cast;
21+
using llvm::isa;
22+
23+
uint8_t *glow::collectConstants(
24+
const IRFunction *F, uint64_t constantMaxSize,
25+
const std::unordered_map<std::string, runtime::RuntimeSymbolInfo>
26+
&symbolTable) {
27+
28+
// At compile time condense constants to a single block of memory.
29+
// This allows the graph to go away after compile time.
30+
uint8_t *baseConstantWeightVarsStore =
31+
(uint8_t *)alignedAlloc(constantMaxSize, TensorAlignment);
32+
for (auto &v : F->getGraph()->getParent()->getConstants()) {
33+
assert(isa<WeightVar>(F->getWeightForNode(v)));
34+
auto *w = cast<WeightVar>(F->getWeightForNode(v));
35+
auto payload = v->getPayload().getUnsafePtr();
36+
auto numBytes = w->getSizeInBytes();
37+
auto it = symbolTable.find(std::string(w->getName()));
38+
assert(it != symbolTable.end() && "Symbol not found.");
39+
auto addr = it->second.offset;
40+
// Copy weight to offset.
41+
memcpy(baseConstantWeightVarsStore + addr, payload, numBytes);
42+
}
43+
return baseConstantWeightVarsStore;
44+
}
45+
46+
/// Helper function, gets offset of \p v from \p symbolTable.
47+
size_t glow::getValueOffset(
48+
Value *v, const std::unordered_map<std::string, runtime::RuntimeSymbolInfo>
49+
&symbolTable) {
50+
auto it = symbolTable.find(std::string(v->getName()));
51+
assert(it != symbolTable.end() && "Symbol not found.");
52+
return it->second.offset;
53+
}

lib/Backends/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
add_library(Backends Backends.cpp)
22

3+
add_library(BackendUtils BackendUtils.cpp)
4+
5+
target_link_libraries(BackendUtils
6+
PRIVATE
7+
IR)
8+
39
add_subdirectory(Interpreter)
410

511
if(GLOW_WITH_OPENCL)
@@ -15,6 +21,7 @@ endif()
1521

1622
target_link_libraries(Backends
1723
PRIVATE
24+
BackendUtils
1825
Interpreter
1926
${linked_backends}
2027
Base

0 commit comments

Comments
 (0)