Decided to have a look at google tensor flow to see what all the hype is about.
Reading the first page of how to install under Linux just blew my mind. Nice work google.
So apparently you are supposed to run this:
1 2 3 4 5 6 |
TF_TYPE="cpu" # Change to "gpu" for GPU support OS="linux" # Change to "darwin" for Mac OS TARGET_DIRECTORY="/usr/local" curl -L \ "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-${TF_TYPE}-${OS}-x86_64-1.4.0.tar.gz" | sudo tar -C $TARGET_DIRECTORY -xz |
Okay that works.
Then:
1 |
sudo ldconfig |
Oh wait linux generally doesn’t include /usr/local/lib by default from any ld.so conf configuration. Well, at least Fedora doesn’t.
Easy fix:
1 |
sudo echo /usr/local/lib >> /etc/ld.so.conf/usr-local.conf |
Then they say to run:
1 2 3 4 |
gcc hello_tf.c /tmp/ccqYuwfC.o: In function `main': hello_tf.c:(.text+0xa): undefined reference to `TF_Version' collect2: error: ld returned 1 exit status |
Hmm okay, that will never work. What magic voodoo will make that function appear for the linker:
Right to add the lib:
1 |
gcc hello_tf.c -ltensorflow |
And then run it like google says:
1 |
a.out |
Hey google guess what, linux doesn’t generally have the current directory in the path. What kind of weird linux are you running?
Maybe try:
1 2 |
./a.out Hello from TensorFlow C library version 1.4.0 |
Wowzer it works.
Google is usually good about their docs. What happened? Too many pythons?
Okay so how to use this thing in C?
Oh wait there aren’t any docs for the c library.
Check the header.. that doesn’t look too promising.
Check github project page. Hmm not much there.
So back to the Getting Started python tutorial. See if it can work in C?
Attempt Digging Through c_api.h
Good news the header has a lot of documentation.
The Computational Graph
Found this:
1 |
typedef struct TF_Graph TF_Graph; |
And This:
1 |
TF_CAPI_EXPORT extern TF_Graph* TF_NewGraph(); |
Must be on to something.
A Tensor
Where?
1 |
typedef struct TF_Tensor TF_Tensor; |
Must be that and:
1 2 3 4 |
TF_CAPI_EXPORT extern TF_Tensor* TF_NewTensor( TF_DataType, const int64_t* dims, int num_dims, void* data, size_t len, void (*deallocator)(void* data, size_t len, void* arg), void* deallocator_arg); |
So how can I get those numbers from the example in there.
Docs:
1 2 3 4 |
// -------------------------------------------------------------------------- // TF_Tensor holds a multi-dimensional array of elements of a single data type. // For all types other than TF_STRING, the data buffer stores elements // in row major order. E.g. if data is treated as a vector of TF_DataType: |
So looking for a float holding tensor.
Have this now so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <stdio.h> #include <tensorflow/c/c_api.h> void tensor_free(void* data, size_t len, void* arg) { printf("Free Called\n"); } int main() { int64_t dims[1] = {1.0f}; int64_t num_dims = 1; float tens_1_data[] = {3.0f}; size_t tens_1_data_len = 1; float tens_2_data[] = {4.0f}; size_t tens_2_data_len = 1; printf("Hello from TensorFlow C library version %s\n", TF_Version()); TF_Tensor * tensor1 = TF_NewTensor(TF_FLOAT, dims, num_dims, tens_1_data, tens_1_data_len, tensor_free, NULL); TF_Tensor * tensor2 = TF_NewTensor(TF_FLOAT, dims, num_dims, tens_2_data, tens_2_data_len, tensor_free, NULL); TF_DeleteTensor(tensor1); TF_DeleteTensor(tensor2); return 0; } |
Quick valgrind check.
Bunch of leaks.. need to shut something down.
Can’t seem to find a shutdown/free/.. anything.. leave that for later.
1 |
valgrind --leak-check=full ./a.out |
Getting Some Output
1 2 3 4 |
Hello from TensorFlow C library version 1.4.0 2017-12-12 17:56:58.424881: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was noTt compiled to use: SSE4.1 Free Called Free Called |
Need A Session
Found this that must be it, but I need a graph and options to make one??
1 2 3 4 |
typedef struct TF_Session TF_Session; TF_CAPI_EXPORT extern TF_Session* TF_NewSession(TF_Graph* graph, const TF_SessionOptions* opts, TF_Status* status); |
Okay says something about NULL being ok.
Try:
1 |
TF_Session * session = TF_NewSession(NULL, NULL, NULL); |
Boom segfault.
So probably the first parameter is the most important.. Try to make a graph.
1 |
TF_Graph * graph = TF_NewGraph(); |
Nope still segfaults.
Try making options.
1 |
TF_SessionOptions * options = TF_NewSessionOptions(); |
Nope still segfaults.
K it’s happy now.. no more crashing.
1 |
TF_Status * status = TF_NewStatus(); |
And clean all those up.
1 2 3 4 5 |
TF_DeleteGraph(graph); TF_DeleteSessionOptions(options); TF_DeleteStatus(status); TF_DeleteTensor(tensor1); TF_DeleteTensor(tensor2); |
Running exiting ok now.
Run The Session?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
TF_CAPI_EXPORT extern void TF_SessionRun( TF_Session* session, // RunOptions const TF_Buffer* run_options, // Input tensors const TF_Output* inputs, TF_Tensor* const* input_values, int ninputs, // Output tensors const TF_Output* outputs, TF_Tensor** output_values, int noutputs, // Target operations const TF_Operation* const* target_opers, int ntargets, // RunMetadata TF_Buffer* run_metadata, // Output status TF_Status*); |
That must be it.
Looks like some TF_Operations and array of TF_Sensors for I/O.
After not finding many examples and many attempts at different guesses and reading the test code in c_api_test.cc and c_test_util.cc I’ve managed to add two numbers together.
The output tensor is allocated by session run so it has been removed.
The final extremely basic C example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <tensorflow/c/c_api.h> /* * Super basic example of using google tensorflow directly from C * */ // Using stack input data nothing to free void tensor_free_none(void * data, size_t len, void* arg) { } TF_Operation * PlaceHolder(TF_Graph * graph, TF_Status * status, TF_DataType dtype, const char * name) { TF_OperationDescription * desc = TF_NewOperation(graph, "Placeholder", name); TF_SetAttrType(desc, "dtype", TF_FLOAT); return TF_FinishOperation(desc, status); } TF_Operation * Const(TF_Graph * graph, TF_Status * status, TF_Tensor * tensor, const char * name) { TF_OperationDescription * desc = TF_NewOperation(graph, "Const", name); TF_SetAttrTensor(desc, "value", tensor, status); TF_SetAttrType(desc, "dtype", TF_TensorType(tensor)); return TF_FinishOperation(desc, status); } TF_Operation * Add(TF_Graph * graph, TF_Status * status, TF_Operation * one, TF_Operation * two, const char * name) { TF_OperationDescription * desc = TF_NewOperation(graph, "AddN", name); TF_Output add_inputs[2] = {{one, 0}, {two, 0}}; TF_AddInputList(desc, add_inputs, 2); return TF_FinishOperation(desc, status); } int main() { printf("TensorFlow C library version: %s\n", TF_Version()); TF_Graph * graph = TF_NewGraph(); TF_SessionOptions * options = TF_NewSessionOptions(); TF_Status * status = TF_NewStatus(); TF_Session * session = TF_NewSession(graph, options, status); float in_val_one = 4.0f; float const_two = 2.0f; TF_Tensor * tensor_in = TF_NewTensor(TF_FLOAT, NULL, 0, &in_val_one, sizeof(float), tensor_free_none, NULL); TF_Tensor * tensor_out = NULL; // easy access after this is allocated by TF_SessionRun TF_Tensor * tensor_const_two = TF_NewTensor(TF_FLOAT, NULL, 0, &const_two, sizeof(float), tensor_free_none, NULL); // Operations TF_Operation * feed = PlaceHolder(graph, status, TF_FLOAT, "feed"); TF_Operation * two = Const(graph, status, tensor_const_two, "const"); TF_Operation * add = Add(graph, status, feed, two, "add"); // Session Inputs TF_Output input_operations[] = { feed, 0 }; TF_Tensor ** input_tensors = {&tensor_in}; // Session Outputs TF_Output output_operations[] = { add, 0 }; TF_Tensor ** output_tensors = {&tensor_out}; TF_SessionRun(session, NULL, // Inputs input_operations, input_tensors, 1, // Outputs output_operations, output_tensors, 1, // Target operations NULL, 0, NULL, status); printf("Session Run Status: %d - %s\n", TF_GetCode(status), TF_Message(status) ); printf("Output Tensor Type: %d\n", TF_TensorType(tensor_out)); float * outval = TF_TensorData(tensor_out); printf("Output Tensor Value: %.2f\n", *outval); TF_CloseSession(session, status); TF_DeleteSession(session, status); TF_DeleteSessionOptions(options); TF_DeleteGraph(graph); TF_DeleteTensor(tensor_in); TF_DeleteTensor(tensor_out); TF_DeleteTensor(tensor_const_two); TF_DeleteStatus(status); return 0; } |
To build and run:
1 2 3 |
gcc -g3 hello_tf.c -ltensorflow -o hello ./hello |
Notes:
The underlying library is written C++ so there is really no point in doing this unless you have some C code that needs to integrate with tensor flow from their.
Valgrind still finds some left over memory from pthread_create. Couldn’t figure away to clean up the lib completely. Doesn’t seem to be any function join the internal threads.
1 2 3 4 5 6 7 8 9 10 11 12 |
==5693== HEAP SUMMARY: ==5693== in use at exit: 5,358,141 bytes in 105,451 blocks ==5693== total heap usage: 310,615 allocs, 205,164 frees, 17,029,114 bytes allocated ==5693== ==5693== 640 bytes in 2 blocks are possibly lost in loss record 63,119 of 63,227 ==5693== at 0x4C2FA50: calloc (vg_replace_malloc.c:711) ==5693== by 0x4013F8A: _dl_allocate_tls (in /usr/lib64/ld-2.24.so) ==5693== by 0x8D3B2DB: pthread_create@@GLIBC_2.2.5 (in /usr/lib64/libpthread-2.24.so) ==5693== by 0x9214C92: std::thread::_M_start_thread(std::shared_ptr<std::thread::_Impl_base>, void (*)()) (in /usr/lib64/libstdc++.so.6.0.22) ==5693== by 0x9214D9C: std::thread::_M_start_thread(std::shared_ptr<std::thread::_Impl_base>) (in /usr/lib64/libstdc++.so.6.0.22) ==5693== by 0x814BFDF: tensorflow::(anonymous namespace)::PosixEnv::StartThread(tensorflow::ThreadOptions const&, std::string const&, std::function<void ()>) (in /usr/local/lib/libtensorflow_framework.so) ==5693== by 0x8124A46: tensorflow::thread::ThreadPool::ThreadPool(ten |