Skip to content

Commit

Permalink
[relay][vm] Reuse allocated device memory
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiics committed Oct 21, 2019
1 parent d660e51 commit d7bbee1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
9 changes: 7 additions & 2 deletions include/tvm/runtime/vm.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* or more contributor license agreements. See the NOTICE file * distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
Expand Down Expand Up @@ -747,6 +746,12 @@ class VirtualMachine : public runtime::ModuleNode {

/*! \brief The parameter name to data mapping. */
std::unordered_map<std::string, ObjectRef> params_;

/*!
* \brief The constant pool for runtime. It caches the device dependent
* object to avoid rellocation of constants during inference.
*/
std::vector<ObjectRef> const_pool_;
};

} // namespace vm
Expand Down
15 changes: 12 additions & 3 deletions src/runtime/vm/vm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -795,9 +795,18 @@ void VirtualMachine::RunLoop() {
}
case Opcode::LoadConst: {
auto constant_obj = exec->constants[instr.const_index];
// TODO(wweic) ctx could be obtained from the ctxs list.
auto device_obj = CopyTo(constant_obj, ctxs[0]);
WriteRegister(instr.dst, device_obj);
// We cache the allocated object in the constant pool. To measure, the
// first iteration will set the pool up. The other iterations will
// directly reuse the allocated objects.
if (const_pool_.size() <= static_cast<size_t>(instr.const_index)) {
const_pool_.resize(instr.const_index + 1);
}

if (!const_pool_[instr.const_index].defined()) {
// TODO(wweic) ctx could be obtained from the ctxs list.
const_pool_[instr.const_index] = CopyTo(constant_obj, ctxs[0]);
}
WriteRegister(instr.dst, const_pool_[instr.const_index]);
pc++;
goto main_loop;
}
Expand Down

0 comments on commit d7bbee1

Please sign in to comment.