|
| 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 "brain/tf_session_entity/tf_session_entity_input.h" |
| 15 | +#include "brain/tf_session_entity/tf_session_entity_output.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 | + ::operator delete(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 | + PL_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 | + // Reference: |
| 66 | + // https://stackoverflow.com/questions/14111900/using-new-on-void-pointer |
| 67 | + void *data = ::operator new(fsize); |
| 68 | + UNUSED_ATTRIBUTE size_t size_read = fread(data, fsize, 1, f); |
| 69 | + fclose(f); |
| 70 | + |
| 71 | + TF_Buffer *buf = TF_NewBuffer(); |
| 72 | + buf->data = data; |
| 73 | + buf->length = fsize; |
| 74 | + buf->data_deallocator = TfSessionEntity::FreeBuffer; |
| 75 | + return buf; |
| 76 | +} |
| 77 | + |
| 78 | +/* |
| 79 | + ******** |
| 80 | + * Evaluation/Session.Run() |
| 81 | + ******** |
| 82 | + */ |
| 83 | + |
| 84 | +// Evaluate op with no inputs/outputs |
| 85 | +TFSE_TEMPLATE_ARGUMENTS |
| 86 | +void TFSE_TYPE::Eval(const std::string &opName) { |
| 87 | + TF_Operation *op = TF_GraphOperationByName(graph_, opName.c_str()); |
| 88 | + TF_SessionRun(session_, nullptr, nullptr, nullptr, 0, // inputs |
| 89 | + nullptr, nullptr, 0, // outputs |
| 90 | + &op, 1, // targets |
| 91 | + nullptr, status_); |
| 92 | +} |
| 93 | + |
| 94 | +// Evaluate op with inputs and outputs |
| 95 | +TFSE_TEMPLATE_ARGUMENTS |
| 96 | +OutputType *TFSE_TYPE::Eval( |
| 97 | + const std::vector<TfSessionEntityInput<InputType>>& helper_inputs, |
| 98 | + const std::vector<TfSessionEntityOutput<OutputType>>& helper_outputs) { |
| 99 | + std::vector<TF_Tensor *> in_vals, out_vals; |
| 100 | + std::vector<TF_Output> ins, outs; |
| 101 | + for (auto helperIn : helper_inputs) { |
| 102 | + ins.push_back( |
| 103 | + {TF_GraphOperationByName(graph_, helperIn.GetPlaceholderName().c_str()), |
| 104 | + 0}); |
| 105 | + in_vals.push_back(helperIn.GetTensor()); |
| 106 | + } |
| 107 | + for (auto helperOut : helper_outputs) { |
| 108 | + outs.push_back({TF_GraphOperationByName( |
| 109 | + graph_, helperOut.GetPlaceholderName().c_str()), |
| 110 | + 0}); |
| 111 | + out_vals.push_back(helperOut.GetTensor()); |
| 112 | + } |
| 113 | + TF_SessionRun(session_, nullptr, &(ins.at(0)), &(in_vals.at(0)), |
| 114 | + ins.size(), // Inputs |
| 115 | + &(outs.at(0)), &(out_vals.at(0)), outs.size(), // Outputs |
| 116 | + nullptr, 0, // Operations |
| 117 | + nullptr, status_); |
| 118 | + PL_ASSERT(TF_GetCode(status_) == TF_OK); |
| 119 | + return static_cast<OutputType *>(TF_TensorData(out_vals.at(0))); |
| 120 | +} |
| 121 | + |
| 122 | +// Evaluate op with only inputs(where nothing is output eg. Backprop) |
| 123 | +TFSE_TEMPLATE_ARGUMENTS |
| 124 | +void TFSE_TYPE::Eval(const std::vector<TfSessionEntityInput<InputType>>& helper_inputs, |
| 125 | + const std::string &op_name) { |
| 126 | + std::vector<TF_Tensor *> in_vals; |
| 127 | + std::vector<TF_Output> ins; |
| 128 | + for (auto helperIn : helper_inputs) { |
| 129 | + ins.push_back( |
| 130 | + {TF_GraphOperationByName(graph_, helperIn.GetPlaceholderName().c_str()), |
| 131 | + 0}); |
| 132 | + in_vals.push_back(helperIn.GetTensor()); |
| 133 | + } |
| 134 | + TF_Operation *op = TF_GraphOperationByName(graph_, op_name.c_str()); |
| 135 | + TF_SessionRun(session_, nullptr, &(ins.at(0)), &(in_vals.at(0)), |
| 136 | + ins.size(), // Inputs |
| 137 | + nullptr, nullptr, 0, // Outputs |
| 138 | + &op, 1, // Operations |
| 139 | + nullptr, status_); |
| 140 | + PL_ASSERT(TF_GetCode(status_) == TF_OK); |
| 141 | +} |
| 142 | + |
| 143 | +/* |
| 144 | + ******** |
| 145 | + * Helper Operations |
| 146 | + ******** |
| 147 | + */ |
| 148 | + |
| 149 | +TFSE_TEMPLATE_ARGUMENTS |
| 150 | +void TFSE_TYPE::PrintOperations() { |
| 151 | + TF_Operation *oper; |
| 152 | + size_t pos = 0; |
| 153 | + std::string graph_ops = "Graph Operations List:"; |
| 154 | + while ((oper = TF_GraphNextOperation(graph_, &pos)) != nullptr) { |
| 155 | + graph_ops += TF_OperationName(oper); |
| 156 | + graph_ops += "\n"; |
| 157 | + } |
| 158 | + LOG_DEBUG("%s", graph_ops.c_str()); |
| 159 | +} |
| 160 | + |
| 161 | +TFSE_TEMPLATE_ARGUMENTS |
| 162 | +bool TFSE_TYPE::IsStatusOk() { return TF_GetCode(status_) == TF_OK; } |
| 163 | + |
| 164 | +// Explicit template Initialization |
| 165 | +template class TfSessionEntity<float, float>; |
| 166 | +} // namespace brain |
| 167 | +} // namespace peloton |
0 commit comments