parent
							
								
									5ffb9588ee
								
							
						
					
					
						commit
						756bb6d53f
					
				
											
												
													File diff suppressed because it is too large
													Load Diff
												
											
										
									
								@ -0,0 +1,155 @@
 | 
				
			||||
/**
 | 
				
			||||
 * 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_SESSION_EXECUTOR_H
 | 
				
			||||
#define MINDSPORE_CCSRC_BACKEND_SESSION_EXECUTOR_H
 | 
				
			||||
 | 
				
			||||
#include <vector>
 | 
				
			||||
#include <string>
 | 
				
			||||
#include <utility>
 | 
				
			||||
#include <memory>
 | 
				
			||||
#include <list>
 | 
				
			||||
#include <queue>
 | 
				
			||||
#include <map>
 | 
				
			||||
#include <thread>
 | 
				
			||||
#include <mutex>
 | 
				
			||||
#include <condition_variable>
 | 
				
			||||
#include "backend/session/session_basic.h"
 | 
				
			||||
#include "ir/anf.h"
 | 
				
			||||
#include "ir/tensor.h"
 | 
				
			||||
#include "utils/any.h"
 | 
				
			||||
#include "utils/contract.h"
 | 
				
			||||
 | 
				
			||||
namespace mindspore {
 | 
				
			||||
namespace session {
 | 
				
			||||
enum TaskType { kUnKnown, kExit, kCompileNodes, kCompileGraph, kBuildGraph, kBuildOp, kRunGraph, kRunOp };
 | 
				
			||||
 | 
				
			||||
class Task {
 | 
				
			||||
 public:
 | 
				
			||||
  Task() = default;
 | 
				
			||||
  virtual ~Task() = default;
 | 
				
			||||
  SessionPtr session_{nullptr};
 | 
				
			||||
  TaskType type_{kUnKnown};
 | 
				
			||||
  virtual void Run() {}
 | 
				
			||||
};
 | 
				
			||||
 | 
				
			||||
class CompileNodesTask : public Task {
 | 
				
			||||
 public:
 | 
				
			||||
  CompileNodesTask() { type_ = kCompileNodes; }
 | 
				
			||||
  ~CompileNodesTask() override = default;
 | 
				
			||||
  void Run() override;
 | 
				
			||||
  AnfNodePtrList nodes_;
 | 
				
			||||
  AnfNodePtrList output_nodes_;
 | 
				
			||||
  GraphId graph_id_{0};
 | 
				
			||||
};
 | 
				
			||||
 | 
				
			||||
class CompileGraphTask : public Task {
 | 
				
			||||
 public:
 | 
				
			||||
  CompileGraphTask() { type_ = kCompileGraph; }
 | 
				
			||||
  ~CompileGraphTask() override = default;
 | 
				
			||||
  void Run() override;
 | 
				
			||||
  FuncGraphPtr func_graph_{nullptr};
 | 
				
			||||
  GraphId graph_id_{0};
 | 
				
			||||
};
 | 
				
			||||
 | 
				
			||||
class BuildGraphTask : public Task {
 | 
				
			||||
 public:
 | 
				
			||||
  BuildGraphTask() { type_ = kBuildGraph; }
 | 
				
			||||
  ~BuildGraphTask() override = default;
 | 
				
			||||
  void Run() override;
 | 
				
			||||
  GraphId graph_id_{0};
 | 
				
			||||
};
 | 
				
			||||
 | 
				
			||||
class RunGraphTask : public Task {
 | 
				
			||||
 public:
 | 
				
			||||
  RunGraphTask() { type_ = kRunGraph; }
 | 
				
			||||
  ~RunGraphTask() override = default;
 | 
				
			||||
  void Run() override;
 | 
				
			||||
  std::vector<tensor::TensorPtr> input_tensors_;
 | 
				
			||||
  VectorRef outputs_;
 | 
				
			||||
  GraphId graph_id_{0};
 | 
				
			||||
  std::map<tensor::TensorPtr, session::KernelWithIndex> tensor_to_node_;
 | 
				
			||||
};
 | 
				
			||||
 | 
				
			||||
class BuildOpTask : public Task {
 | 
				
			||||
 public:
 | 
				
			||||
  BuildOpTask() { type_ = kBuildOp; }
 | 
				
			||||
  ~BuildOpTask() override = default;
 | 
				
			||||
  void Run() override;
 | 
				
			||||
  OpRunInfo *op_run_info_{nullptr};
 | 
				
			||||
  GraphInfo graph_info_;
 | 
				
			||||
  std::vector<tensor::TensorPtr> input_tensors_;
 | 
				
			||||
  std::vector<int> tensors_mask_;
 | 
				
			||||
};
 | 
				
			||||
 | 
				
			||||
class RunOpTask : public Task {
 | 
				
			||||
 public:
 | 
				
			||||
  RunOpTask() { type_ = kRunOp; }
 | 
				
			||||
  ~RunOpTask() override = default;
 | 
				
			||||
  void Run() override;
 | 
				
			||||
  OpRunInfo *op_run_info_{nullptr};
 | 
				
			||||
  GraphInfo graph_info_;
 | 
				
			||||
  std::vector<tensor::TensorPtr> input_tensors_;
 | 
				
			||||
  VectorRef outputs_;
 | 
				
			||||
};
 | 
				
			||||
 | 
				
			||||
class ExitTask : public Task {
 | 
				
			||||
 public:
 | 
				
			||||
  ExitTask() { type_ = kExit; }
 | 
				
			||||
  ~ExitTask() override = default;
 | 
				
			||||
};
 | 
				
			||||
 | 
				
			||||
class Executor {
 | 
				
			||||
 public:
 | 
				
			||||
  Executor(const std::string &device_name, uint32_t device_id);
 | 
				
			||||
  ~Executor() = default;
 | 
				
			||||
  void WorkerLoop();
 | 
				
			||||
  void WorkerJoin();
 | 
				
			||||
  GraphId CompileGraphAsync(const SessionPtr &session, const AnfNodePtrList &lst, const AnfNodePtrList &outputs);
 | 
				
			||||
  GraphId CompileGraphAsync(const SessionPtr &session, NotNull<FuncGraphPtr> func_graph);
 | 
				
			||||
  void BuildGraphAsync(const SessionPtr &session, GraphId graphId);
 | 
				
			||||
  void RunGraphAsync(const SessionPtr &session, const GraphId &graph_id, const std::vector<tensor::TensorPtr> &inputs,
 | 
				
			||||
                     VectorRef *outputs);
 | 
				
			||||
  void BuildOpAsync(const SessionPtr &session, OpRunInfo *op_run_info, const GraphInfo &graph_info,
 | 
				
			||||
                    const std::vector<tensor::TensorPtr> &input_tensors, const std::vector<int> &tensors_mask);
 | 
				
			||||
  py::tuple RunOpAsync(const SessionPtr &session, OpRunInfo *op_run_info, const GraphInfo &graph_info,
 | 
				
			||||
                       const std::vector<tensor::TensorPtr> &input_tensors);
 | 
				
			||||
  void OnRunGraphFinished();
 | 
				
			||||
 | 
				
			||||
 protected:
 | 
				
			||||
  void UpdateOutputTensors(VectorRef *outputs,
 | 
				
			||||
                           const std::map<tensor::TensorPtr, session::KernelWithIndex> &tensor_to_node);
 | 
				
			||||
  std::vector<std::shared_ptr<RunGraphTask>> GetNewReadyTasks();
 | 
				
			||||
  bool IsAllInputsReady(const std::vector<tensor::TensorPtr> &inputs);
 | 
				
			||||
  void StopWorker();
 | 
				
			||||
  void OnWorkerExit();
 | 
				
			||||
 | 
				
			||||
  uint32_t device_id_;
 | 
				
			||||
  std::string device_name_;
 | 
				
			||||
  std::mutex task_mutex_;
 | 
				
			||||
  std::mutex pending_task_mutex_;
 | 
				
			||||
  std::condition_variable task_cond_var_;
 | 
				
			||||
  std::condition_variable compile_cond_var_;
 | 
				
			||||
  std::condition_variable build_cond_var_;
 | 
				
			||||
  std::condition_variable run_cond_var_;
 | 
				
			||||
  std::condition_variable build_op_cond_var_;
 | 
				
			||||
  std::condition_variable run_op_cond_var_;
 | 
				
			||||
  std::queue<std::shared_ptr<Task>> ready_tasks_;
 | 
				
			||||
  std::list<std::shared_ptr<RunGraphTask>> pending_tasks_;
 | 
				
			||||
  std::shared_ptr<std::thread> worker_;
 | 
				
			||||
};
 | 
				
			||||
}  // namespace session
 | 
				
			||||
}  // namespace mindspore
 | 
				
			||||
#endif  // MINDSPORE_CCSRC_BACKEND_SESSION_EXECUTOR_H
 | 
				
			||||
@ -0,0 +1,54 @@
 | 
				
			||||
/**
 | 
				
			||||
 * 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/session/executor_manager.h"
 | 
				
			||||
 | 
				
			||||
namespace mindspore {
 | 
				
			||||
namespace session {
 | 
				
			||||
std::shared_ptr<Executor> ExecutorManager::GetExecutor(const std::string &device_name, int device_id) {
 | 
				
			||||
  std::string device_key = device_name + "_" + std::to_string(device_id);
 | 
				
			||||
  auto iter = executors_.find(device_key);
 | 
				
			||||
  if (iter != executors_.end()) {
 | 
				
			||||
    return iter->second;
 | 
				
			||||
  }
 | 
				
			||||
  auto executor = std::make_shared<Executor>(device_name, device_id);
 | 
				
			||||
  executors_[device_key] = executor;
 | 
				
			||||
  return executor;
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
void ExecutorManager::OnRunGraphFinished() {
 | 
				
			||||
  for (auto &item : executors_) {
 | 
				
			||||
    auto &executor = item.second;
 | 
				
			||||
    if (executor != nullptr) {
 | 
				
			||||
      executor->OnRunGraphFinished();
 | 
				
			||||
    }
 | 
				
			||||
  }
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
void ExecutorManager::JoinExecutorWorkers() {
 | 
				
			||||
  for (auto &item : executors_) {
 | 
				
			||||
    auto &executor = item.second;
 | 
				
			||||
    if (executor != nullptr) {
 | 
				
			||||
      executor->WorkerJoin();
 | 
				
			||||
    }
 | 
				
			||||
  }
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
void ExecutorManager::Clear() {
 | 
				
			||||
  JoinExecutorWorkers();
 | 
				
			||||
  executors_.clear();
 | 
				
			||||
}
 | 
				
			||||
}  // namespace session
 | 
				
			||||
}  // namespace mindspore
 | 
				
			||||
@ -0,0 +1,45 @@
 | 
				
			||||
/**
 | 
				
			||||
 * 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_SESSION_EXECUTOR_MANGER_H_
 | 
				
			||||
#define MINDSPORE_CCSRC_BACKEND_SESSION_EXECUTOR_MANGER_H_
 | 
				
			||||
#include <set>
 | 
				
			||||
#include <map>
 | 
				
			||||
#include <string>
 | 
				
			||||
#include <memory>
 | 
				
			||||
#include "backend/session/executor.h"
 | 
				
			||||
namespace mindspore {
 | 
				
			||||
namespace session {
 | 
				
			||||
class Executor;
 | 
				
			||||
class ExecutorManager {
 | 
				
			||||
 public:
 | 
				
			||||
  static ExecutorManager &Instance() {
 | 
				
			||||
    static ExecutorManager instance;
 | 
				
			||||
    return instance;
 | 
				
			||||
  }
 | 
				
			||||
  std::shared_ptr<Executor> GetExecutor(const std::string &device_name, int device_id);
 | 
				
			||||
  void OnRunGraphFinished();
 | 
				
			||||
  void Clear();
 | 
				
			||||
 | 
				
			||||
 private:
 | 
				
			||||
  ExecutorManager() = default;
 | 
				
			||||
  ~ExecutorManager() = default;
 | 
				
			||||
  DISABLE_COPY_AND_ASSIGN(ExecutorManager)
 | 
				
			||||
  void JoinExecutorWorkers();
 | 
				
			||||
  std::map<std::string, std::shared_ptr<Executor>> executors_;
 | 
				
			||||
};
 | 
				
			||||
}  // namespace session
 | 
				
			||||
}  // namespace mindspore
 | 
				
			||||
#endif  // MINDSPORE_CCSRC_BACKEND_SESSION_EXECUTOR_MANGER_H_
 | 
				
			||||
Some files were not shown because too many files have changed in this diff Show More
					Loading…
					
					
				
		Reference in new issue