18#include "llvm/ADT/None.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/Support/Debug.h"
21#include "llvm/Support/JSON.h"
31#define TFUTILS_GETDATATYPE_IMPL(T, E) \
32 template <> TensorType TensorSpec::getDataType<T>() { return TensorType::E; }
36#undef TFUTILS_GETDATATYPE_IMPL
39 TensorTypeNames{
"INVALID",
40#define TFUTILS_GETNAME_IMPL(T, _) #T,
42#undef TFUTILS_GETNAME_IMPL
46 return TensorTypeNames[
static_cast<size_t>(TT)];
51 OS.attribute(
"name",
name());
53 OS.attribute(
"port",
port());
54 OS.attributeArray(
"shape", [&]() {
62 size_t ElementSize,
const std::vector<int64_t> &Shape)
63 : Name(Name), Port(Port), Type(Type), Shape(Shape),
64 ElementCount(std::accumulate(Shape.begin(), Shape.end(), 1,
65 std::multiplies<int64_t>())),
66 ElementSize(ElementSize) {}
69 const json::Value &Value) {
71 [&](
const llvm::Twine &Message) -> llvm::Optional<TensorSpec> {
73 llvm::raw_string_ostream OS(S);
75 Ctx.emitError(
"Unable to parse JSON Value as spec (" + Message +
"): " + S);
80 json::Path::Root Root(
"tensor_spec");
81 json::ObjectMapper Mapper(Value, Root);
83 json::ObjectMapper Mapper(Value);
86 return EmitError(
"Value is not a dict");
88 std::string TensorName;
91 std::vector<int64_t> TensorShape;
93 if (!Mapper.map<std::string>(
"name", TensorName))
94 return EmitError(
"'name' property not present or not a string");
95 if (!Mapper.map<std::string>(
"type",
TensorType))
96 return EmitError(
"'type' property not present or not a string");
97 if (!Mapper.map<
int>(
"port", TensorPort))
98 return EmitError(
"'port' property not present or not an int");
99 if (!Mapper.map<std::vector<int64_t>>(
"shape", TensorShape))
100 return EmitError(
"'shape' property not present or not an int array");
102#define PARSE_TYPE(T, E) \
103 if (TensorType == #T) \
104 return TensorSpec::createSpec<T>(TensorName, TensorShape, TensorPort);
111 switch (Spec.
type()) {
112#define _IMR_DBG_PRINTER(T, N) \
113 case TensorType::N: { \
114 const T *TypedBuff = reinterpret_cast<const T *>(Buffer); \
115 auto R = llvm::make_range(TypedBuff, TypedBuff + Spec.getElementCount()); \
117 llvm::map_range(R, [](T V) { return std::to_string(V); }), ","); \
120#undef _IMR_DBG_PRINTER
123 llvm_unreachable(
"invalid tensor type");
#define TFUTILS_GETDATATYPE_IMPL(T, E)
#define TFUTILS_GETNAME_IMPL(T, _)
#define _IMR_DBG_PRINTER(T, N)
#define SUPPORTED_TENSOR_TYPES(M)
TensorSpec encapsulates the specification of a tensor: its dimensions, or "shape" (row-major),...
void toJSON(llvm::json::OStream &OS) const
TensorSpec(const std::string &NewName, const TensorSpec &Other)
const std::string & name() const
const std::vector< int64_t > & shape() const
static TensorType getDataType()
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>,...
StringRef toString(TensorType TT)