LLVM 22.0.0git
TensorSpec.h
Go to the documentation of this file.
1//===- TensorSpec.h - type descriptor for a tensor --------------*- C++ -*-===//
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#ifndef LLVM_ANALYSIS_TENSORSPEC_H
10#define LLVM_ANALYSIS_TENSORSPEC_H
11
12#include "llvm/Config/llvm-config.h"
14
15#include "llvm/ADT/StringMap.h"
16#include "llvm/IR/LLVMContext.h"
17
18#include <memory>
19#include <optional>
20#include <vector>
21
22namespace llvm {
23namespace json {
24class OStream;
25class Value;
26} // namespace json
27
28/// TensorSpec encapsulates the specification of a tensor: its dimensions, or
29/// "shape" (row-major), its type (see TensorSpec::getDataType specializations
30/// for supported types), its name and port (see "TensorFlow: Large-Scale
31/// Machine Learning on Heterogeneous Distributed Systems", section 4.2, para 2:
32/// https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/45166.pdf)
33///
34/// Note that the design is motivated by Tensorflow, but it is not intended to
35/// be Tensorflow-specific.
36///
37/// Known tensor types. The left part is the C type, the
38/// right is a name we can use to identify the type (to implement TensorSpec
39/// equality checks), and to use, if needed, when mapping to an underlying
40/// evaluator's type system. The main requirement is that the C type we use has
41/// the same size and encoding (e.g. endian-ness) as the one used by the
42/// evaluator.
43#define SUPPORTED_TENSOR_TYPES(M) \
44 M(float, Float) \
45 M(double, Double) \
46 M(int8_t, Int8) \
47 M(uint8_t, UInt8) \
48 M(int16_t, Int16) \
49 M(uint16_t, UInt16) \
50 M(int32_t, Int32) \
51 M(uint32_t, UInt32) \
52 M(int64_t, Int64) \
53 M(uint64_t, UInt64)
54
55enum class TensorType {
56 Invalid,
57#define _TENSOR_TYPE_ENUM_MEMBERS(_, Name) Name,
59#undef _TENSOR_TYPE_ENUM_MEMBERS
60 Total
61};
62
63class TensorSpec final {
64public:
65 template <typename T>
66 static TensorSpec createSpec(const std::string &Name,
67 const std::vector<int64_t> &Shape,
68 int Port = 0) {
69 return TensorSpec(Name, Port, getDataType<T>(), sizeof(T), Shape);
70 }
71
72 const std::string &name() const { return Name; }
73 int port() const { return Port; }
74 TensorType type() const { return Type; }
75 const std::vector<int64_t> &shape() const { return Shape; }
76
77 bool operator==(const TensorSpec &Other) const {
78 return Name == Other.Name && Port == Other.Port && Type == Other.Type &&
79 Shape == Other.Shape;
80 }
81
82 bool operator!=(const TensorSpec &Other) const { return !(*this == Other); }
83
84 /// Get the number of elements in a tensor with this shape.
85 size_t getElementCount() const { return ElementCount; }
86 /// Get the size, in bytes, of one element.
87 size_t getElementByteSize() const { return ElementSize; }
88 /// Get the total size of a memory buffer needed to store the whole tensor.
89 size_t getTotalTensorBufferSize() const { return ElementCount * ElementSize; }
90
91 template <typename T> bool isElementType() const {
92 return getDataType<T>() == Type;
93 }
94
95 TensorSpec(const std::string &NewName, const TensorSpec &Other)
96 : TensorSpec(NewName, Other.Port, Other.Type, Other.ElementSize,
97 Other.Shape) {}
98
99 LLVM_ABI void toJSON(json::OStream &OS) const;
100
101private:
102 LLVM_ABI TensorSpec(const std::string &Name, int Port, TensorType Type,
103 size_t ElementSize, const std::vector<int64_t> &Shape);
104
105 template <typename T> static TensorType getDataType();
106
107 std::string Name;
108 int Port = 0;
110 std::vector<int64_t> Shape;
111 size_t ElementCount = 0;
112 size_t ElementSize = 0;
113};
114
115/// For debugging.
116LLVM_ABI std::string tensorValueToString(const char *Buffer,
117 const TensorSpec &Spec);
118
119/// Construct a TensorSpec from a JSON dictionary of the form:
120/// { "name": <string>,
121/// "port": <int>,
122/// "type": <string. Use LLVM's types, e.g. float, double, int64_t>,
123/// "shape": <array of ints> }
124/// For the "type" field, see the C++ primitive types used in
125/// TFUTILS_SUPPORTED_TYPES.
126LLVM_ABI std::optional<TensorSpec>
127getTensorSpecFromJSON(LLVMContext &Ctx, const json::Value &Value);
128
129#define TFUTILS_GETDATATYPE_DEF(T, Name) \
130 template <> LLVM_ABI TensorType TensorSpec::getDataType<T>();
132
133#undef TFUTILS_GETDATATYPE_DEF
134} // namespace llvm
135
136#endif // LLVM_ANALYSIS_TENSORSPEC_H
This file defines the StringMap class.
#define LLVM_ABI
Definition: Compiler.h:213
raw_pwrite_stream & OS
#define TFUTILS_GETDATATYPE_DEF(T, Name)
Definition: TensorSpec.h:129
#define SUPPORTED_TENSOR_TYPES(M)
TensorSpec encapsulates the specification of a tensor: its dimensions, or "shape" (row-major),...
Definition: TensorSpec.h:43
size_t getElementCount() const
Get the number of elements in a tensor with this shape.
Definition: TensorSpec.h:85
const std::string & name() const
Definition: TensorSpec.h:72
TensorType type() const
Definition: TensorSpec.h:74
static TensorSpec createSpec(const std::string &Name, const std::vector< int64_t > &Shape, int Port=0)
Definition: TensorSpec.h:66
const std::vector< int64_t > & shape() const
Definition: TensorSpec.h:75
size_t getElementByteSize() const
Get the size, in bytes, of one element.
Definition: TensorSpec.h:87
bool operator==(const TensorSpec &Other) const
Definition: TensorSpec.h:77
int port() const
Definition: TensorSpec.h:73
bool operator!=(const TensorSpec &Other) const
Definition: TensorSpec.h:82
size_t getTotalTensorBufferSize() const
Get the total size of a memory buffer needed to store the whole tensor.
Definition: TensorSpec.h:89
TensorSpec(const std::string &NewName, const TensorSpec &Other)
Definition: TensorSpec.h:95
LLVM_ABI void toJSON(json::OStream &OS) const
Definition: TensorSpec.cpp:50
bool isElementType() const
Definition: TensorSpec.h:91
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
json::OStream allows writing well-formed JSON without materializing all structures as json::Value ahe...
Definition: JSON.h:996
A Value is an JSON value of unknown type.
Definition: JSON.h:288
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI 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
@ Other
Any other memory.
LLVM_ABI std::string tensorValueToString(const char *Buffer, const TensorSpec &Spec)
For debugging.
Definition: TensorSpec.cpp:107
TensorType
Definition: TensorSpec.h:55
_TENSOR_TYPE_ENUM_MEMBERS(_, Name)