MLCompilerBridge
Tools for streamlining communication with ML models for compiler optimizations.
Loading...
Searching...
No Matches
bitstreamSerDes.cpp
Go to the documentation of this file.
1//===- bitstreamSerDes.cpp - Serializer for Bitstream -----------*- 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//===----------------------------------------------------------------------===//
13//===----------------------------------------------------------------------===//
14
17#include "llvm/Support/JSON.h"
18
19#include <cstddef>
20#include <cstdint>
21#include <cstdlib>
22#include <optional>
23#include <string>
24#include <vector>
25
26#define DEBUG_TYPE "bitstream-serdes"
27
28namespace MLBridge {
29void BitstreamSerDes::setFeature(const std::string &name, const int value) {
30 auto *valuePtr = new int(value);
31 featuresint[name] = valuePtr;
32 tensorSpecs.push_back(TensorSpec::createSpec<int>(name, {1}));
33 rawData.push_back(valuePtr);
34}
35
36void BitstreamSerDes::setFeature(const std::string &name, const long value) {
37 auto *valuePtr = new long(value);
38 featureslong[name] = valuePtr;
39 tensorSpecs.push_back(TensorSpec::createSpec<long>(name, {1}));
40 rawData.push_back(valuePtr);
41}
42
43void BitstreamSerDes::setFeature(const std::string &name, const float value) {
44 auto *valuePtr = new float(value);
45 featuresfloat[name] = valuePtr;
46 tensorSpecs.push_back(TensorSpec::createSpec<float>(name, {1}));
47 rawData.push_back(valuePtr);
48}
49
50void BitstreamSerDes::setFeature(const std::string &name, const double value) {
51 auto *valuePtr = new double(value);
52 featuresdouble[name] = valuePtr;
53 tensorSpecs.push_back(TensorSpec::createSpec<double>(name, {1}));
54 rawData.push_back(valuePtr);
55}
56
57void BitstreamSerDes::setFeature(const std::string &name,
58 const std::string value) {
59 auto *valuePtr = new std::string(value);
60 featuresstring[name] = valuePtr;
61 long size = value.length();
62 tensorSpecs.push_back(TensorSpec::createSpec<uint8_t>(name, {size}));
63 rawData.push_back((void *)valuePtr->c_str());
64}
65
66void BitstreamSerDes::setFeature(const std::string &name, const bool value) {
67 auto *valuePtr = new bool(value);
68 featuresbool[name] = valuePtr;
69 tensorSpecs.push_back(TensorSpec::createSpec<uint8_t>(name, {1}));
70 rawData.push_back(valuePtr);
71}
72
73void BitstreamSerDes::setFeature(const std::string &name,
74 const std::vector<int> &value) {
75 auto *valuePtr = new std::vector<int>(value);
76 featuresVectorint[name] = valuePtr;
77 tensorSpecs.push_back(
78 TensorSpec::createSpec<int>(name, {static_cast<long>(valuePtr->size())}));
79 rawData.push_back(valuePtr->data());
80}
81
82void BitstreamSerDes::setFeature(const std::string &name,
83 const std::vector<long> &value) {
84 auto *valuePtr = new std::vector<long>(value);
85 featuresVectorlong[name] = valuePtr;
87 name, {static_cast<long>(valuePtr->size())}));
88 rawData.push_back(valuePtr->data());
89}
90
91void BitstreamSerDes::setFeature(const std::string &name,
92 const std::vector<float> &value) {
93 auto *valuePtr = new std::vector<float>(value);
94 featuresVectorfloat[name] = valuePtr;
96 name, {static_cast<long>(valuePtr->size())}));
97 rawData.push_back(valuePtr->data());
98}
99
100void BitstreamSerDes::setFeature(const std::string &name,
101 const std::vector<double> &value) {
102 auto *valuePtr = new std::vector<double>(value);
103 featuresVectordouble[name] = valuePtr;
105 name, {static_cast<long>(valuePtr->size())}));
106 rawData.push_back(valuePtr->data());
107}
108
109void BitstreamSerDes::setFeature(const std::string &name,
110 const std::vector<std::string> &value) {
111 llvm_unreachable("Currently std::vector<std::string> not supported");
112}
113
114void BitstreamSerDes::setFeature(const std::string &name,
115 const std::vector<bool> &value) {
116 llvm_unreachable("Currently std::vector<bool> not supported");
117}
118
120 std::unique_ptr<llvm::raw_ostream> OS =
121 std::make_unique<llvm::raw_string_ostream>(Buffer);
122 llvm::json::OStream J(*OS);
123 J.object([&]() {
124 J.attributeArray("features", [&]() {
125 for (size_t I = 0; I < tensorSpecs.size(); ++I) {
126 tensorSpecs[I].toJSON(J);
127 }
128 });
129 });
130 J.flush();
131 OS->write("\n", 1);
132 MLBRIDGE_DEBUG(std::cout << "rawData.size(): " << rawData.size() << "\n");
133 for (size_t I = 0; I < rawData.size(); ++I) {
134 OS->write(reinterpret_cast<const char *>(rawData[I]),
135 tensorSpecs[I].getTotalTensorBufferSize());
136 }
137 OS->write("\n", 1);
138 OS->flush();
139 auto *out = new std::string(Buffer);
141 return out;
142}
143
145 // set the message length
146 auto *res = reinterpret_cast<std::string *>(data);
147 this->MessageLength = res->size();
148 return res->data();
149}
150} // namespace MLBridge
This file defines the debug macros for the MLCompilerBridge.
#define MLBRIDGE_DEBUG(X)
Definition Debug.h:25
Bitstream Serialization/Deserialization which sends header information followed by the raw data.
virtual void setFeature(const std::string &name, const google::protobuf::Message *value)
Definition baseSerDes.h:60
void * getSerializedData() override
void * deserializeUntyped(void *) override
std::vector< TensorSpec > tensorSpecs
void cleanDataStructures() override
std::vector< const void * > rawData
static TensorType getDataType()