LLVM  16.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/Config/config.h"
14 
15 #include "llvm/ADT/Twine.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/JSON.h"
22 #include <array>
23 #include <cassert>
24 #include <numeric>
25 
26 using namespace llvm;
27 
28 namespace llvm {
29 
30 #define TFUTILS_GETDATATYPE_IMPL(T, E) \
31  template <> TensorType TensorSpec::getDataType<T>() { return TensorType::E; }
32 
34 
35 #undef TFUTILS_GETDATATYPE_IMPL
36 
37 static std::array<std::string, static_cast<size_t>(TensorType::Total)>
38  TensorTypeNames{"INVALID",
39 #define TFUTILS_GETNAME_IMPL(T, _) #T,
41 #undef TFUTILS_GETNAME_IMPL
42  };
43 
45  return TensorTypeNames[static_cast<size_t>(TT)];
46 }
47 
49  OS.object([&]() {
50  OS.attribute("name", name());
51  OS.attribute("type", toString(type()));
52  OS.attribute("port", port());
53  OS.attributeArray("shape", [&]() {
54  for (size_t D : shape())
55  OS.value(static_cast<int64_t>(D));
56  });
57  });
58 }
59 
60 TensorSpec::TensorSpec(const std::string &Name, int Port, TensorType Type,
61  size_t ElementSize, const std::vector<int64_t> &Shape)
62  : Name(Name), Port(Port), Type(Type), Shape(Shape),
63  ElementCount(std::accumulate(Shape.begin(), Shape.end(), 1,
64  std::multiplies<int64_t>())),
65  ElementSize(ElementSize) {}
66 
68  const json::Value &Value) {
69  auto EmitError = [&](const llvm::Twine &Message) -> Optional<TensorSpec> {
70  std::string S;
72  OS << Value;
73  Ctx.emitError("Unable to parse JSON Value as spec (" + Message + "): " + S);
74  return std::nullopt;
75  };
76  // FIXME: accept a Path as a parameter, and use it for error reporting.
77  json::Path::Root Root("tensor_spec");
78  json::ObjectMapper Mapper(Value, Root);
79  if (!Mapper)
80  return EmitError("Value is not a dict");
81 
82  std::string TensorName;
83  int TensorPort = -1;
84  std::string TensorType;
85  std::vector<int64_t> TensorShape;
86 
87  if (!Mapper.map<std::string>("name", TensorName))
88  return EmitError("'name' property not present or not a string");
89  if (!Mapper.map<std::string>("type", TensorType))
90  return EmitError("'type' property not present or not a string");
91  if (!Mapper.map<int>("port", TensorPort))
92  return EmitError("'port' property not present or not an int");
93  if (!Mapper.map<std::vector<int64_t>>("shape", TensorShape))
94  return EmitError("'shape' property not present or not an int array");
95 
96 #define PARSE_TYPE(T, E) \
97  if (TensorType == #T) \
98  return TensorSpec::createSpec<T>(TensorName, TensorShape, TensorPort);
100 #undef PARSE_TYPE
101  return std::nullopt;
102 }
103 
104 } // namespace llvm
llvm::json::Path::Root
The root is the trivial Path to the root value.
Definition: JSON.h:680
llvm::json::Value
A Value is an JSON value of unknown type.
Definition: JSON.h:283
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:404
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:629
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:1016
llvm::Optional
Definition: APInt.h:33
llvm::json::ObjectMapper::map
bool map(StringLiteral Prop, T &Out)
Maps a property to a field.
Definition: JSON.h:834
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:987
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:957
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
llvm::TensorTypeNames
static std::array< std::string, static_cast< size_t >TensorType::Total)> TensorTypeNames
Definition: TensorSpec.cpp:38
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::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
TFUTILS_GETDATATYPE_IMPL
#define TFUTILS_GETDATATYPE_IMPL(T, E)
Definition: TensorSpec.cpp:30
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:1012
raw_ostream.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
Debug.h
llvm::TensorSpec::toJSON
void toJSON(json::OStream &OS) const
Definition: TensorSpec.cpp:48
llvm::json::ObjectMapper
Helper for mapping JSON objects onto protocol structs.
Definition: JSON.h:820
llvm::getTensorSpecFromJSON
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:67