TensorSpec.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- TensorSpec.h - type descriptor for a tensor --------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. #ifndef LLVM_ANALYSIS_TENSORSPEC_H
  15. #define LLVM_ANALYSIS_TENSORSPEC_H
  16. #include "llvm/Config/llvm-config.h"
  17. #include "llvm/ADT/StringMap.h"
  18. #include "llvm/IR/LLVMContext.h"
  19. #include "llvm/Support/JSON.h"
  20. #include <memory>
  21. #include <optional>
  22. #include <vector>
  23. namespace llvm {
  24. /// TensorSpec encapsulates the specification of a tensor: its dimensions, or
  25. /// "shape" (row-major), its type (see TensorSpec::getDataType specializations
  26. /// for supported types), its name and port (see "TensorFlow: Large-Scale
  27. /// Machine Learning on Heterogeneous Distributed Systems", section 4.2, para 2:
  28. /// https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/45166.pdf)
  29. ///
  30. /// Known tensor types. The left part is the C type, the right is a name we
  31. /// can use to identify the type (to implement TensorSpec equality checks), and
  32. /// to use, if needed, when mapping to an underlying evaluator's type system.
  33. /// The main requirement is that the C type we use has the same size and
  34. /// encoding (e.g. endian-ness) as the one used by the evaluator.
  35. #define SUPPORTED_TENSOR_TYPES(M) \
  36. M(float, Float) \
  37. M(double, Double) \
  38. M(int8_t, Int8) \
  39. M(uint8_t, UInt8) \
  40. M(int16_t, Int16) \
  41. M(uint16_t, UInt16) \
  42. M(int32_t, Int32) \
  43. M(uint32_t, UInt32) \
  44. M(int64_t, Int64) \
  45. M(uint64_t, UInt64)
  46. enum class TensorType {
  47. Invalid,
  48. #define _TENSOR_TYPE_ENUM_MEMBERS(_, Name) Name,
  49. SUPPORTED_TENSOR_TYPES(_TENSOR_TYPE_ENUM_MEMBERS)
  50. #undef _TENSOR_TYPE_ENUM_MEMBERS
  51. Total
  52. };
  53. class TensorSpec final {
  54. public:
  55. template <typename T>
  56. static TensorSpec createSpec(const std::string &Name,
  57. const std::vector<int64_t> &Shape,
  58. int Port = 0) {
  59. return TensorSpec(Name, Port, getDataType<T>(), sizeof(T), Shape);
  60. }
  61. const std::string &name() const { return Name; }
  62. int port() const { return Port; }
  63. TensorType type() const { return Type; }
  64. const std::vector<int64_t> &shape() const { return Shape; }
  65. bool operator==(const TensorSpec &Other) const {
  66. return Name == Other.Name && Port == Other.Port && Type == Other.Type &&
  67. Shape == Other.Shape;
  68. }
  69. bool operator!=(const TensorSpec &Other) const { return !(*this == Other); }
  70. /// Get the number of elements in a tensor with this shape.
  71. size_t getElementCount() const { return ElementCount; }
  72. /// Get the size, in bytes, of one element.
  73. size_t getElementByteSize() const { return ElementSize; }
  74. /// Get the total size of a memory buffer needed to store the whole tensor.
  75. size_t getTotalTensorBufferSize() const { return ElementCount * ElementSize; }
  76. template <typename T> bool isElementType() const {
  77. return getDataType<T>() == Type;
  78. }
  79. TensorSpec(const std::string &NewName, const TensorSpec &Other)
  80. : TensorSpec(NewName, Other.Port, Other.Type, Other.ElementSize,
  81. Other.Shape) {}
  82. void toJSON(json::OStream &OS) const;
  83. private:
  84. TensorSpec(const std::string &Name, int Port, TensorType Type,
  85. size_t ElementSize, const std::vector<int64_t> &Shape);
  86. template <typename T> static TensorType getDataType();
  87. std::string Name;
  88. int Port = 0;
  89. TensorType Type = TensorType::Invalid;
  90. std::vector<int64_t> Shape;
  91. size_t ElementCount = 0;
  92. size_t ElementSize = 0;
  93. };
  94. /// Construct a TensorSpec from a JSON dictionary of the form:
  95. /// { "name": <string>,
  96. /// "port": <int>,
  97. /// "type": <string. Use LLVM's types, e.g. float, double, int64_t>,
  98. /// "shape": <array of ints> }
  99. /// For the "type" field, see the C++ primitive types used in
  100. /// TFUTILS_SUPPORTED_TYPES.
  101. std::optional<TensorSpec> getTensorSpecFromJSON(LLVMContext &Ctx,
  102. const json::Value &Value);
  103. #define TFUTILS_GETDATATYPE_DEF(T, Name) \
  104. template <> TensorType TensorSpec::getDataType<T>();
  105. SUPPORTED_TENSOR_TYPES(TFUTILS_GETDATATYPE_DEF)
  106. #undef TFUTILS_GETDATATYPE_DEF
  107. } // namespace llvm
  108. #endif // LLVM_ANALYSIS_TENSORSPEC_H
  109. #ifdef __GNUC__
  110. #pragma GCC diagnostic pop
  111. #endif