!7468 [MemOpt]Safe Optimized Memory Allocation Solver
Merge pull request !7468 from laiyongqiang/somas_online_commitpull/7468/MERGE
commit
7b967a33a7
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,132 @@
|
||||
/**
|
||||
* 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 MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_H_
|
||||
#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_H_
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "backend/kernel_compiler/tbe/tbe_utils.h"
|
||||
#include "backend/optimizer/somas/somas_node.h"
|
||||
#include "backend/optimizer/somas/somas_solver_pre.h"
|
||||
#include "backend/optimizer/somas/somas_stream.h"
|
||||
#include "backend/session/anf_runtime_algorithm.h"
|
||||
#include "backend/session/kernel_graph.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace somas {
|
||||
class Somas {
|
||||
public:
|
||||
// Constructors/Destructors
|
||||
Somas() = default;
|
||||
Somas(const Somas &) = delete;
|
||||
Somas &operator=(const Somas &) = delete;
|
||||
~Somas() = default;
|
||||
|
||||
bool Allocate(const session::KernelGraph *graph);
|
||||
size_t GetTotalMemSize() { return mem_offset_; }
|
||||
void set_mem_base_addr(uint8_t *mem_base_addr) { mem_base_addr_ = mem_base_addr; }
|
||||
uint8_t *GetNodeOutputPtr(const AnfNodePtr &node, size_t index) const;
|
||||
uint8_t *GetNodeWorkSpacePtr(const AnfNodePtr &node, size_t index) const;
|
||||
|
||||
void DumpSomasBasicIR(const string filename);
|
||||
void DumpSomasMemoryIR(const string filename);
|
||||
|
||||
private:
|
||||
// Maps
|
||||
std::unordered_map<size_t, SomasTensorPtr> tensors_map_;
|
||||
std::map<void *, SomasNodePtr> nodes_map_;
|
||||
|
||||
// Vectors
|
||||
std::vector<SomasNodePtr> nodes_list_;
|
||||
std::vector<SomasStreamPtr> streams_list_;
|
||||
std::vector<SomasTensorPtr> tensors_list_;
|
||||
|
||||
// Stream groups
|
||||
std::vector<vector<uint32_t>> streams_groups_;
|
||||
|
||||
// Solver
|
||||
std::unordered_map<size_t, SomasSolverTensorDescPtr> solver_tensor_desc_list_;
|
||||
SomasSolverPrePtr somas_solver_;
|
||||
|
||||
// Constraints
|
||||
std::shared_ptr<Array> cannot_reuse_;
|
||||
|
||||
// Contiguous list
|
||||
std::vector<vector<size_t>> contiguous_tensors_list_;
|
||||
|
||||
// Ref lists
|
||||
std::vector<vector<size_t>> ref_node_constraints_;
|
||||
std::vector<vector<size_t>> ref_overlap_constraints_;
|
||||
|
||||
// total Offset
|
||||
size_t mem_offset_;
|
||||
|
||||
// getnext op output size
|
||||
size_t get_next_size_;
|
||||
|
||||
// Memory base addr
|
||||
uint8_t *mem_base_addr_{nullptr};
|
||||
|
||||
// Save debug info
|
||||
bool save_graphs_{false};
|
||||
std::string save_graphs_path_;
|
||||
|
||||
// statistic info
|
||||
size_t upper_bound_{0};
|
||||
size_t lower_bound_{0};
|
||||
size_t workspace_total_size_{0};
|
||||
size_t comm_input_total_size_{0};
|
||||
size_t comm_output_total_size_{0};
|
||||
size_t lifelong_all_total_size_{0};
|
||||
size_t lifelong_start_total_size_{0};
|
||||
size_t lifelong_end_total_size_{0};
|
||||
|
||||
bool InitSomasTensors(const session::KernelGraph *graph);
|
||||
void InitBasicInfo(const session::KernelGraph *graph);
|
||||
void InitSomasStreamAndNode(const session::KernelGraph *graph);
|
||||
void InitSomasOutputAndWorkspaceTensors(const session::KernelGraph *graph);
|
||||
void InitSomasInputTensors(const session::KernelGraph *graph);
|
||||
void GetNextOutputProcess(const session::KernelGraph *graph);
|
||||
void IndependentNodeOutputProcess(const session::KernelGraph *graph);
|
||||
void SummaryInputProcess(const session::KernelGraph *graph);
|
||||
void RefNodeProcess(const session::KernelGraph *graph);
|
||||
void UnReuseNodeProcess(const session::KernelGraph *graph);
|
||||
SomasTensorPtr CreateGapTensor(size_t gap_tensor_id);
|
||||
void GenContiguousList(const session::KernelGraph *graph);
|
||||
|
||||
void PreprocessingConflicts();
|
||||
void ComputeConflictPairs();
|
||||
|
||||
bool Assign(const session::KernelGraph *graph);
|
||||
|
||||
void DumpOfflineIR(const string filename);
|
||||
void DumpSomasMemoryPoolInfoIR(const string filename);
|
||||
std::string GetSplitName(const string &scope_name) const;
|
||||
size_t CalcLowerBound() const;
|
||||
void GenStatisticInfo();
|
||||
};
|
||||
|
||||
using SomasPtr = std::shared_ptr<Somas>;
|
||||
} // namespace somas
|
||||
} // namespace mindspore
|
||||
#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_H_
|
@ -0,0 +1,46 @@
|
||||
/**
|
||||
* 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 "backend/optimizer/somas/somas_node.h"
|
||||
#include <algorithm>
|
||||
|
||||
namespace mindspore {
|
||||
namespace somas {
|
||||
void SomasNode::ComputeAncestorNodes() {
|
||||
// Fast algorithm: assuming nodes execute this function in the received topological order
|
||||
int64_t thisId = this->GetStream()->GetId();
|
||||
|
||||
for (SomasNodePtr node : ancestor_nodes_) {
|
||||
int64_t ancestorId = node->GetStream()->GetId();
|
||||
// Map Improvement for max_ancestor_order
|
||||
if (thisId != ancestorId) {
|
||||
this->anc_stream_max_order_[ancestorId] = std::max(this->anc_stream_max_order_[ancestorId], node->GetId());
|
||||
}
|
||||
for (SomasStreamPtr stream : node->GetStream()->ancestor_streams_) {
|
||||
int64_t streamId = stream->GetId();
|
||||
this->anc_stream_max_order_[streamId] =
|
||||
std::max(this->anc_stream_max_order_[streamId], node->anc_stream_max_order_[streamId]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SomasNode::PresetAncestorStreams(const std::vector<SomasStreamPtr> stream_vector) {
|
||||
for (SomasStreamPtr stream : stream_vector) {
|
||||
anc_stream_max_order_[stream->GetId()] = 0;
|
||||
}
|
||||
}
|
||||
} // namespace somas
|
||||
} // namespace mindspore
|
@ -0,0 +1,78 @@
|
||||
/**
|
||||
* 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 MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_NODE_H_
|
||||
#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_NODE_H_
|
||||
|
||||
#include "backend/optimizer/somas/somas_stream.h"
|
||||
#include "backend/optimizer/somas/somas_tensor.h"
|
||||
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace mindspore {
|
||||
namespace somas {
|
||||
class SomasStream;
|
||||
class SomasTensor;
|
||||
|
||||
enum NodeType { kCommonNode, kCommunicationNode };
|
||||
|
||||
using SomasStreamPtr = std::shared_ptr<SomasStream>;
|
||||
using SomasTensorPtr = std::shared_ptr<SomasTensor>;
|
||||
|
||||
class SomasNode {
|
||||
public:
|
||||
using SomasNodePtr = std::shared_ptr<SomasNode>;
|
||||
// Public attributes (mutated in code)
|
||||
std::string scope_full_name_;
|
||||
|
||||
std::set<SomasNodePtr>
|
||||
ancestor_nodes_; // keeping only distance *one* ancestor nodes; enough to ComputeAncestorNodes()
|
||||
std::set<SomasTensorPtr> tensors_;
|
||||
|
||||
std::vector<SomasTensorPtr> input_tensors_;
|
||||
std::vector<SomasTensorPtr> output_tensors_;
|
||||
std::vector<SomasTensorPtr> workspace_tensors_;
|
||||
|
||||
std::unordered_map<int64_t, size_t> anc_stream_max_order_;
|
||||
|
||||
// Constructors/Destructors
|
||||
SomasNode(size_t id, NodeType type, SomasStreamPtr stream) : id_(id), stream_(stream), type_(type) {}
|
||||
SomasNode(const SomasNode &) = delete;
|
||||
SomasNode &operator=(const SomasNode &) = delete;
|
||||
~SomasNode() = default;
|
||||
|
||||
// Accessors
|
||||
const size_t &GetId() { return id_; }
|
||||
SomasStreamPtr GetStream() { return stream_; }
|
||||
const NodeType &GetType() { return type_; }
|
||||
|
||||
// Computing ancestors
|
||||
void PresetAncestorStreams(const std::vector<SomasStreamPtr> stream_vector);
|
||||
void ComputeAncestorNodes();
|
||||
|
||||
private:
|
||||
const size_t id_{0};
|
||||
SomasStreamPtr const stream_;
|
||||
const NodeType type_;
|
||||
};
|
||||
} // namespace somas
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_NODE_H_
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,177 @@
|
||||
/**
|
||||
* 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 MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_SOLVER_ALG_H_
|
||||
#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_SOLVER_ALG_H_
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <numeric>
|
||||
#include <set>
|
||||
#include <stack>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "backend/optimizer/somas/somas_solver_pre.h"
|
||||
#include "utils/ms_context.h"
|
||||
|
||||
using std::pair;
|
||||
using std::set;
|
||||
using std::stack;
|
||||
using std::unordered_map;
|
||||
using std::vector;
|
||||
|
||||
namespace mindspore {
|
||||
namespace somas {
|
||||
class Interval {
|
||||
public:
|
||||
Interval() { m_a_ = m_b_ = 0; }
|
||||
explicit Interval(SomasSolverTensorDescPtr t) {
|
||||
m_a_ = t->offset_;
|
||||
m_b_ = m_a_ + t->size_;
|
||||
}
|
||||
Interval(const size_t &a, const size_t &b) {
|
||||
m_a_ = a;
|
||||
m_b_ = b;
|
||||
}
|
||||
bool intersect(const Interval &i) { return (in(i.m_a_) || in(i.m_b_)); }
|
||||
bool in(const size_t &a) { return ((a > m_a_) && (a < m_b_)); }
|
||||
Interval intersection(const Interval &i) {
|
||||
if (m_a_ < i.m_a_)
|
||||
return Interval(m_a_, i.m_b_);
|
||||
else
|
||||
return Interval(i.m_a_, m_b_);
|
||||
}
|
||||
void merge(const Interval &i) {
|
||||
m_a_ = std::min(m_a_, i.m_a_);
|
||||
m_b_ = std::max(m_b_, i.m_b_);
|
||||
}
|
||||
size_t &lb() { return m_a_; }
|
||||
size_t &ub() { return m_b_; }
|
||||
bool contains(size_t width) { return (m_b_ - m_a_) >= width; }
|
||||
bool contains(const Interval &a) { return ((a.m_a_ >= m_a_) && (a.m_b_ <= m_b_)); }
|
||||
Interval &operator=(const Interval &in) {
|
||||
m_a_ = in.m_a_;
|
||||
m_b_ = in.m_b_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
size_t m_a_;
|
||||
size_t m_b_;
|
||||
};
|
||||
|
||||
class BlockTensor {
|
||||
public:
|
||||
SomasSolverTensorDescPtr m_start_tensor_;
|
||||
unordered_map<uint32_t,
|
||||
std::set<pair<size_t, size_t>, bool (*)(const pair<size_t, size_t> &, const pair<size_t, size_t> &)>>
|
||||
offsets_candidates_;
|
||||
uint32_t m_current_sol_;
|
||||
bool m_bre_allocate_;
|
||||
unordered_map<uint32_t, size_t> offsets_;
|
||||
size_t m_size_;
|
||||
BlockTensor()
|
||||
: m_start_tensor_(NULL),
|
||||
offsets_candidates_(),
|
||||
m_current_sol_(0),
|
||||
m_bre_allocate_(true),
|
||||
offsets_(),
|
||||
m_size_(0) {}
|
||||
|
||||
BlockTensor &operator=(const BlockTensor &bt) {
|
||||
m_bre_allocate_ = bt.m_bre_allocate_;
|
||||
m_current_sol_ = 0;
|
||||
m_start_tensor_ = bt.m_start_tensor_;
|
||||
offsets_candidates_ = bt.offsets_candidates_;
|
||||
offsets_ = bt.offsets_;
|
||||
m_size_ = bt.m_size_;
|
||||
return *this;
|
||||
}
|
||||
void log() {
|
||||
SomasSolverTensorDescPtr p = m_start_tensor_;
|
||||
MS_LOG(DEBUG) << "Block of Tensors [" << m_start_tensor_->index_ << "]\nsize: " << m_size_ << "Tensors:";
|
||||
while (p) {
|
||||
MS_LOG(DEBUG) << "[" << p->index_ << "," << p->size_ << "]";
|
||||
p = p->right_;
|
||||
}
|
||||
}
|
||||
bool Alone() const { return ((NULL == m_start_tensor_->right_) && (NULL == m_start_tensor_->left_)); }
|
||||
};
|
||||
|
||||
class FootPrint : public std::enable_shared_from_this<FootPrint> {
|
||||
public:
|
||||
uint32_t m_solId_;
|
||||
|
||||
FootPrint()
|
||||
: m_offset_(0),
|
||||
m_starts_(),
|
||||
m_foot_print_next_(NULL),
|
||||
m_alignment_(0),
|
||||
m_branching_strategy_(0),
|
||||
m_algorithm_(0) {}
|
||||
void setAlignment(const size_t a) { m_alignment_ = a; }
|
||||
void setBranchingStrategy(uint32_t bs) { m_branching_strategy_ = bs; }
|
||||
void setCurrentSol(uint32_t solId) { m_solId_ = solId; }
|
||||
void setAlgorithm(uint32_t algorithm) { m_algorithm_ = algorithm; }
|
||||
void addStart(BlockTensor *elemIndex) { m_starts_.push_back(elemIndex); }
|
||||
void addElem(BlockTensor *block, const size_t &offset);
|
||||
std::shared_ptr<FootPrint> &Next() { return m_foot_print_next_; }
|
||||
vector<BlockTensor *> &getStarts() { return m_starts_; }
|
||||
void Destroy();
|
||||
const size_t getOffset() { return m_offset_; }
|
||||
void setOffset(const size_t &offset) { m_offset_ = offset; }
|
||||
bool findOffset(const std::shared_ptr<Array> &constraints, const BlockTensor &block, size_t *offset);
|
||||
void ConstrainedBLocks(const std::shared_ptr<Array> &constraints, const BlockTensor &b1, const BlockTensor &b2,
|
||||
vector<Interval> *oInterval_l);
|
||||
void Merge(vector<Interval> *l_interval, stack<Interval> *l_merged);
|
||||
bool findFirst(stack<Interval> *merged, const BlockTensor &block, size_t *offset);
|
||||
size_t Result();
|
||||
void printStats();
|
||||
|
||||
private:
|
||||
size_t m_offset_;
|
||||
vector<BlockTensor *> m_starts_;
|
||||
std::shared_ptr<FootPrint> m_foot_print_next_;
|
||||
size_t m_alignment_;
|
||||
uint32_t m_branching_strategy_;
|
||||
uint32_t m_algorithm_;
|
||||
};
|
||||
|
||||
class FastHeuristic {
|
||||
public:
|
||||
FastHeuristic() : m_alignment_(512), m_tensors_allocated_(0) {}
|
||||
|
||||
void setAlignment(const size_t &a) { m_alignment_ = a; }
|
||||
void Destroy();
|
||||
bool Eval( // unordered_map<size_t, SomasSolverTensorDescPtr> &tensors_m,
|
||||
vector<BlockTensor> *block_tensors_v, std::shared_ptr<FootPrint> foot_print,
|
||||
const std::shared_ptr<Array> &pConstraints);
|
||||
|
||||
private:
|
||||
size_t m_alignment_;
|
||||
size_t m_tensors_allocated_;
|
||||
};
|
||||
} // namespace somas
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_SOLVER_ALG_H_
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,101 @@
|
||||
/**
|
||||
* 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 MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_SOLVER_CORE_H_
|
||||
#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_SOLVER_CORE_H_
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "backend/optimizer/somas/somas_solver_alg.h"
|
||||
#include "backend/optimizer/somas/somas_solver_pre.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace somas {
|
||||
class SomasSolverCore {
|
||||
public:
|
||||
/// Interface Function: receive parameters, creates the model to solve and then save the result
|
||||
SomasSolverCore(const std::unordered_map<size_t, SomasSolverTensorDescPtr> &tensors,
|
||||
const std::shared_ptr<Array> &constraints)
|
||||
: tensors_(tensors),
|
||||
constraints_(constraints),
|
||||
upperbound_(SIZE_MAX),
|
||||
timing_(0),
|
||||
lifelongmemory_(0),
|
||||
verify_(false),
|
||||
all_(true),
|
||||
best_sol_(0),
|
||||
sort_strategy_(kGreaterSizeSmallerIndex),
|
||||
branching_strategy_(kBest),
|
||||
sol_count_(0),
|
||||
algorithm_(kManyObjects) {}
|
||||
~SomasSolverCore() = default;
|
||||
|
||||
Status MemoryAllocationSolver();
|
||||
Status Verify();
|
||||
bool Verify(const size_t &);
|
||||
Status Verify(unordered_map<size_t, SomasSolverTensorDescPtr> *);
|
||||
void VerifySolution(const bool verify) { verify_ = verify; }
|
||||
void SortTensors();
|
||||
void BuildBlocks();
|
||||
void Clean();
|
||||
void SetBestSolution() { RestoreSolution(best_sol_); }
|
||||
void RestoreSolution(uint32_t sol_id);
|
||||
void SetSortingStrategy(SortingType sort_strategy) { sort_strategy_ = sort_strategy; }
|
||||
void SetFittingStrategy(FittingType branching_strategy) { branching_strategy_ = branching_strategy; }
|
||||
void SetAlgorithmStrategy(AlgorithmType algorithm_strategy) { algorithm_ = algorithm_strategy; }
|
||||
void SetAllStrategies(bool all) { all_ = all; }
|
||||
const size_t &GetUpperbound() const { return upperbound_; }
|
||||
|
||||
private:
|
||||
std::unordered_map<size_t, SomasSolverTensorDescPtr> tensors_;
|
||||
vector<BlockTensor> block_tensors_;
|
||||
std::shared_ptr<Array> constraints_;
|
||||
size_t upperbound_{0};
|
||||
size_t timing_{0};
|
||||
size_t lifelongmemory_{0};
|
||||
bool verify_{false};
|
||||
bool all_{false};
|
||||
uint32_t best_sol_{0};
|
||||
SortingType best_sort_;
|
||||
FittingType best_branching_;
|
||||
SortingType sort_strategy_;
|
||||
FittingType branching_strategy_;
|
||||
uint32_t sol_count_{0};
|
||||
AlgorithmType algorithm_;
|
||||
|
||||
size_t FindSolutions();
|
||||
size_t Search(const std::shared_ptr<FootPrint> &pFootprint);
|
||||
void AppendLifelongTensors();
|
||||
void Destroy(std::shared_ptr<FootPrint> &);
|
||||
|
||||
const std::string sorting_[6] = {"size(>), index(<)",
|
||||
"size(>), index(>)",
|
||||
"size(>), constraints(<), index(<)",
|
||||
"size(>), constraints(<), index(>)",
|
||||
"size(>), constraints(>), index(<)",
|
||||
"size(>), constraints(>), index(>)"};
|
||||
const std::string branching_[4] = {"bestfit", "smallest", "largest", "worstfit"};
|
||||
const std::string algorithm_type_[2] = {"Shared Objects", "Single Object"};
|
||||
};
|
||||
} // namespace somas
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_SOLVER_CORE_H_
|
@ -0,0 +1,209 @@
|
||||
/**
|
||||
* 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 <cstdio>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "backend/optimizer/somas/somas_solver_core.h"
|
||||
#include "backend/optimizer/somas/somas_solver_pre.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace somas {
|
||||
Status SomasSolverPre::Solving(const session::KernelGraph *graph,
|
||||
std::unordered_map<size_t, SomasSolverTensorDescPtr> *ptensors,
|
||||
std::shared_ptr<Array> pConstraints, const vector<vector<size_t>> &continuous_v,
|
||||
bool bVerifySolution, bool ball, SortingType sorting, FittingType fitting,
|
||||
AlgorithmType algorithm) {
|
||||
Status retval = SUCCESS;
|
||||
|
||||
try {
|
||||
size_t maxIndex = 0;
|
||||
std::unordered_map<size_t, SomasSolverTensorDescPtr> &tensors = *ptensors;
|
||||
std::unordered_map<size_t, SomasSolverTensorDescPtr>::iterator max =
|
||||
std::max_element(tensors.begin(), tensors.end(),
|
||||
[](const std::pair<size_t, SomasSolverTensorDescPtr> &a,
|
||||
const std::pair<size_t, SomasSolverTensorDescPtr> &b) { return a.first < b.first; });
|
||||
maxIndex = max->first;
|
||||
if (maxIndex > pConstraints->Rows() - 1) {
|
||||
MS_LOG(WARNING) << "ERROR: MaxIndex invalid, MaxIndex " << maxIndex << ", Rows " << pConstraints->Rows();
|
||||
return FAILED;
|
||||
}
|
||||
MS_LOG(INFO) << "Filling in constraints matrix..";
|
||||
uint32_t continuous_cnt = 0;
|
||||
|
||||
// creating S Lists
|
||||
for (auto &aux : continuous_v) {
|
||||
for (uint32_t i = 0; i < aux.size() - 1; i++) {
|
||||
uint32_t index1 = aux[i];
|
||||
uint32_t index2 = aux[i + 1];
|
||||
if (NULL == tensors[index1]) {
|
||||
MS_LOG(WARNING) << "NULL tensor received in continuous constraint (tensor index " << index1 << ")";
|
||||
return FAILED;
|
||||
}
|
||||
if (NULL == tensors[index2]) {
|
||||
MS_LOG(WARNING) << "NULL tensor received in continuous constraint (tensor index " << index2 << ")";
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
const size_t continuous = 2;
|
||||
(*pConstraints)(index2, index1) = continuous;
|
||||
if (tensors[index1]->right_)
|
||||
MS_LOG(WARNING) << "Warning:tensor " << index1
|
||||
<< " already has a right tensor (id: " << tensors[index1]->right_->index_;
|
||||
if (tensors[index2]->left_)
|
||||
MS_LOG(WARNING) << "Warning:tensor " << index2
|
||||
<< " already has a left tensor (id: " << tensors[index2]->left_->index_;
|
||||
|
||||
tensors[index1]->right_ = tensors[index2];
|
||||
tensors[index2]->left_ = tensors[index1];
|
||||
continuous_cnt++;
|
||||
}
|
||||
}
|
||||
continuous_cnt++;
|
||||
|
||||
std::shared_ptr<SomasSolverCore> pSolver = std::make_shared<SomasSolverCore>(tensors, pConstraints);
|
||||
pSolver->SetAlgorithmStrategy(algorithm);
|
||||
pSolver->SetSortingStrategy(sorting);
|
||||
pSolver->SetFittingStrategy(fitting);
|
||||
pSolver->SetAllStrategies(ball);
|
||||
pSolver->VerifySolution(bVerifySolution);
|
||||
|
||||
if (SUCCESS == (pSolver->MemoryAllocationSolver())) {
|
||||
max_offset_ = pSolver->GetUpperbound();
|
||||
const double giga = 1024. * 1024. * 1024.;
|
||||
MS_LOG(INFO) << "SomasSolver::Solving SUCCESS";
|
||||
MS_LOG(INFO) << "SomasSolver::Solving RESULT: " << max_offset_ << " (" << max_offset_ / (giga) << " GB)";
|
||||
}
|
||||
auto context_ptr = MsContext::GetInstance();
|
||||
MS_EXCEPTION_IF_NULL(context_ptr);
|
||||
bool save_graphs = context_ptr->get_param<bool>(MS_CTX_SAVE_GRAPHS_FLAG);
|
||||
if (save_graphs) {
|
||||
Log(graph, tensors, pConstraints, continuous_v);
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
MS_LOG(EXCEPTION) << "SomasSolver::Solving FAILED: " << e.what();
|
||||
retval = FAILED;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
void SomasSolverPre::Log(const session::KernelGraph *graph,
|
||||
const unordered_map<size_t, SomasSolverTensorDescPtr> &tensors,
|
||||
const std::shared_ptr<Array> &pConstraints, const vector<vector<size_t>> &continuous_v) {
|
||||
MS_LOG(INFO) << "SomasSolver::Log Writing somas-input.txt..";
|
||||
|
||||
auto context_ptr = MsContext::GetInstance();
|
||||
MS_EXCEPTION_IF_NULL(context_ptr);
|
||||
auto save_graphs_path = context_ptr->get_param<std::string>(MS_CTX_SAVE_GRAPHS_PATH);
|
||||
std::string filename = save_graphs_path + "/" + "somas_solver_input_" + std::to_string(graph->graph_id()) + ".ir";
|
||||
if (filename.size() > PATH_MAX) {
|
||||
MS_LOG(ERROR) << "File path " << filename << " is too long.";
|
||||
return;
|
||||
}
|
||||
char real_path[PATH_MAX] = {0};
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
if (_fullpath(real_path, filename.c_str(), PATH_MAX) == nullptr) {
|
||||
MS_LOG(DEBUG) << "dir " << filename << " does not exit.";
|
||||
}
|
||||
#else
|
||||
if (realpath(filename.c_str(), real_path) == nullptr) {
|
||||
MS_LOG(DEBUG) << "Dir " << filename << " does not exit.";
|
||||
}
|
||||
#endif
|
||||
|
||||
std::string path_string = real_path;
|
||||
ChangeFileMode(path_string, S_IRWXU);
|
||||
std::ofstream ofs_1(real_path);
|
||||
|
||||
if (!ofs_1.is_open()) {
|
||||
MS_LOG(ERROR) << "Open log file '" << real_path << "' failed!";
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto &t : tensors) {
|
||||
ofs_1 << "T " << t.second->index_ << " " << t.second->size_ << " " << t.second->lifelong_ << std::endl;
|
||||
}
|
||||
|
||||
for (auto &t1 : tensors) {
|
||||
for (auto &t2 : tensors) {
|
||||
size_t idx1 = t1.first;
|
||||
size_t idx2 = t2.first;
|
||||
if ((idx1 != idx2) && (*pConstraints)(idx1, idx2) == 1) {
|
||||
ofs_1 << "C " << idx1 << " " << idx2 << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (auto &s : continuous_v) {
|
||||
ofs_1 << "S";
|
||||
for (auto idx : s) {
|
||||
ofs_1 << " " << idx;
|
||||
}
|
||||
ofs_1 << std::endl;
|
||||
}
|
||||
ofs_1.close();
|
||||
|
||||
MS_LOG(INFO) << "SomasSolver::Log Writing somas-output.txt..";
|
||||
std::string out_filename =
|
||||
save_graphs_path + "/" + "somas_solver_output_" + std::to_string(graph->graph_id()) + ".ir";
|
||||
if (out_filename.size() > PATH_MAX) {
|
||||
MS_LOG(ERROR) << "File path " << out_filename << " is too long.";
|
||||
return;
|
||||
}
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
if (_fullpath(real_path, out_filename.c_str(), PATH_MAX) == nullptr) {
|
||||
MS_LOG(DEBUG) << "dir " << out_filename << " does not exit.";
|
||||
}
|
||||
#else
|
||||
if (realpath(out_filename.c_str(), real_path) == nullptr) {
|
||||
MS_LOG(DEBUG) << "Dir " << out_filename << " does not exit.";
|
||||
}
|
||||
#endif
|
||||
path_string = real_path;
|
||||
ChangeFileMode(path_string, S_IRWXU);
|
||||
std::ofstream ofs_2(real_path);
|
||||
|
||||
if (!ofs_2.is_open()) {
|
||||
MS_LOG(ERROR) << "Open log file '" << real_path << "' failed!";
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto &t : tensors) {
|
||||
SomasSolverTensorDescPtr tensor = t.second;
|
||||
int continuous = 0;
|
||||
if (tensor->left_ == NULL && tensor->right_ != NULL)
|
||||
continuous = 1;
|
||||
else if (tensor->left_ != NULL && tensor->right_ != NULL)
|
||||
continuous = 2;
|
||||
else if (tensor->left_ != NULL && tensor->right_ == NULL)
|
||||
continuous = 3;
|
||||
const size_t alignment = 512;
|
||||
bool size_aligned = tensor->size_ % alignment == 0;
|
||||
bool offset_aligned = tensor->offset_ % alignment == 0;
|
||||
|
||||
ofs_2 << std::endl
|
||||
<< "tensor_id=" << tensor->index_ << "\tsize=" << tensor->size_ << "\toffset=" << tensor->offset_
|
||||
<< "\tcontinuous=" << continuous << "\tsize_aligned=" << size_aligned
|
||||
<< "\toffset_aligned=" << offset_aligned;
|
||||
}
|
||||
ofs_2.close();
|
||||
|
||||
MS_LOG(INFO) << "SomasSolver::Log done";
|
||||
}
|
||||
} // namespace somas
|
||||
} // namespace mindspore
|
@ -0,0 +1,159 @@
|
||||
/**
|
||||
* 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 MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_SOLVER_PRE_H_
|
||||
#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_SOLVER_PRE_H_
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <stack>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include "backend/session/kernel_graph.h"
|
||||
|
||||
using std::unordered_map;
|
||||
using std::vector;
|
||||
|
||||
namespace mindspore {
|
||||
namespace somas {
|
||||
enum Status { FAILED, SUCCESS };
|
||||
enum AlgorithmType { kManyObjects = 0, kSingleObject, kNumAlgorithmTypes };
|
||||
enum SortingType {
|
||||
kGreaterSizeSmallerIndex = 0,
|
||||
#ifdef SOMAS_DEBUG
|
||||
kGreaterSizeGreaterIndex,
|
||||
kGreaterSizeSmallerConstraintsSmallerIndex,
|
||||
kGreaterSizeSmallerConstraintsGreaterIndex,
|
||||
kGreaterSizeGreaterConstraintsSmallerIndex,
|
||||
kGreaterSizeGreaterConstraintsGreaterIndex,
|
||||
#endif
|
||||
kNumSortingTypes
|
||||
};
|
||||
enum FittingType {
|
||||
kBest = 0,
|
||||
kSmallest,
|
||||
#ifdef SOMAS_DEBUG
|
||||
kLargest,
|
||||
kWorst,
|
||||
#endif
|
||||
kNumFittingTypes
|
||||
};
|
||||
|
||||
class Array {
|
||||
public:
|
||||
Array(const size_t &rows, const size_t &cols) : rows_(rows), cols_(cols) {
|
||||
conflicts_array_ = std::make_unique<int[]>(rows * cols);
|
||||
for (uint32_t i = 0; i < rows * cols; i++) {
|
||||
conflicts_array_[i] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
Array(const Array &array) : rows_(array.rows_), cols_(array.cols_) {
|
||||
conflicts_array_ = std::make_unique<int[]>(array.rows_ * array.cols_);
|
||||
for (uint32_t i = 0; i < array.rows_ * array.cols_; i++) {
|
||||
conflicts_array_[i] = array.conflicts_array_[i];
|
||||
}
|
||||
}
|
||||
|
||||
Array &operator=(const Array &array) { return *this; }
|
||||
|
||||
int &operator()(const size_t &i, const size_t &j) {
|
||||
assert((i * cols_ + j) < (rows_ * cols_));
|
||||
return conflicts_array_[i * cols_ + j];
|
||||
}
|
||||
|
||||
const size_t &Rows() { return rows_; }
|
||||
const size_t &Cols() { return cols_; }
|
||||
|
||||
private:
|
||||
const size_t rows_;
|
||||
const size_t cols_;
|
||||
std::unique_ptr<int[]> conflicts_array_;
|
||||
};
|
||||
|
||||
struct SomasSolverTensorDesc {
|
||||
size_t index_;
|
||||
size_t size_;
|
||||
size_t offset_;
|
||||
bool lifelong_;
|
||||
size_t constraints_;
|
||||
using SomasSolverTensorDescPtr = std::shared_ptr<SomasSolverTensorDesc>;
|
||||
SomasSolverTensorDescPtr right_;
|
||||
SomasSolverTensorDescPtr left_;
|
||||
bool blocked_;
|
||||
|
||||
SomasSolverTensorDesc() = default;
|
||||
|
||||
SomasSolverTensorDesc(size_t index, size_t size, size_t offset, bool blifelong)
|
||||
: index_(index), size_(size), offset_(offset), lifelong_(blifelong) {
|
||||
constraints_ = 0;
|
||||
right_ = NULL;
|
||||
left_ = NULL;
|
||||
blocked_ = false;
|
||||
}
|
||||
|
||||
void Update(size_t index, size_t size, size_t offset, bool blifelong, size_t constraints) {
|
||||
index_ = index;
|
||||
size_ = size;
|
||||
offset_ = offset;
|
||||
lifelong_ = blifelong;
|
||||
constraints_ = constraints;
|
||||
}
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &out, const SomasSolverTensorDescPtr n) {
|
||||
out << n->index_ << " " << n->size_ << " " << n->offset_ << "\n";
|
||||
return out;
|
||||
}
|
||||
friend std::istream &operator>>(std::istream &in, SomasSolverTensorDescPtr n) {
|
||||
in >> n->index_ >> n->size_ >> n->offset_;
|
||||
return in;
|
||||
}
|
||||
};
|
||||
using SomasSolverTensorDescPtr = std::shared_ptr<SomasSolverTensorDesc>;
|
||||
|
||||
class SomasSolverPre {
|
||||
public:
|
||||
SomasSolverPre() = default;
|
||||
~SomasSolverPre() = default;
|
||||
|
||||
SomasSolverPre(const SomasSolverPre &) = delete;
|
||||
SomasSolverPre &operator=(const SomasSolverPre &) = delete;
|
||||
|
||||
size_t GetMaxOffset() { return max_offset_; }
|
||||
|
||||
Status Solving(const session::KernelGraph *graph, std::unordered_map<size_t, SomasSolverTensorDescPtr> *tensors,
|
||||
std::shared_ptr<Array> pConstraints, const vector<vector<size_t>> &continuous_v,
|
||||
bool bVerifySolution, // true -> Check continuous and non overlapping constraints solution
|
||||
bool ball = true, // true -> run full set of heuristics, false -> run single heuristic specified
|
||||
SortingType sorting = kGreaterSizeSmallerIndex, FittingType fitting = kBest,
|
||||
AlgorithmType algorithm = kManyObjects);
|
||||
|
||||
void Log(const session::KernelGraph *graph, const unordered_map<size_t, SomasSolverTensorDescPtr> &tensors,
|
||||
const std::shared_ptr<Array> &pConstraints_v, const vector<vector<size_t>> &continuous_v);
|
||||
|
||||
private:
|
||||
size_t max_offset_;
|
||||
};
|
||||
using SomasSolverPrePtr = std::shared_ptr<SomasSolverPre>;
|
||||
} // namespace somas
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_SOLVER_PRE_H_
|
@ -0,0 +1,53 @@
|
||||
/**
|
||||
* 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 "backend/optimizer/somas/somas_stream.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace somas {
|
||||
void SomasStream::ComputeAncestorStreams() {
|
||||
// (Naive) algorithm: for a given stream, compute its ancestors assuming only distance 1 ancestors are known (handles
|
||||
// cycles between streams)
|
||||
std::set<SomasStreamPtr> current_level, temp_level, already_visited;
|
||||
auto thisPtr = std::make_shared<SomasStream>(id_);
|
||||
already_visited.insert(thisPtr);
|
||||
// Initialize current level to distance 2 ancestors
|
||||
for (auto stream1 : ancestor_streams_) {
|
||||
already_visited.insert(stream1);
|
||||
for (auto stream2 : stream1->ancestor_streams_) {
|
||||
if (std::find(already_visited.begin(), already_visited.end(), stream2) == already_visited.end())
|
||||
current_level.insert(stream2);
|
||||
}
|
||||
}
|
||||
|
||||
while (!current_level.empty()) {
|
||||
// Push current level into ancestors
|
||||
for (auto stream1 : current_level) {
|
||||
ancestor_streams_.insert(stream1);
|
||||
already_visited.insert(stream1);
|
||||
// Keep next level of this ancestor
|
||||
for (auto stream2 : stream1->ancestor_streams_) {
|
||||
if (std::find(already_visited.begin(), already_visited.end(), stream2) == already_visited.end())
|
||||
temp_level.insert(stream2);
|
||||
}
|
||||
}
|
||||
current_level.clear();
|
||||
current_level = temp_level;
|
||||
temp_level.clear();
|
||||
}
|
||||
}
|
||||
} // namespace somas
|
||||
} // namespace mindspore
|
@ -0,0 +1,61 @@
|
||||
/**
|
||||
* 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 MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_STREAM_H_
|
||||
#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_STREAM_H_
|
||||
|
||||
#include "backend/optimizer/somas/somas_node.h"
|
||||
#include "backend/optimizer/somas/somas_tensor.h"
|
||||
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
namespace mindspore {
|
||||
namespace somas {
|
||||
class SomasNode;
|
||||
class SomasTensor;
|
||||
|
||||
using SomasTensorPtr = std::shared_ptr<SomasTensor>;
|
||||
|
||||
class SomasStream {
|
||||
public:
|
||||
using SomasStreamPtr = std::shared_ptr<SomasStream>;
|
||||
|
||||
// Attributes mutated in code
|
||||
std::vector<SomasTensorPtr> tensors_; // vector needed for same-stream loop in ConflictComputing()
|
||||
std::set<SomasStreamPtr> ancestor_streams_;
|
||||
std::set<SomasStreamPtr> ancestor_streams_group_;
|
||||
|
||||
// Constructors/Destructors
|
||||
explicit SomasStream(int64_t id) : id_(id) {}
|
||||
SomasStream(const SomasStream &) = delete;
|
||||
SomasStream &operator=(const SomasStream &) = delete;
|
||||
~SomasStream() = default;
|
||||
|
||||
// Accessors
|
||||
const int64_t &GetId() const { return id_; }
|
||||
|
||||
// Ancestor Computing
|
||||
void ComputeAncestorStreams(); // Given "ancestors at distance one" information, compute "ancestors at any distance"
|
||||
|
||||
private:
|
||||
const int64_t id_{0};
|
||||
};
|
||||
} // namespace somas
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_STREAM_H_
|
@ -0,0 +1,64 @@
|
||||
/**
|
||||
* 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 "backend/optimizer/somas/somas_tensor.h"
|
||||
#include "backend/optimizer/somas/somas_node.h"
|
||||
#include "backend/optimizer/somas/somas_stream.h"
|
||||
#include "backend/optimizer/somas/somas.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace somas {
|
||||
SomasTensor::SomasTensor(size_t id, SomasNodePtr source_node, SomasStreamPtr source_stream, size_t real_size,
|
||||
LifeLongType lifelong_value)
|
||||
: lifelong_value_(lifelong_value),
|
||||
type_(kUnknown),
|
||||
offset_(0),
|
||||
id_(id),
|
||||
source_node_(source_node),
|
||||
source_stream_(source_stream),
|
||||
original_size_(real_size) {
|
||||
const size_t alignment = 512;
|
||||
const size_t alignment_complement = 31;
|
||||
aligned_size_ = (real_size > 0) ? (real_size + alignment + alignment_complement) / alignment * alignment : 0;
|
||||
|
||||
solver_tensor_desc_ = std::make_shared<SomasSolverTensorDesc>(id_, aligned_size_, offset_, false);
|
||||
|
||||
ref_overlap_ = false;
|
||||
between_streams_ = false;
|
||||
num_constraints_ = 0;
|
||||
}
|
||||
|
||||
SomasSolverTensorDescPtr SomasTensor::GetSolverTensorDesc() {
|
||||
if (type_ == kGap) { // ignore lifelong_ value for gaps given to solver, and pass with original_size_
|
||||
solver_tensor_desc_->Update(id_, original_size_, offset_, false, num_constraints_);
|
||||
} else {
|
||||
solver_tensor_desc_->Update(id_, aligned_size_, offset_, lifelong_value_ == kLifeLongGraphAll, num_constraints_);
|
||||
}
|
||||
if (aligned_size_ == 0) { // ignore zero-size tensors for solver
|
||||
return nullptr;
|
||||
} else {
|
||||
return solver_tensor_desc_;
|
||||
}
|
||||
}
|
||||
|
||||
void SomasTensor::ComputeMaxDestinationId() {
|
||||
for (SomasStreamPtr stream : destinationStreams_) max_destination_id_[stream] = 0;
|
||||
|
||||
for (SomasNodePtr node : destinations_)
|
||||
if (node->GetId() > max_destination_id_[node->GetStream()]) max_destination_id_[node->GetStream()] = node->GetId();
|
||||
}
|
||||
} // namespace somas
|
||||
} // namespace mindspore
|
@ -0,0 +1,129 @@
|
||||
/**
|
||||
* 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 MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_TENSOR_H_
|
||||
#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_TENSOR_H_
|
||||
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "backend/optimizer/somas/somas_node.h"
|
||||
#include "backend/optimizer/somas/somas_solver_pre.h"
|
||||
#include "backend/optimizer/somas/somas_stream.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace somas {
|
||||
class SomasNode;
|
||||
class SomasStream;
|
||||
|
||||
// Lifetime type
|
||||
struct Lifetime {
|
||||
size_t start_;
|
||||
size_t end_;
|
||||
|
||||
explicit Lifetime(size_t start = 0, size_t end = 0) : start_(start), end_(end) {}
|
||||
};
|
||||
using lifetime_t = struct Lifetime;
|
||||
|
||||
// Tensor type
|
||||
enum TensorType {
|
||||
kCommon,
|
||||
kOutputOnly,
|
||||
kWorkspace,
|
||||
kGetNextOutput,
|
||||
kSummaryInput,
|
||||
kRefNodeInput,
|
||||
kRefNodeOutput,
|
||||
kGap,
|
||||
kUnknown
|
||||
};
|
||||
|
||||
enum LifeLongType {
|
||||
kLifeLongNone, // life time is from tensor start to tensor end
|
||||
kLifeLongGraphAll, // life time is from graph start to graph end
|
||||
kLifeLongGraphStart, // life time is from graph start to tensor end
|
||||
kLifeLongGraphEnd // life time is from tensor start to graph end
|
||||
};
|
||||
|
||||
using SomasNodePtr = std::shared_ptr<SomasNode>;
|
||||
using SomasStreamPtr = std::shared_ptr<SomasStream>;
|
||||
|
||||
class SomasTensor {
|
||||
public:
|
||||
using SomasTensorPtr = std::shared_ptr<SomasTensor>;
|
||||
|
||||
size_t aligned_size_{0};
|
||||
LifeLongType lifelong_value_;
|
||||
|
||||
bool ref_overlap_;
|
||||
bool between_streams_;
|
||||
|
||||
lifetime_t lifetime_;
|
||||
TensorType type_;
|
||||
|
||||
size_t offset_{0};
|
||||
size_t num_constraints_{0};
|
||||
|
||||
std::set<SomasNodePtr> destinations_;
|
||||
std::set<SomasStreamPtr> destinationStreams_;
|
||||
unordered_map<SomasStreamPtr, size_t> max_destination_id_;
|
||||
|
||||
// Constructors/Destructors
|
||||
explicit SomasTensor(size_t id, SomasNodePtr source_node, SomasStreamPtr source_stream, size_t real_size,
|
||||
LifeLongType lifelong_value = kLifeLongNone);
|
||||
SomasTensor(const SomasTensor &) = delete;
|
||||
SomasTensor &operator=(const SomasTensor &) = delete;
|
||||
~SomasTensor() = default;
|
||||
|
||||
// Accessors
|
||||
const size_t &GetId() { return id_; }
|
||||
SomasNodePtr GetSourceNode() const { return source_node_; }
|
||||
SomasStreamPtr GetSourceStream() const { return source_stream_; }
|
||||
const size_t &GetOriginalSize() { return original_size_; }
|
||||
const size_t &GetAlignedSize() { return aligned_size_; }
|
||||
bool IsLifelong() { return lifelong_value_ == kLifeLongGraphAll; }
|
||||
bool IsWorkspace() { return type_ == kWorkspace; }
|
||||
bool IsOutputOnly() { return type_ == kOutputOnly; }
|
||||
size_t GetOffset() { return offset_; }
|
||||
bool IsBetweenStreams() { return between_streams_; }
|
||||
bool IsSemiLifelongStart() { return lifelong_value_ == kLifeLongGraphStart; }
|
||||
bool IsSemiLifelongEnd() { return lifelong_value_ == kLifeLongGraphEnd; }
|
||||
bool IsRefOverlap() { return ref_overlap_; }
|
||||
bool IsGap() { return type_ == kGap; }
|
||||
|
||||
// Computing functions
|
||||
void SetOffset(size_t start_offset = 0) {
|
||||
if (aligned_size_ != 0 && type_ != kGetNextOutput) {
|
||||
offset_ = start_offset + solver_tensor_desc_->offset_;
|
||||
}
|
||||
}
|
||||
SomasSolverTensorDescPtr GetSolverTensorDesc();
|
||||
void ComputeMaxDestinationId();
|
||||
|
||||
private:
|
||||
const size_t id_{0};
|
||||
const SomasNodePtr source_node_;
|
||||
SomasStreamPtr const source_stream_;
|
||||
const size_t original_size_{0};
|
||||
|
||||
SomasSolverTensorDescPtr solver_tensor_desc_;
|
||||
};
|
||||
} // namespace somas
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_TENSOR_H_
|
Loading…
Reference in new issue