You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

tensor_info.h 5.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * \file imperative/src/impl/interpreter/tensor_info.h
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #pragma once
  12. #include "megbrain/imperative/op_def.h"
  13. #include "megbrain/imperative/physical_tensor.h"
  14. #include "megbrain/imperative/utils/to_string.h"
  15. namespace mgb::imperative {
  16. namespace interpreter::intl {
  17. enum EvictType {
  18. NONE = 0,
  19. SWAP = 1,
  20. DROP = 2,
  21. };
  22. /*!
  23. * \brief an identifier to specify a component of evicted tensors
  24. *
  25. * Each component tracks the sum of the compute costs of its elements, with the
  26. * union of two components having the sum of each constituent cost.
  27. */
  28. struct DsuNode {
  29. DsuNode(double _t) : t(_t) {}
  30. std::shared_ptr<DsuNode> parent;
  31. bool is_root() { return !bool(parent); }
  32. double t;
  33. };
  34. struct TensorInfo;
  35. using TensorInfoPtr = std::shared_ptr<TensorInfo>;
  36. struct TensorInfo {
  37. enum Status {
  38. InvalidStatus,
  39. Allocated,
  40. Produced,
  41. Swapped,
  42. Dropped,
  43. Deleted,
  44. };
  45. uint64_t id = -1;
  46. std::string name;
  47. // Most attrs of TensorInfo, except `ptr` and `h_value`,
  48. // were visited read and written in main thread.
  49. // Lock interpreter when visiting `ptr`.
  50. TensorPtr ptr;
  51. LogicalTensorDesc desc;
  52. MemoryDesc mem_desc;
  53. double compute_time;
  54. size_t memory;
  55. double last_used_time;
  56. bool invalid = false;
  57. bool allow_delete = false;
  58. EvictType evict_type = NONE;
  59. // Status should be only modified in worker thread
  60. Status status = InvalidStatus;
  61. // Used by HostCompute and Memory Swap.
  62. // HostCompute and Swap does not happen in one thread.
  63. // Maybe a barrier is needed.
  64. HostTensorND h_value;
  65. // reserved for auto drop
  66. size_t pinned = 0;
  67. size_t recompute_times = 0;
  68. size_t ref_cnt = 0;
  69. std::shared_ptr<DsuNode> dsu_ptr;
  70. // Not reference count, inc when used as input
  71. size_t ptr_use_count = 0;
  72. // Used by `Drop` action
  73. struct ComputePath {
  74. uint64_t id;
  75. std::shared_ptr<OpDef> op;
  76. SmallVector<TensorInfo*> inputs;
  77. SmallVector<TensorInfo*> unique_inputs;
  78. SmallVector<TensorInfo*> outputs;
  79. size_t ref_cnt() {
  80. return outputs.size() - std::count(outputs.begin(), outputs.end(), nullptr);
  81. }
  82. static ComputePath* make(
  83. uint64_t id, std::shared_ptr<OpDef> op, SmallVector<TensorInfo*> inputs,
  84. SmallVector<TensorInfo*> outputs) {
  85. auto* path = new TensorInfo::ComputePath();
  86. path->id = id;
  87. path->op = op;
  88. path->inputs = inputs;
  89. path->outputs = outputs;
  90. // dedup
  91. SmallVector<TensorInfo*> unique_inputs = inputs;
  92. std::sort(unique_inputs.begin(), unique_inputs.end());
  93. unique_inputs.erase(
  94. std::unique(unique_inputs.begin(), unique_inputs.end()),
  95. unique_inputs.end());
  96. path->unique_inputs = unique_inputs;
  97. // attach users
  98. for (auto input : unique_inputs) {
  99. input->users.push_back(path);
  100. }
  101. // attach producer
  102. for (auto output : outputs) {
  103. output->producer = path;
  104. }
  105. // update ref_cnt
  106. for (auto input : inputs) {
  107. input->ref_cnt += outputs.size();
  108. }
  109. return path;
  110. }
  111. }* producer = nullptr;
  112. double eval_func(
  113. double cost, double free_mem, double cur_time, double param_cost,
  114. double param_mem, double param_time, double param_recompute_times) {
  115. return pow(cost + 1e-3, param_cost) *
  116. pow(param_recompute_times, (double)recompute_times) /
  117. (pow((memory + free_mem) / 1024.0 / 1024.0, param_mem) *
  118. pow((double)(cur_time - last_used_time + 1e-3), param_time));
  119. }
  120. void pin() { ++pinned; }
  121. void unpin() { --pinned; }
  122. // returns true if producer is deleted
  123. bool detach_producer() {
  124. if (!producer) {
  125. return false;
  126. }
  127. auto output =
  128. std::find(producer->outputs.begin(), producer->outputs.end(), this);
  129. mgb_assert(output != producer->outputs.end());
  130. *output = nullptr;
  131. bool deleted = false;
  132. if (producer->ref_cnt() == 0) {
  133. for (auto* input : producer->unique_inputs) {
  134. input->users.erase(
  135. std::find(input->users.begin(), input->users.end(), producer));
  136. }
  137. delete producer;
  138. deleted = true;
  139. }
  140. producer = nullptr;
  141. return deleted;
  142. }
  143. bool size_exceeds_thd(size_t thd) { return memory > thd; }
  144. SmallVector<ComputePath*> users;
  145. // UINT_MAX as a magic default value
  146. size_t cand_index = UINT_MAX;
  147. };
  148. } // namespace interpreter::intl
  149. } // namespace mgb::imperative

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台