LLVM  17.0.0git
TensorSpec.cpp
Go to the documentation of this file.
1 //===- TensorSpec.cpp - tensor type abstraction ---------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implementation file for the abstraction of a tensor type, and JSON loading
10 // utils.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/Config/config.h"
15 
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/Twine.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/JSON.h"
24 #include <array>
25 #include <cassert>
26 #include <numeric>
27 
28 using namespace llvm;
29 
30 namespace llvm {
31 
32 #define TFUTILS_GETDATATYPE_IMPL(T, E) \
33  template <> TensorType TensorSpec::getDataType<T>() { return TensorType::E; }
34 
36 
37 #undef TFUTILS_GETDATATYPE_IMPL
38 
39 static std::array<std::string, static_cast<size_t>(TensorType::Total)>
40  TensorTypeNames{"INVALID",
41 #define TFUTILS_GETNAME_IMPL(T, _) #T,
43 #undef TFUTILS_GETNAME_IMPL
44  };
45 
47  return TensorTypeNames[static_cast<size_t>(TT)];
48 }
49 
51  OS.object([&]() {
52  OS.attribute("name", name());
53  OS.attribute("type", toString(type()));
54  OS.attribute("port", port());
55  OS.attributeArray("shape", [&]() {
56  for (size_t D : shape())
57  OS.value(static_cast<int64_t>(D));
58  });
59  });
60 }
61 
62 TensorSpec::TensorSpec(const std::string &Name, int Port, TensorType Type,
63  size_t ElementSize, const std::vector<int64_t> &Shape)
64  : Name(Name), Port(Port), Type(Type), Shape(Shape),
65  ElementCount(std::accumulate(Shape.begin(), Shape.end(), 1,
66  std::multiplies<int64_t>())),
67  ElementSize(ElementSize) {}
68 
69 std::optional<TensorSpec> getTensorSpecFromJSON(LLVMContext &Ctx,
70  const json::Value &Value) {
71  auto EmitError =
72  [&](const llvm::Twine &Message) -> std::optional<TensorSpec> {
73  std::string S;
75  OS << Value;
76  Ctx.emitError("Unable to parse JSON Value as spec (" + Message + "): " + S);
77  return std::nullopt;
78  };
79  // FIXME: accept a Path as a parameter, and use it for error reporting.
80  json::Path::Root Root("tensor_spec");
81  json::ObjectMapper Mapper(Value, Root);
82  if (!Mapper)
83  return EmitError("Value is not a dict");
84 
85  std::string TensorName;
86  int TensorPort = -1;
87  std::string TensorType;
88  std::vector<int64_t> TensorShape;
89 
90  if (!Mapper.map<std::string>("name", TensorName))
91  return EmitError("'name' property not present or not a string");
92  if (!Mapper.map<std::string>("type", TensorType))
93  return EmitError("'type' property not present or not a string");
94  if (!Mapper.map<int>("port", TensorPort))
95  return EmitError("'port' property not present or not an int");
96  if (!Mapper.map<std::vector<int64_t>>("shape", TensorShape))
97  return EmitError("'shape' property not present or not an int array");
98 
99 #define PARSE_TYPE(T, E) \
100  if (TensorType == #T) \
101  return TensorSpec::createSpec<T>(TensorName, TensorShape, TensorPort);
103 #undef PARSE_TYPE
104  return std::nullopt;
105 }
106 
107 std::string tensorValueToString(const char *Buffer, const TensorSpec &Spec) {
108  switch (Spec.type()) {
109 #define _IMR_DBG_PRINTER(T, N) \
110  case TensorType::N: { \
111  const T *TypedBuff = reinterpret_cast<const T *>(Buffer); \
112  auto R = llvm::make_range(TypedBuff, TypedBuff + Spec.getElementCount()); \
113  return llvm::join( \
114  llvm::map_range(R, [](T V) { return std::to_string(V); }), ","); \
115  }
117 #undef _IMR_DBG_PRINTER
118  case TensorType::Total:
119  case TensorType::Invalid:
120  llvm_unreachable("invalid tensor type");
121  }
122  // To appease warnings about not all control paths returning a value.
123  return "";
124 }
125 
126 } // namespace llvm
llvm::TensorType::Invalid
@ Invalid
llvm::json::Path::Root
The root is the trivial Path to the root value.
Definition: JSON.h:679
llvm::json::Value
A Value is an JSON value of unknown type.
Definition: JSON.h:282
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::LLVMContext::emitError
void emitError(uint64_t LocCookie, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
Definition: LLVMContext.cpp:271
llvm::ElementCount
Definition: TypeSize.h:279
llvm::tensorValueToString
std::string tensorValueToString(const char *Buffer, const TensorSpec &Spec)
For debugging.
Definition: TensorSpec.cpp:107
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:642
TensorSpec.h
ManagedStatic.h
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::TensorType::Total
@ Total
llvm::sys::path::end
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:235
llvm::sys::path::begin
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:226
llvm::json::OStream::attributeArray
void attributeArray(llvm::StringRef Key, Block Contents)
Emit an attribute whose value is an array with elements from the Block.
Definition: JSON.h:1015
_IMR_DBG_PRINTER
#define _IMR_DBG_PRINTER(T, N)
llvm::json::ObjectMapper::map
bool map(StringLiteral Prop, T &Out)
Maps a property to a field.
Definition: JSON.h:833
STLExtras.h
CommandLine.h
PARSE_TYPE
#define PARSE_TYPE(T, E)
llvm::json::OStream::object
void object(Block Contents)
Emit an object whose elements are emitted in the provided Block.
Definition: JSON.h:986
Twine.h
llvm::TensorSpec::port
int port() const
Definition: TensorSpec.h:64
JSON.h
llvm::TensorSpec::type
TensorType type() const
Definition: TensorSpec.h:65
llvm::json::OStream
json::OStream allows writing well-formed JSON without materializing all structures as json::Value ahe...
Definition: JSON.h:956
D
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
StringExtras.h
llvm::TensorTypeNames
static std::array< std::string, static_cast< size_t >TensorType::Total)> TensorTypeNames
Definition: TensorSpec.cpp:40
llvm::TensorSpec::TensorSpec
TensorSpec(const std::string &NewName, const TensorSpec &Other)
Definition: TensorSpec.h:86
llvm::TensorSpec::name
const std::string & name() const
Definition: TensorSpec.h:63
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::TensorType
TensorType
Definition: TensorSpec.h:46
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
SUPPORTED_TENSOR_TYPES
#define SUPPORTED_TENSOR_TYPES(M)
TensorSpec encapsulates the specification of a tensor: its dimensions, or "shape" (row-major),...
Definition: TensorSpec.h:34
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
std
Definition: BitVector.h:851
llvm::toString
const char * toString(DWARFSectionKind Kind)
Definition: DWARFUnitIndex.h:67
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::json::OStream::value
void value(const Value &V)
Emit a self-contained value (number, string, vector<string> etc).
Definition: JSON.cpp:756
TFUTILS_GETNAME_IMPL
#define TFUTILS_GETNAME_IMPL(T, _)
llvm::TensorSpec::shape
const std::vector< int64_t > & shape() const
Definition: TensorSpec.h:66
llvm::Spec
Definition: FunctionSpecialization.h:87
TFUTILS_GETDATATYPE_IMPL
#define TFUTILS_GETDATATYPE_IMPL(T, E)
Definition: TensorSpec.cpp:32
llvm::json::OStream::attribute
void attribute(llvm::StringRef Key, const Value &Contents)
Emit an attribute whose value is self-contained (number, vector<int> etc).
Definition: JSON.h:1011
raw_ostream.h
llvm::getTensorSpecFromJSON
std::optional< TensorSpec > getTensorSpecFromJSON(LLVMContext &Ctx, const json::Value &Value)
Construct a TensorSpec from a JSON dictionary of the form: { "name": <string>, "port": <int>,...
Definition: TensorSpec.cpp:69
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
Debug.h
llvm::TensorSpec::toJSON
void toJSON(json::OStream &OS) const
Definition: TensorSpec.cpp:50
llvm::TensorSpec
Definition: TensorSpec.h:54
llvm::json::ObjectMapper
Helper for mapping JSON objects onto protocol structs.
Definition: JSON.h:819