tf_utils.H
Go to the documentation of this file.
1 /*
2 Licensed under the MIT License <http://opensource.org/licenses/MIT>.
3 SPDX-License-Identifier: MIT
4 Copyright (c) 2018 - 2019 Daniil Goncharov <neargye@gmail.com>.
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 
24 This file is copied from TensorFlowFoam:
25 https://github.com/argonne-lcf/TensorFlowFoam
26 */
27 
28 #pragma once
29 
30 #if defined(_MSC_VER)
31 #if !defined(COMPILER_MSVC)
32 #define COMPILER_MSVC // Set MSVC visibility of exported symbols in the shared library.
33 #endif
34 #pragma warning(push)
35 #pragma warning(disable : 4190)
36 #endif
37 
38 #include <tensorflow/c/c_api.h> // TensorFlow C API header
39 #include <cstddef>
40 #include <cstdint>
41 #include <vector>
42 
43 namespace tf_utils
44 {
45 
46 TF_Graph* LoadGraph(const char* graphPath);
47 
48 void DeleteGraph(TF_Graph* graph);
49 
50 TF_Session* CreateSession(TF_Graph* graph);
51 
52 void DeleteSession(TF_Session* session);
53 
54 TF_Code RunSession(TF_Session* session,
55  const TF_Output* inputs,
56  TF_Tensor* const* input_tensors,
57  std::size_t ninputs,
58  const TF_Output* outputs,
59  TF_Tensor** output_tensors,
60  std::size_t noutputs);
61 
62 TF_Code RunSession(TF_Session* session,
63  const std::vector<TF_Output>& inputs,
64  const std::vector<TF_Tensor*>& input_tensors,
65  const std::vector<TF_Output>& outputs,
66  std::vector<TF_Tensor*>& output_tensors);
67 
68 TF_Tensor* CreateTensor(TF_DataType data_type,
69  const std::int64_t* dims,
70  std::size_t num_dims,
71  const void* data,
72  std::size_t len);
73 
74 template<typename T>
75 TF_Tensor* CreateTensor(TF_DataType data_type, const std::vector<std::int64_t>& dims, const std::vector<T>& data)
76 {
77  return CreateTensor(data_type,
78  dims.data(),
79  dims.size(),
80  data.data(),
81  data.size() * sizeof(T));
82 }
83 
84 TF_Tensor* CreateEmptyTensor(TF_DataType data_type, const std::int64_t* dims, std::size_t num_dims);
85 
86 TF_Tensor* CreateEmptyTensor(TF_DataType data_type, const std::vector<std::int64_t>& dims);
87 
88 void DeleteTensor(TF_Tensor* tensor);
89 
90 void DeleteTensors(const std::vector<TF_Tensor*>& tensors);
91 
92 void SetTensorsData(TF_Tensor* tensor, const void* data, std::size_t len);
93 
94 template<typename T>
95 void SetTensorsData(TF_Tensor* tensor, const std::vector<T>& data)
96 {
97  SetTensorsData(tensor, data.data(), data.size() * sizeof(T));
98 }
99 
100 template<typename T>
101 std::vector<T> GetTensorsData(const TF_Tensor* tensor)
102 {
103  auto data = static_cast<T*>(TF_TensorData(tensor));
104  if (data == nullptr)
105  {
106  return {};
107  }
108 
109  return {data, data + (TF_TensorByteSize(tensor) / TF_DataTypeSize(TF_TensorType(tensor)))};
110 }
111 
112 template<typename T>
113 std::vector<std::vector<T>> GetTensorsData(const std::vector<TF_Tensor*>& tensors)
114 {
115  std::vector<std::vector<T>> data;
116  data.reserve(tensors.size());
117  for (const auto t : tensors)
118  {
119  data.push_back(GetTensorsData<T>(t));
120  }
121 
122  return data;
123 }
124 
125 } // namespace tf_utils
126 
127 #if defined(_MSC_VER)
128 #pragma warning(pop)
129 #endif
tf_utils::DeleteTensors
void DeleteTensors(const std::vector< TF_Tensor * > &tensors)
tf_utils::CreateEmptyTensor
TF_Tensor * CreateEmptyTensor(TF_DataType data_type, const std::int64_t *dims, std::size_t num_dims)
tf_utils::DeleteTensor
void DeleteTensor(TF_Tensor *tensor)
Definition: tf_utils.C:299
tf_utils
Definition: tf_utils.C:36
tf_utils::CreateSession
TF_Session * CreateSession(TF_Graph *graph)
tf_utils::LoadGraph
TF_Graph * LoadGraph(const char *graphPath)
Definition: tf_utils.C:132
T
volScalarField & T
Definition: createRefsHeatTransfer.H:5
tf_utils::SetTensorsData
void SetTensorsData(TF_Tensor *tensor, const void *data, std::size_t len)
tf_utils::RunSession
TF_Code RunSession(TF_Session *session, const TF_Output *inputs, TF_Tensor *const *input_tensors, std::size_t ninputs, const TF_Output *outputs, TF_Tensor **output_tensors, std::size_t noutputs)
tf_utils::GetTensorsData
std::vector< T > GetTensorsData(const TF_Tensor *tensor)
Definition: tf_utils.H:101
tf_utils::CreateTensor
TF_Tensor * CreateTensor(TF_DataType data_type, const std::int64_t *dims, std::size_t num_dims, const void *data, std::size_t len)
Definition: tf_utils.C:255
tf_utils::DeleteGraph
void DeleteGraph(TF_Graph *graph)
tf_utils::DeleteSession
void DeleteSession(TF_Session *session)
Definition: tf_utils.C:171