parent
40601b1f81
commit
87224a512a
@ -0,0 +1,70 @@
|
||||
/**
|
||||
* 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, addr=%p, size=%zu", aligned_ptr->Get(), 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, addr=%p, size=%zu", aligned_ptr->Get(), size);
|
||||
return aligned_ptr->MutableGet();
|
||||
}
|
||||
|
||||
Status HostMemAllocator::Free(const void *memory_addr) {
|
||||
GELOGD("Free host memory, addr=%p", 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,58 @@
|
||||
/**
|
||||
* 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 <unordered_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 memory_type) : memory_type_(memory_type) {}
|
||||
~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();
|
||||
|
||||
rtMemType_t memory_type_;
|
||||
std::unordered_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_
|
@ -0,0 +1,86 @@
|
||||
/**
|
||||
* 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 {
|
||||
const uint32_t kInplaceSupportOutputIndex = 0;
|
||||
const uint32_t kInplaceSupportOutputNum = 1;
|
||||
static const std::set<std::string> src_node_types = { 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 (src_node_types.count(node->GetType()) > 0) {
|
||||
GELOGD("meet src_node %s, skip InplaceSupportCheckPass", node->GetName().c_str());
|
||||
return SUCCESS;
|
||||
}
|
||||
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 (src_node_types.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) {
|
||||
GELOGD("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()) {
|
||||
GELOGD("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);
|
||||
}
|
||||
|
||||
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_
|
Loading…
Reference in new issue