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 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/physical_tensor.h"
  13. #include "megbrain/imperative/op_def.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. struct TensorInfo;
  23. using TensorInfoPtr = std::shared_ptr<TensorInfo>;
  24. struct TensorInfo {
  25. enum Prop {
  26. Device, Shape, DType, DevValue, HostValue
  27. };
  28. uint64_t id;
  29. TensorPtr ptr;
  30. LogicalTensorDesc desc;
  31. // FIXME: broken by drop
  32. bool value_fetched = false;
  33. bool invalid = false;
  34. bool allow_delete = false;
  35. EvictType evict_type = NONE;
  36. HostTensorND h_value;
  37. // reserved for auto drop
  38. size_t pinned = 0;
  39. size_t recompute_times = 0;
  40. struct ComputePath {
  41. std::shared_ptr<OpDef> op;
  42. SmallVector<TensorInfo*> inputs;
  43. SmallVector<TensorInfo*> unique_inputs;
  44. SmallVector<TensorInfo*> outputs;
  45. size_t ref_cnt() {
  46. return outputs.size() - std::count(outputs.begin(), outputs.end(), nullptr);
  47. }
  48. static ComputePath* make(std::shared_ptr<OpDef> op, SmallVector<TensorInfo*> inputs, SmallVector<TensorInfo*> outputs) {
  49. auto* path = new TensorInfo::ComputePath();
  50. path->op = op;
  51. path->inputs = inputs;
  52. path->outputs = outputs;
  53. // dedup
  54. SmallVector<TensorInfo*> unique_inputs = inputs;
  55. std::sort(unique_inputs.begin(), unique_inputs.end());
  56. unique_inputs.erase(std::unique(unique_inputs.begin(), unique_inputs.end()), unique_inputs.end());
  57. path->unique_inputs = unique_inputs;
  58. // attach users
  59. for (auto input: unique_inputs) {
  60. input->users.push_back(path);
  61. }
  62. // attach producer
  63. for (auto output: outputs) {
  64. output->producer = path;
  65. }
  66. return path;
  67. }
  68. }* producer = nullptr;
  69. void pin() {
  70. ++pinned;
  71. }
  72. void unpin() {
  73. --pinned;
  74. }
  75. void detach_producer() {
  76. if (!producer) {
  77. return;
  78. }
  79. auto output = std::find(producer->outputs.begin(), producer->outputs.end(), this);
  80. mgb_assert(output != producer->outputs.end());
  81. *output = nullptr;
  82. if (producer->ref_cnt() == 0) {
  83. for (auto* input: producer->unique_inputs) {
  84. input->users.erase(std::find(input->users.begin(), input->users.end(), producer));
  85. }
  86. delete producer;
  87. }
  88. producer = nullptr;
  89. }
  90. SmallVector<ComputePath*> users;
  91. };
  92. }
  93. template <>
  94. struct ToStringTrait<interpreter::intl::TensorInfo::Prop>{
  95. using TensorInfo = interpreter::intl::TensorInfo;
  96. std::string operator()(TensorInfo::Prop prop) const {
  97. switch(prop) {
  98. case TensorInfo::DType:
  99. return "dtype";
  100. case TensorInfo::DevValue:
  101. return "dev_value";
  102. case TensorInfo::Device:
  103. return "device";
  104. case TensorInfo::HostValue:
  105. return "host_value";
  106. case TensorInfo::Shape:
  107. return "shape";
  108. default:
  109. return "unknown";
  110. }
  111. }
  112. };
  113. }

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