!772 GeTensor aligned addr & zero copy support
From: @chen_yemeng Reviewed-by: @sheng-nan,@wqtshg Signed-off-by: @wqtshgpull/772/MERGE
commit
b65e4eb25f
@ -0,0 +1,69 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2019-2020 Huawei Technologies Co., Ltd
|
||||||
|
*
|
||||||
|
* 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 "graph/manager/host_mem_allocator.h"
|
||||||
|
#include "framework/common/debug/ge_log.h"
|
||||||
|
#include "common/ge/ge_util.h"
|
||||||
|
|
||||||
|
namespace ge {
|
||||||
|
const void *HostMemAllocator::Malloc(const std::shared_ptr<AlignedPtr> &aligned_ptr, size_t size) {
|
||||||
|
if (aligned_ptr == nullptr) {
|
||||||
|
GELOGW("Insert a null aligned_ptr");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
GELOGD("allocate existed host memory succ, size=%zu", size);
|
||||||
|
allocated_blocks_[aligned_ptr->Get()] = { size, aligned_ptr };
|
||||||
|
return aligned_ptr->Get();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t *HostMemAllocator::Malloc(size_t size) {
|
||||||
|
GELOGD("start to malloc host memory, size=%zu", size);
|
||||||
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
std::shared_ptr<AlignedPtr> aligned_ptr = MakeShared<AlignedPtr>(size);
|
||||||
|
if (aligned_ptr == nullptr) {
|
||||||
|
GELOGE(INTERNAL_ERROR, "make shared_ptr for AlignedPtr failed");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
allocated_blocks_[aligned_ptr->Get()] = { size, aligned_ptr };
|
||||||
|
GELOGD("allocate host memory succ, size=%zu", size);
|
||||||
|
return aligned_ptr->MutableGet();
|
||||||
|
}
|
||||||
|
|
||||||
|
Status HostMemAllocator::Free(const void *memory_addr) {
|
||||||
|
if (memory_addr == nullptr) {
|
||||||
|
GELOGE(GE_GRAPH_FREE_FAILED, "Invalid memory pointer");
|
||||||
|
return GE_GRAPH_FREE_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
auto it = allocated_blocks_.find(memory_addr);
|
||||||
|
if (it == allocated_blocks_.end()) {
|
||||||
|
GELOGE(PARAM_INVALID, "Invalid memory pointer");
|
||||||
|
return PARAM_INVALID;
|
||||||
|
}
|
||||||
|
it->second.second.reset();
|
||||||
|
allocated_blocks_.erase(it);
|
||||||
|
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void HostMemAllocator::Clear() {
|
||||||
|
for (auto &block : allocated_blocks_) {
|
||||||
|
block.second.second.reset();
|
||||||
|
}
|
||||||
|
allocated_blocks_.clear();
|
||||||
|
}
|
||||||
|
} // namespace ge
|
@ -0,0 +1,57 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2019-2020 Huawei Technologies Co., Ltd
|
||||||
|
*
|
||||||
|
* 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 GE_GRAPH_MANAGER_HOST_MEM_ALLOCATOR_H_
|
||||||
|
#define GE_GRAPH_MANAGER_HOST_MEM_ALLOCATOR_H_
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
#include "framework/common/ge_inner_error_codes.h"
|
||||||
|
#include "graph/aligned_ptr.h"
|
||||||
|
#include "runtime/mem.h"
|
||||||
|
|
||||||
|
namespace ge {
|
||||||
|
class HostMemAllocator {
|
||||||
|
public:
|
||||||
|
explicit HostMemAllocator(rtMemType_t) {}
|
||||||
|
~HostMemAllocator() = default;
|
||||||
|
|
||||||
|
HostMemAllocator(const HostMemAllocator &) = delete;
|
||||||
|
HostMemAllocator &operator=(const HostMemAllocator &) = delete;
|
||||||
|
|
||||||
|
Status Initialize() {
|
||||||
|
Clear();
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
void Finalize() { Clear(); }
|
||||||
|
|
||||||
|
const void *Malloc(const std::shared_ptr<AlignedPtr>& aligned_ptr, size_t size);
|
||||||
|
uint8_t *Malloc(size_t size);
|
||||||
|
Status Free(const void *memory_addr);
|
||||||
|
|
||||||
|
std::pair<size_t, std::shared_ptr<AlignedPtr>> GetAlignedPtr(const void *addr) { return allocated_blocks_[addr]; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
void Clear();
|
||||||
|
|
||||||
|
std::map<const void *, std::pair<size_t, std::shared_ptr<AlignedPtr>>> allocated_blocks_;
|
||||||
|
// lock around all operations
|
||||||
|
mutable std::mutex mutex_;
|
||||||
|
};
|
||||||
|
} // namespace ge
|
||||||
|
|
||||||
|
#endif // GE_GRAPH_MANAGER_HOST_MEM_ALLOCATOR_H_
|
@ -1,133 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
|
||||||
*
|
|
||||||
* 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 "graph/passes/assign_pass.h"
|
|
||||||
|
|
||||||
#include "framework/common/debug/ge_log.h"
|
|
||||||
#include "framework/common/debug/log.h"
|
|
||||||
#include "graph/utils/graph_utils.h"
|
|
||||||
#include "graph/debug/ge_attr_define.h"
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
const uint32_t kValidInputNodeOutputNum = 1;
|
|
||||||
const int32_t kAssignRefInputIndex = 0;
|
|
||||||
const int32_t kAssignValueInputIndex = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace ge {
|
|
||||||
Status AssignPass::Run(NodePtr &node) {
|
|
||||||
GELOGD("AssignPass running");
|
|
||||||
if (node->GetType() != ASSIGN) {
|
|
||||||
GELOGD("No need run AssignPass on [%s, %s].", node->GetName().c_str(), node->GetType().c_str());
|
|
||||||
return SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto &ref_in_anchor = node->GetInDataAnchor(kAssignRefInputIndex);
|
|
||||||
const auto &value_in_anchor = node->GetInDataAnchor(kAssignValueInputIndex);
|
|
||||||
if ((ref_in_anchor == nullptr) || (value_in_anchor == nullptr)) {
|
|
||||||
GELOGE(FAILED, "In data anchor is null, node:%s", node->GetName().c_str());
|
|
||||||
return FAILED;
|
|
||||||
}
|
|
||||||
const auto &ref_peer_anchor = ref_in_anchor->GetPeerOutAnchor();
|
|
||||||
const auto &value_peer_anchor = value_in_anchor->GetPeerOutAnchor();
|
|
||||||
if ((ref_peer_anchor == nullptr) || (value_peer_anchor == nullptr)) {
|
|
||||||
GELOGE(FAILED, "Peer data anchor is null, node:%s", node->GetName().c_str());
|
|
||||||
return FAILED;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (IsCondMatch(node, ref_peer_anchor, value_peer_anchor)) {
|
|
||||||
///
|
|
||||||
/// variable not-const not-const
|
|
||||||
/// \ / |
|
|
||||||
/// \ / |
|
|
||||||
/// Assign ----> variable
|
|
||||||
/// | |
|
|
||||||
/// | |
|
|
||||||
/// node node
|
|
||||||
///
|
|
||||||
GELOGI("Optimization for assign_node %s start", node->GetName().c_str());
|
|
||||||
if (IsolateAndDeleteNode(node, {kAssignRefInputIndex}) != SUCCESS) {
|
|
||||||
GELOGE(FAILED, "Isolate and delete assign_node %s failed.", node->GetName().c_str());
|
|
||||||
return FAILED;
|
|
||||||
}
|
|
||||||
AddNodeDeleted(node);
|
|
||||||
|
|
||||||
const auto &ref_input = ref_peer_anchor->GetOwnerNode()->GetOpDesc();
|
|
||||||
const auto &value_input = value_peer_anchor->GetOwnerNode()->GetOpDesc();
|
|
||||||
if ((ref_input == nullptr) || (value_input == nullptr)) {
|
|
||||||
GELOGE(FAILED, "value input is null");
|
|
||||||
return FAILED;
|
|
||||||
}
|
|
||||||
if (!AttrUtils::SetStr(value_input->MutableOutputDesc(value_peer_anchor->GetIdx()), ASSIGN_VAR_NAME,
|
|
||||||
ref_input->GetName())) {
|
|
||||||
GELOGE(FAILED, "Set attr ASSIGN_VAR_NAME failed.");
|
|
||||||
return FAILED;
|
|
||||||
}
|
|
||||||
|
|
||||||
// variable has and only has one input
|
|
||||||
if (ref_input->UpdateInputDesc(0, value_input->GetOutputDesc(value_peer_anchor->GetIdx())) != GRAPH_SUCCESS) {
|
|
||||||
GELOGE(FAILED, "Update input_desc for variable %s failed.", ref_input->GetName().c_str());
|
|
||||||
return FAILED;
|
|
||||||
}
|
|
||||||
if (GraphUtils::AddEdge(value_peer_anchor, ref_peer_anchor->GetOwnerNode()->GetInDataAnchor(0)) != GRAPH_SUCCESS) {
|
|
||||||
GELOGE(FAILED, "Add data edge %s->%s failed", value_input->GetName().c_str(), ref_input->GetName().c_str());
|
|
||||||
return FAILED;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
GELOGD("AssignPass success");
|
|
||||||
return SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
|
||||||
/// @brief Check if need optimize for assign_node
|
|
||||||
/// @param [in] assign_node
|
|
||||||
/// @param [in] peer_data_anchor for ref_input of assign_node
|
|
||||||
/// @param [in] peer_data_anchor for value_input of assign_node
|
|
||||||
/// @return Status
|
|
||||||
///
|
|
||||||
bool AssignPass::IsCondMatch(const NodePtr &node, const OutDataAnchorPtr &ref_peer_anchor,
|
|
||||||
const OutDataAnchorPtr &value_peer_anchor) {
|
|
||||||
GELOGD("Check if assign_node %s match optimization condition, ref_input: %s, value_input: %s",
|
|
||||||
node->GetName().c_str(), ref_peer_anchor->GetOwnerNode()->GetName().c_str(),
|
|
||||||
value_peer_anchor->GetOwnerNode()->GetName().c_str());
|
|
||||||
|
|
||||||
const std::string &value_type = value_peer_anchor->GetOwnerNode()->GetType();
|
|
||||||
if ((value_type == CONSTANTOP) || (value_type == CONSTANT)) {
|
|
||||||
GELOGD("value input is const");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::string &ref_type = ref_peer_anchor->GetOwnerNode()->GetType();
|
|
||||||
if ((ref_type != VARIABLE) && (ref_type != VARIABLEV2)) {
|
|
||||||
GELOGD("ref input is not var");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!ref_peer_anchor->GetOwnerNode()->GetInDataNodes().empty()) {
|
|
||||||
GELOGD("ref input has data input");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((ref_peer_anchor->GetPeerInDataNodesSize() != kValidInputNodeOutputNum) ||
|
|
||||||
(value_peer_anchor->GetPeerInDataNodesSize() != kValidInputNodeOutputNum)) {
|
|
||||||
GELOGD("ref / value input has other output(s)");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
GELOGD("Optimization condition matches, assign_node: %s", node->GetName().c_str());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
} // namespace ge
|
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,83 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||||
|
*
|
||||||
|
* 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 "graph/passes/inplace_support_check_pass.h"
|
||||||
|
#include "framework/common/debug/log.h"
|
||||||
|
#include "graph/utils/graph_utils.h"
|
||||||
|
#include "graph/debug/ge_attr_define.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
constexpr uint32_t kInplaceSupportOutputIndex = 0;
|
||||||
|
constexpr uint32_t kInplaceSupportOutputNum = 1;
|
||||||
|
static const std::set<std::string> kSrcNodeTypes = { ge::DATA, ge::ANN_DATA, ge::AIPPDATA,
|
||||||
|
ge::CONSTANT, ge::CONSTANTOP,
|
||||||
|
ge::VARIABLE, ge::VARIABLEV2 };
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace ge {
|
||||||
|
Status InplaceSupportCheckPass::Run(NodePtr &node) {
|
||||||
|
GELOGD("InplaceSupportCheckPass running");
|
||||||
|
if (node->GetAllOutDataAnchorsSize() != kInplaceSupportOutputNum) {
|
||||||
|
GELOGD("output num of node %s is not %u, skip InplaceSupportCheckPass",
|
||||||
|
node->GetName().c_str(), kInplaceSupportOutputNum);
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
GE_CHECK_NOTNULL(node->GetOpDesc());
|
||||||
|
const DataType &output_type = node->GetOpDesc()->GetOutputDesc(kInplaceSupportOutputIndex).GetDataType();
|
||||||
|
const GeShape &output_shape = node->GetOpDesc()->GetOutputDesc(kInplaceSupportOutputIndex).GetShape();
|
||||||
|
GELOGD("process InplaceSupportCheckPass on node %s", node->GetName().c_str());
|
||||||
|
for (const auto &in_data_anchor : node->GetAllInDataAnchors()) {
|
||||||
|
const auto &peer_data_anchor = in_data_anchor->GetPeerOutAnchor();
|
||||||
|
if (peer_data_anchor == nullptr) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
auto in_node = peer_data_anchor->GetOwnerNode();
|
||||||
|
if (kSrcNodeTypes.count(in_node->GetType()) > 0) {
|
||||||
|
GELOGD("meet src_node %s", in_node->GetName().c_str());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (peer_data_anchor->GetPeerInDataNodesSize() != kInplaceSupportOutputNum) {
|
||||||
|
GELOGD("peer_data_anchor links with multi in_data_anchors");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t inplace_input_idx = in_data_anchor->GetIdx();
|
||||||
|
const DataType &input_type = node->GetOpDesc()->GetInputDesc(inplace_input_idx).GetDataType();
|
||||||
|
const GeShape &input_shape = node->GetOpDesc()->GetInputDesc(inplace_input_idx).GetShape();
|
||||||
|
if (input_type != output_type) {
|
||||||
|
GELOGW("DataType mismatch, in_idx=%d, input_type=%u, output_type=%u", inplace_input_idx, input_type, output_type);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (input_shape.GetDims() != output_shape.GetDims()) {
|
||||||
|
GELOGW("Shape mismatch, in_idx=%d, input_shape=[%s], output_shape=[%s]",
|
||||||
|
inplace_input_idx, input_shape.ToString().c_str(), output_shape.ToString().c_str());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
GELOGD("add attr INPLACE_SUPPORT_INPUT_INDEX on node %s, input_idx=%d", node->GetName().c_str(), inplace_input_idx);
|
||||||
|
if (!AttrUtils::SetInt(node->GetOpDesc()->MutableOutputDesc(kInplaceSupportOutputIndex),
|
||||||
|
INPLACE_SUPPORT_INPUT_INDEX, inplace_input_idx)) {
|
||||||
|
GELOGE(FAILED, "Set attr INPLACE_SUPPORT_INPUT_INDEX on node %s failed.", node->GetName().c_str());
|
||||||
|
return FAILED;
|
||||||
|
}
|
||||||
|
AddRePassNode(node);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
GELOGD("InplaceSupportCheckPass success");
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
} // namespace ge
|
@ -0,0 +1,28 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||||
|
*
|
||||||
|
* 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 GE_GRAPH_PASSES_INPLACE_SUPPORT_CHECK_PASS_H_
|
||||||
|
#define GE_GRAPH_PASSES_INPLACE_SUPPORT_CHECK_PASS_H_
|
||||||
|
|
||||||
|
#include "graph/passes/base_pass.h"
|
||||||
|
|
||||||
|
namespace ge {
|
||||||
|
class InplaceSupportCheckPass : public BaseNodePass {
|
||||||
|
public:
|
||||||
|
Status Run(NodePtr &node) override;
|
||||||
|
};
|
||||||
|
} // namespace ge
|
||||||
|
#endif // GE_GRAPH_PASSES_INPLACE_SUPPORT_CHECK_PASS_H_
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue