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_array.h 3.8 kB

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * Copyright 2021 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MINDSPORE_CCSRC_RUNTIME_DEVICE_TENSOR_ARRAY_H_
  17. #define MINDSPORE_CCSRC_RUNTIME_DEVICE_TENSOR_ARRAY_H_
  18. #include <vector>
  19. #include <string>
  20. #include <memory>
  21. #include "backend/common/session/kernel_graph.h"
  22. #include "backend/common/session/anf_runtime_algorithm.h"
  23. #include "include/common/utils/anfalgo.h"
  24. #include "kernel/kernel.h"
  25. namespace mindspore {
  26. namespace device {
  27. class TensorArray {
  28. public:
  29. // Base TensorArray. Constructed by name, dtype and shapes.
  30. TensorArray(const string &name, const TypePtr &dtype, const std::vector<size_t> &shapes)
  31. : name_(name), dtype_(dtype), shapes_(shapes), valid_size_(0), max_size_(0), is_dynamic_(true) {}
  32. virtual ~TensorArray() = default;
  33. // Check the index in valid range. Used in Read().
  34. virtual bool CheckReadIndexLogical(const int64_t index);
  35. // Check the dtype and shape of the input data. Used in Write().
  36. virtual bool CheckValue(const TypeId &dtype, const std::vector<size_t> &shape);
  37. // Function Write() is used to insert or append dev_value to the position of index.
  38. virtual bool Write(const int64_t index, const mindspore::kernel::AddressPtr &dev_value);
  39. // Function Read() can get the tensors in the scope of tensors_.
  40. virtual mindspore::kernel::AddressPtr Read(const int64_t index);
  41. // Function Free() will release the memory in TensorArray.
  42. virtual void Free();
  43. // These three func should by implied for different device due to the difference in memory usage.
  44. // Create/Release Memory is used for malloc/free a device memory, used in function Write().
  45. // ClearMemory is used to reset the input addr with zeros, used in function Free().
  46. virtual void ReleaseMemory(const DeviceMemPtr addr) = 0;
  47. virtual void *CreateMemory(const size_t size) = 0;
  48. virtual void ClearMemory(void *addr, const size_t size) = 0;
  49. // Clear() will only set the valid size of TensorArray to zero. The memory in TensorArray is still
  50. // kept. In this situation, we can reuse the memory for next using.
  51. virtual void Clear();
  52. // A vector of tensor address are kept in a TensorArray. For memory reusing, we will keep the addr
  53. // after Clear(), in this time, the valid size will be zero but the real size still kept as
  54. // tensors_.size(). Overall, using GetValidSize() to get a logical TensorArray size, and using
  55. // GetRealSize() to get a physical TensorArray size.
  56. virtual size_t GetValidSize() const;
  57. virtual size_t GetRealSize() const;
  58. // This function is used in the situation that is_dynamic == false then set the max size.
  59. // Otherwise, it won't be used and use the default implement.
  60. virtual void SetMaxSize(const int64_t size, const bool is_dynamic);
  61. // Return the tensor address in position index.
  62. virtual const void *GetTensorAddr(const size_t &index) const;
  63. protected:
  64. std::string name_;
  65. TypePtr dtype_;
  66. std::vector<size_t> shapes_;
  67. size_t valid_size_;
  68. int64_t max_size_;
  69. bool is_dynamic_;
  70. // Using a vector tensors_ to store the dev_tensor_addr from Write().
  71. std::vector<mindspore::kernel::AddressPtr> tensors_;
  72. };
  73. using TensorArrayPtr = std::shared_ptr<TensorArray>;
  74. } // namespace device
  75. } // namespace mindspore
  76. #endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_TENSOR_ARRAY_H_