Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 9f30f3b

Browse files
committed
Tensorflow Session Entity Wrapper + Minor Tests
1 parent e20d519 commit 9f30f3b

File tree

11 files changed

+628
-3
lines changed

11 files changed

+628
-3
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// tf_session_entity.cpp
6+
//
7+
// Identification: src/brain/tf_session_entity/tf_session_entity.cpp
8+
//
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "brain/tf_session_entity/tf_session_entity.h"
14+
#include <include/common/logger.h>
15+
#include <include/common/macros.h>
16+
17+
namespace peloton {
18+
namespace brain {
19+
20+
/**
21+
* Constructor/Desctructor
22+
**/
23+
24+
TFSE_TEMPLATE_ARGUMENTS
25+
TFSE_TYPE::TfSessionEntity() {
26+
graph_ = TF_NewGraph();
27+
status_ = TF_NewStatus();
28+
session_options_ = TF_NewSessionOptions();
29+
session_ = TF_NewSession(graph_, session_options_, status_);
30+
}
31+
32+
TFSE_TEMPLATE_ARGUMENTS
33+
TFSE_TYPE::~TfSessionEntity() {
34+
TF_DeleteStatus(status_);
35+
TF_DeleteGraph(graph_);
36+
}
37+
38+
/*
39+
********
40+
* Graph Import Utilities
41+
********
42+
*/
43+
44+
TFSE_TEMPLATE_ARGUMENTS
45+
void TFSE_TYPE::FreeBuffer(void *data, UNUSED_ATTRIBUTE size_t length) {
46+
free(data);
47+
}
48+
49+
TFSE_TEMPLATE_ARGUMENTS
50+
void TFSE_TYPE::ImportGraph(const std::string &filename) {
51+
TF_Buffer *graph_def = ReadFile(filename);
52+
TF_ImportGraphDefOptions *opts = TF_NewImportGraphDefOptions();
53+
TF_GraphImportGraphDef(graph_, graph_def, opts, status_);
54+
TF_DeleteImportGraphDefOptions(opts);
55+
TF_DeleteBuffer(graph_def);
56+
assert(IsStatusOk());
57+
}
58+
59+
TFSE_TEMPLATE_ARGUMENTS
60+
TF_Buffer *TFSE_TYPE::ReadFile(const std::string &filename) {
61+
FILE *f = fopen(filename.c_str(), "rb");
62+
fseek(f, 0, SEEK_END);
63+
size_t fsize = (size_t)ftell(f);
64+
fseek(f, 0, SEEK_SET); // same as rewind(f);
65+
66+
void *data = malloc(fsize);
67+
fread(data, fsize, 1, f);
68+
fclose(f);
69+
70+
TF_Buffer *buf = TF_NewBuffer();
71+
buf->data = data;
72+
buf->length = fsize;
73+
buf->data_deallocator = TfSessionEntity::FreeBuffer;
74+
return buf;
75+
}
76+
77+
/*
78+
********
79+
* Evaluation/Session.Run()
80+
********
81+
*/
82+
83+
// Evaluate op with no inputs/outputs
84+
TFSE_TEMPLATE_ARGUMENTS
85+
void TFSE_TYPE::Eval(const std::string &opName) {
86+
TF_Operation *op = TF_GraphOperationByName(graph_, opName.c_str());
87+
TF_SessionRun(session_, nullptr, nullptr, nullptr, 0, // inputs
88+
nullptr, nullptr, 0, // outputs
89+
&op, 1, // targets
90+
nullptr, status_);
91+
}
92+
93+
// Evaluate op with inputs and outputs
94+
TFSE_TEMPLATE_ARGUMENTS
95+
OutputType *TFSE_TYPE::Eval(
96+
std::vector<TfSessionEntityInput<InputType>> helper_inputs,
97+
std::vector<TfSessionEntityOutput<OutputType>> helper_outputs) {
98+
std::vector<TF_Tensor *> in_vals, out_vals;
99+
std::vector<TF_Output> ins, outs;
100+
for (auto helperIn : helper_inputs) {
101+
ins.push_back(
102+
{TF_GraphOperationByName(graph_, helperIn.GetPlaceholderName().c_str()),
103+
0});
104+
in_vals.push_back(helperIn.GetTensor());
105+
}
106+
for (auto helperOut : helper_outputs) {
107+
outs.push_back({TF_GraphOperationByName(
108+
graph_, helperOut.GetPlaceholderName().c_str()),
109+
0});
110+
out_vals.push_back(helperOut.GetTensor());
111+
}
112+
TF_SessionRun(session_, nullptr, &(ins.at(0)), &(in_vals.at(0)),
113+
ins.size(), // Inputs
114+
&(outs.at(0)), &(out_vals.at(0)), outs.size(), // Outputs
115+
nullptr, 0, // Operations
116+
nullptr, status_);
117+
assert(TF_GetCode(status_) == TF_OK);
118+
return static_cast<OutputType *>(TF_TensorData(out_vals.at(0)));
119+
}
120+
121+
// Evaluate op with only inputs(where nothing is output eg. Backprop)
122+
TFSE_TEMPLATE_ARGUMENTS
123+
void TFSE_TYPE::Eval(std::vector<TfSessionEntityInput<InputType>> helper_inputs,
124+
const std::string &op_name) {
125+
std::vector<TF_Tensor *> in_vals;
126+
std::vector<TF_Output> ins;
127+
for (auto helperIn : helper_inputs) {
128+
ins.push_back(
129+
{TF_GraphOperationByName(graph_, helperIn.GetPlaceholderName().c_str()),
130+
0});
131+
in_vals.push_back(helperIn.GetTensor());
132+
}
133+
TF_Operation *op = TF_GraphOperationByName(graph_, op_name.c_str());
134+
TF_SessionRun(session_, nullptr, &(ins.at(0)), &(in_vals.at(0)),
135+
ins.size(), // Inputs
136+
nullptr, nullptr, 0, // Outputs
137+
&op, 1, // Operations
138+
nullptr, status_);
139+
assert(TF_GetCode(status_) == TF_OK);
140+
}
141+
142+
/*
143+
********
144+
* Helper Operations
145+
********
146+
*/
147+
148+
TFSE_TEMPLATE_ARGUMENTS
149+
void TFSE_TYPE::PrintOperations() {
150+
TF_Operation *oper;
151+
size_t pos = 0;
152+
std::string graph_ops = "Graph Operations List:";
153+
while ((oper = TF_GraphNextOperation(graph_, &pos)) != nullptr) {
154+
graph_ops += TF_OperationName(oper);
155+
graph_ops += "\n";
156+
}
157+
LOG_DEBUG("%s", graph_ops.c_str());
158+
}
159+
160+
TFSE_TEMPLATE_ARGUMENTS
161+
bool TFSE_TYPE::IsStatusOk() { return TF_GetCode(status_) == TF_OK; }
162+
163+
// Explicit template Initialization
164+
template class TfSessionEntity<float, float>;
165+
} // namespace brain
166+
} // namespace peloton
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// tf_session_entity_input.cpp
6+
//
7+
// Identification: src/brain/tf_session_entity/tf_session_entity_input.cpp
8+
//
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "brain/tf_session_entity/tf_session_entity_input.h"
14+
#include <include/common/macros.h>
15+
16+
namespace peloton {
17+
namespace brain {
18+
TFSEIN_TEMPLATE_ARGUMENTS
19+
TFSEIN_TYPE::TfSessionEntityInput(InputType input, const std::string &op) {
20+
this->placeholder_name_ = op;
21+
this->DetermineDataType();
22+
InputType input_for_tf = input;
23+
this->tensor_ =
24+
TF_AllocateTensor(this->data_type_, nullptr, 0, sizeof(InputType));
25+
auto buff = (InputType *)TF_TensorData(this->tensor_);
26+
PL_MEMCPY(buff, &input_for_tf, sizeof(InputType));
27+
}
28+
29+
// 1d vector
30+
TFSEIN_TEMPLATE_ARGUMENTS
31+
TFSEIN_TYPE::TfSessionEntityInput(std::vector<InputType> input,
32+
const std::string &op) {
33+
this->placeholder_name_ = op;
34+
this->DetermineDataType();
35+
int64_t dims[] = {static_cast<int64_t>(input.size())};
36+
InputType *input_for_tf = input.data();
37+
this->tensor_ =
38+
TF_AllocateTensor(this->data_type_, dims, 1, dims[0] * sizeof(InputType));
39+
auto buff = (InputType *)TF_TensorData(this->tensor_);
40+
PL_MEMCPY(buff, input_for_tf, dims[0] * sizeof(InputType));
41+
}
42+
43+
// 2d vector
44+
TFSEIN_TEMPLATE_ARGUMENTS
45+
TFSEIN_TYPE::TfSessionEntityInput(std::vector<std::vector<InputType>> input,
46+
const std::string &op) {
47+
this->placeholder_name_ = op;
48+
this->DetermineDataType();
49+
int64_t dims[] = {static_cast<int64_t>(input.size()),
50+
static_cast<int64_t>(input[0].size())};
51+
InputType *input_for_tf = Flatten(input);
52+
this->tensor_ = TF_AllocateTensor(this->data_type_, dims, 2,
53+
dims[0] * dims[1] * sizeof(InputType));
54+
auto buff = (InputType *)TF_TensorData(this->tensor_);
55+
PL_MEMCPY(buff, input_for_tf, dims[0] * dims[1] * sizeof(InputType));
56+
}
57+
58+
// raw flattened input
59+
TFSEIN_TEMPLATE_ARGUMENTS
60+
TFSEIN_TYPE::TfSessionEntityInput(InputType *input, std::vector<int64_t> dims,
61+
const std::string &op) {
62+
this->placeholder_name_ = op;
63+
this->DetermineDataType();
64+
InputType *input_for_tf = input;
65+
int64_t num_elems = 1;
66+
for (auto elem : dims) {
67+
num_elems *= elem;
68+
}
69+
this->tensor_ = TF_AllocateTensor(this->data_type_, dims.data(), dims.size(),
70+
num_elems * sizeof(InputType));
71+
auto buff = (InputType *)TF_TensorData(this->tensor_);
72+
PL_MEMCPY(buff, input_for_tf, num_elems * sizeof(InputType));
73+
}
74+
75+
// Flattens 2d inputs
76+
TFSEIN_TEMPLATE_ARGUMENTS
77+
InputType *TFSEIN_TYPE::Flatten(std::vector<std::vector<InputType>> elems) {
78+
std::vector<InputType> flattened;
79+
for (auto row : elems) {
80+
for (float elem : row) {
81+
flattened.push_back(elem);
82+
}
83+
}
84+
return flattened.data();
85+
}
86+
87+
// Explicit template Initialization
88+
template class TfSessionEntityInput<float>;
89+
90+
} // namespace brain
91+
} // namespace peloton
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// tf_session_entity_io.cpp
6+
//
7+
// Identification: src/brain/tf_session_entity/tf_session_entity_io.cpp
8+
//
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "brain/tf_session_entity/tf_session_entity_io.h"
14+
15+
namespace peloton {
16+
namespace brain {
17+
18+
TFSEIO_BASE_TEMPLATE_ARGUMENTS
19+
void TFSEIO_BASE_TYPE::DetermineDataType() {
20+
if (std::is_same<N, int64_t>::value) {
21+
data_type_ = TF_INT64;
22+
} else if (std::is_same<N, int32_t>::value) {
23+
data_type_ = TF_INT32;
24+
} else if (std::is_same<N, int16_t>::value) {
25+
data_type_ = TF_INT16;
26+
} else if (std::is_same<N, int8_t>::value) {
27+
data_type_ = TF_INT8;
28+
} else if (std::is_same<N, int>::value) {
29+
data_type_ = TF_INT32;
30+
} else if (std::is_same<N, float>::value) {
31+
data_type_ = TF_FLOAT;
32+
} else if (std::is_same<N, double>::value) {
33+
data_type_ = TF_DOUBLE;
34+
}
35+
}
36+
37+
TFSEIO_BASE_TEMPLATE_ARGUMENTS
38+
std::string TFSEIO_BASE_TYPE::GetPlaceholderName() { return placeholder_name_; }
39+
40+
TFSEIO_BASE_TEMPLATE_ARGUMENTS
41+
TF_Tensor *TFSEIO_BASE_TYPE::GetTensor() { return tensor_; }
42+
43+
// Explicit template Initialization
44+
template class TfSessionEntityIOBase<float>;
45+
46+
} // namespace brain
47+
} // namespace peloton
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// tf_session_entity_output.cpp
6+
//
7+
// Identification: src/brain/tf_session_entity/tf_session_entity_output.cpp
8+
//
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "brain/tf_session_entity/tf_session_entity_output.h"
14+
15+
namespace peloton {
16+
namespace brain {
17+
18+
TFSEOUT_TEMPLATE_ARGUMENTS
19+
TFSEOUT_TYPE::TfSessionEntityOutput(const std::string &op) {
20+
this->placeholder_name_ = op;
21+
this->DetermineDataType();
22+
this->tensor_ =
23+
TF_AllocateTensor(this->data_type_, nullptr, 0, sizeof(OutputType));
24+
}
25+
26+
TFSEOUT_TEMPLATE_ARGUMENTS
27+
TFSEOUT_TYPE::TfSessionEntityOutput(std::vector<int64_t> dims,
28+
const std::string &op) {
29+
this->placeholder_name_ = op;
30+
this->DetermineDataType();
31+
int64_t num_elems = 1;
32+
for (auto elem : dims) {
33+
num_elems *= elem;
34+
}
35+
this->tensor_ = TF_AllocateTensor(this->data_type_, dims.data(), dims.size(),
36+
sizeof(OutputType) * num_elems);
37+
}
38+
39+
// Explicit template Initialization
40+
template class TfSessionEntityOutput<float>;
41+
} // namespace brain
42+
} // namespace peloton

0 commit comments

Comments
 (0)