-
Notifications
You must be signed in to change notification settings - Fork 74k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how can i use "TF_SetTarget or TF_SetConfig" to set the number of threads in c code #13853
Comments
Yes, you'll want to say:
|
Is this correct? I just tried this (trying both "intra_op_parallelism_threads: 4" and ""introa_op_parallelism_threads: 4") and neither seemed to work. |
I'm also having trouble with this when trying to set the gpu memory via the C API. When I run the code below I get the following error message: "Unparseable ConfigProto". TF_Status* status = TF_NewStatus(); TF_SetConfig(opts, (void *)str.c_str(),str.size(), status); TF_Code code = TF_GetCode(status); Are there any documentation/examples/comments that show how to set session options via the C API? I've been referencing the c_api_test.cc (link below) which has many examples of how to use the C API but it does not show how to format string for the TF_SetConfig function. https://github.com/tensorflow/tensorflow/blob/r1.4/tensorflow/c/c_api_test.cc |
@bgraf422 I believe >>> import tensorflow as tf
>>> gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)
>>> config = tf.ConfigProto(gpu_options=gpu_options)
>>> serialized = config.SerializeToString()
>>> list(map(hex, serialized))
['0x32', '0x9', '0x9', '0x0', '0x0', '0x0', '0x0', '0x0', '0x0', '0xe0', '0x3f'] And you should use this serialized representation in the C code instead, like: uint8_t config[11] = {0x32, 0x09, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x3f};
TF_SetConfig(opts, (void*)config, 11, status); (Code off the top of my head, may not work but you can get the gist.) Obviously this is not ideal and you should construct/serialize the Config object instead in your C code, but this seems to be the only solution if you want to rely solely on what's exposed by TensorFlow's C API. @drpngx do you have any thoughts on this? This seems like a hacky way to set this. |
Also: I've tried this in an Android NDK app to set the number of threads, and while it makes a warning go away I've observed that this had no effect. For me setting the number of threads to 1 or 8 through the C API had no effect on benchmark time, while setting threads using the equivalent code in C++ did. I'd be interested if this works for you or not. |
Yup that did the trick, thanks for the suggestion! I confirmed that only half of the gpu memory was being used via the nvidia-smi CLI tool. I agree it would be best to construct the config object but I don't see any way of doing that without being dependent on the C++ API. This will work for me for now, thanks for the quick response! |
Ah, I'm guessing you're on Linux then? Maybe I'm just experiencing an Android problem. But yeah this would require some sort of wrapper around config in the C API, I'm not sure how much the TF devs want that though (it would definitely add more to the API surface). |
Yup I'm on Ubuntu 16.04. Makes sense that they don't have all the functionality in the C API since it is mostly meant to be used for building bindings to other languages. I hope they eventually add more features to the C API as it is pretty convenient for deployment. It's a lot easier to just pull down the C API binaries than compiling the source code into a C++ shared library. |
@zo7 I have tried your memory-limitation trick and it works! Thanks! |
c++ code:
python2.7 code:
|
I need. It's a binary proto. You want to use |
no need for hex-encoding, just use |
Hey guys, I'm late to the party but is this written anywhere in the API?
Code in C:
|
A slightly more advanced version of the code by @hoavt-54 ... // reverse engineered the TF_SetConfig protocol from python code like:
// >> config = tf.ConfigProto();config.intra_op_parallelism_threads=7;config.SerializeToString()
// '\x10\x07'
uint8_t intra_op_parallelism_threads = 1;
uint8_t inter_op_parallelism_threads = 1;
uint8_t buf[]={0x10,intra_op_parallelism_threads,0x28,inter_op_parallelism_threads};
TF_SetConfig(sess_opts, buf,sizeof(buf),status); By default, TF assumes it can take over all CPUs. Unfortunately, its threading is nowhere near linear. On a 16 core machine, processing a model single-threaded changed the |
To get Code example https://github.com/apivovarov/TF_C_API/blob/master/config.cc build command:
TF_SetConfig API: |
How can I restrict the tensorflow c api to use only and only one core of the cpu? |
1 similar comment
How can I restrict the tensorflow c api to use only and only one core of the cpu? |
Source code / logs
this is my c code:
this is the debug output:
The text was updated successfully, but these errors were encountered: