MLCompilerBridge
Tools for streamlining communication with ML models for compiler optimizations.
Loading...
Searching...
No Matches
TensorSpec.h
Go to the documentation of this file.
1//===- TensorSpec.h - type descriptor for a tensor --------------*- C++ -*-===//
2//
3// Part of the MLCompilerBridge Project, under the Apache License v2.0 with LLVM
4// Exceptions. See the LICENSE file for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7// (Preliminary version adopted from TensorSpec.h of LLVM 17.X)
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef MLBRIDGE_TENSORSPEC_H
12#define MLBRIDGE_TENSORSPEC_H
13
14#include "llvm/ADT/Optional.h"
15#include "llvm/IR/LLVMContext.h"
16#include "llvm/Support/JSON.h"
17#include <memory>
18#include <numeric>
19#include <vector>
20
21namespace MLBridge {
33#define SUPPORTED_TENSOR_TYPES(M) \
34 M(float, Float) \
35 M(double, Double) \
36 M(int8_t, Int8) \
37 M(uint8_t, UInt8) \
38 M(int16_t, Int16) \
39 M(uint16_t, UInt16) \
40 M(int32_t, Int32) \
41 M(uint32_t, UInt32) \
42 M(int64_t, Int64) \
43 M(uint64_t, UInt64)
44
45enum class TensorType {
46 Invalid,
47#define _TENSOR_TYPE_ENUM_MEMBERS(_, Name) Name,
49#undef _TENSOR_TYPE_ENUM_MEMBERS
50 Total
51};
52
53class TensorSpec final {
54public:
55 template <typename T>
56 static TensorSpec createSpec(const std::string &Name,
57 const std::vector<int64_t> &Shape,
58 int Port = 6020) {
59 return TensorSpec(Name, Port, getDataType<T>(), sizeof(T), Shape);
60 }
61
62 const std::string &name() const { return Name; }
63 int port() const { return Port; }
64 TensorType type() const { return Type; }
65 const std::vector<int64_t> &shape() const { return Shape; }
66 void setShape(const std::vector<int64_t> &NewShape) {
68 ElementCount = std::accumulate(Shape.begin(), Shape.end(), 1,
69 std::multiplies<int64_t>());
70 }
71 bool operator==(const TensorSpec &Other) const {
72 return Name == Other.Name && Port == Other.Port && Type == Other.Type &&
73 Shape == Other.Shape;
74 }
75
76 bool operator!=(const TensorSpec &Other) const { return !(*this == Other); }
77
79 size_t getElementCount() const { return ElementCount; }
81 size_t getElementByteSize() const { return ElementSize; }
84
85 template <typename T> bool isElementType() const {
86 return getDataType<T>() == Type;
87 }
88
92
93 void toJSON(llvm::json::OStream &OS) const;
94
95private:
96 TensorSpec(const std::string &Name, int Port, TensorType Type,
97 size_t ElementSize, const std::vector<int64_t> &Shape);
98
99 template <typename T> static TensorType getDataType();
100
101 std::string Name;
102 int Port = 0;
104 std::vector<int64_t> Shape;
105 size_t ElementCount = 0;
106 size_t ElementSize = 0;
107};
108
110std::string tensorValueToString(const char *Buffer, const TensorSpec &Spec);
111
119llvm::Optional<TensorSpec>
120getTensorSpecFromJSON(llvm::LLVMContext &Ctx, const llvm::json::Value &Value);
121
122#define TFUTILS_GETDATATYPE_DEF(T, Name) \
123 template <> TensorType TensorSpec::getDataType<T>();
125
126#undef TFUTILS_GETDATATYPE_DEF
127} // namespace MLBridge
128
129#endif
#define TFUTILS_GETDATATYPE_DEF(T, Name)
Definition TensorSpec.h:122
#define SUPPORTED_TENSOR_TYPES(M)
TensorSpec encapsulates the specification of a tensor: its dimensions, or "shape" (row-major),...
Definition TensorSpec.h:33
bool isElementType() const
Definition TensorSpec.h:85
void toJSON(llvm::json::OStream &OS) const
TensorType type() const
Definition TensorSpec.h:64
bool operator==(const TensorSpec &Other) const
Definition TensorSpec.h:71
TensorSpec(const std::string &NewName, const TensorSpec &Other)
Definition TensorSpec.h:89
size_t getElementCount() const
Get the number of elements in a tensor with this shape.
Definition TensorSpec.h:79
static TensorSpec createSpec(const std::string &Name, const std::vector< int64_t > &Shape, int Port=6020)
Definition TensorSpec.h:56
size_t getElementByteSize() const
Get the size, in bytes, of one element.
Definition TensorSpec.h:81
size_t getTotalTensorBufferSize() const
Get the total size of a memory buffer needed to store the whole tensor.
Definition TensorSpec.h:83
const std::string & name() const
Definition TensorSpec.h:62
const std::vector< int64_t > & shape() const
Definition TensorSpec.h:65
static TensorType getDataType()
void setShape(const std::vector< int64_t > &NewShape)
Definition TensorSpec.h:66
bool operator!=(const TensorSpec &Other) const
Definition TensorSpec.h:76
std::vector< int64_t > Shape
Definition TensorSpec.h:104
std::string tensorValueToString(const char *Buffer, const TensorSpec &Spec)
For debugging.
llvm::Optional< TensorSpec > getTensorSpecFromJSON(llvm::LLVMContext &Ctx, const llvm::json::Value &Value)
Construct a TensorSpec from a JSON dictionary of the form: { "name": <string>, "port": <int>,...
_TENSOR_TYPE_ENUM_MEMBERS(_, Name)