Skip to content

Commit ec348bb

Browse files
committed
GPU config options in docs
1 parent e75d4ee commit ec348bb

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

docs/source/quickstart.rst

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,79 @@ By default, calling ``model(input)`` will use the input operation named ``servin
125125

126126

127127
For a complete (runnable) example you can check :ref:`this example<MultiInputOutput>`., which uses a toy model with two inputs and two outputs.
128+
129+
GPU Config Options
130+
------------------
131+
132+
You can specify TensorFlow's GPU Options to prevent it from reserving all your GPU memory. To do so in cppflow2, you need to change the global context and set your serialized options:
133+
134+
.. code:: c++
135+
136+
137+
// Serialized config options (example of 30% memory fraction)
138+
// Read more to see how to obtain the serialized options
139+
std::vector<uint8_t> config{0x32,0xb,0x9,0x34,0x33,0x33,0x33,0x33,0x33,0xd3,0x3f,0x20,0x1};
140+
// Create new options with your configuration
141+
TFE_ContextOptions* options = TFE_NewContextOptions();
142+
TFE_ContextOptionsSetConfig(options, config.data(), config.size(), cppflow::context::get_status());
143+
// Replace the global context with your options
144+
cppflow::get_global_context() = cppflow::context(options);
145+
146+
To obtain your desired serialized config options you can just run a small python script to print them:
147+
148+
.. code:: python
149+
150+
import tensorflow.compat.v1 as tf
151+
152+
def create_serialized_options(fraction, growth):
153+
config = tf.ConfigProto()
154+
config.gpu_options.per_process_gpu_memory_fraction = fraction
155+
config.gpu_options.allow_growth = growth
156+
serialized = config.SerializeToString()
157+
return '{' + ','.join(list(map(hex, serialized))) + '}'
158+
159+
# Example with 30% and enable memory growth
160+
# {0x32,0xb,0x9,0x33,0x33,0x33,0x33,0x33,0x33,0xd3,0x3f,0x20,0x1}
161+
print(create_serialized_options(fraction=0.3, growth=True))
162+
163+
Also, for convenience, here is a list with some precomputed serialized options:
164+
165+
+-----------------+---------------+------------------------------------------------------------------+
166+
| Memory Fraction | Memory Growth | Serialized Options |
167+
+=================+===============+==================================================================+
168+
| 10% | True | {0x32,0xb,0x9,0x9a,0x99,0x99,0x99,0x99,0x99,0xb9,0x3f,0x20,0x1} |
169+
+ +---------------+------------------------------------------------------------------+
170+
| | False | {0x32,0x9,0x9,0x9a,0x99,0x99,0x99,0x99,0x99,0xb9,0x3f} |
171+
+-----------------+---------------+------------------------------------------------------------------+
172+
| 20% | True | {0x32,0xb,0x9,0x9a,0x99,0x99,0x99,0x99,0x99,0xc9,0x3f,0x20,0x1} |
173+
+ +---------------+------------------------------------------------------------------+
174+
| | False | {0x32,0x9,0x9,0x9a,0x99,0x99,0x99,0x99,0x99,0xc9,0x3f} |
175+
+-----------------+---------------+------------------------------------------------------------------+
176+
| 30% | True | {0x32,0xb,0x9,0x34,0x33,0x33,0x33,0x33,0x33,0xd3,0x3f,0x20,0x1} |
177+
+ +---------------+------------------------------------------------------------------+
178+
| | False | {0x32,0x9,0x9,0x34,0x33,0x33,0x33,0x33,0x33,0xd3,0x3f} |
179+
+-----------------+---------------+------------------------------------------------------------------+
180+
| 40% | True | {0x32,0xb,0x9,0x9a,0x99,0x99,0x99,0x99,0x99,0xd9,0x3f,0x20,0x1} |
181+
+ +---------------+------------------------------------------------------------------+
182+
| | False | {0x32,0x9,0x9,0x9a,0x99,0x99,0x99,0x99,0x99,0xd9,0x3f} |
183+
+-----------------+---------------+------------------------------------------------------------------+
184+
| 50% | True | {0x32,0xb,0x9,0x0,0x0,0x0,0x0,0x0,0x0,0xe0,0x3f,0x20,0x1} |
185+
+ +---------------+------------------------------------------------------------------+
186+
| | False | {0x32,0x9,0x9,0x0,0x0,0x0,0x0,0x0,0x0,0xe0,0x3f} |
187+
+-----------------+---------------+------------------------------------------------------------------+
188+
| 60% | True | {0x32,0xb,0x9,0x34,0x33,0x33,0x33,0x33,0x33,0xe3,0x3f,0x20,0x1} |
189+
+ +---------------+------------------------------------------------------------------+
190+
| | False | {0x32,0x9,0x9,0x34,0x33,0x33,0x33,0x33,0x33,0xe3,0x3f} |
191+
+-----------------+---------------+------------------------------------------------------------------+
192+
| 70% | True | {0x32,0xb,0x9,0x67,0x66,0x66,0x66,0x66,0x66,0xe6,0x3f,0x20,0x1} |
193+
+ +---------------+------------------------------------------------------------------+
194+
| | False | {0x32,0x9,0x9,0x67,0x66,0x66,0x66,0x66,0x66,0xe6,0x3f} |
195+
+-----------------+---------------+------------------------------------------------------------------+
196+
| 80% | True | {0x32,0xb,0x9,0x9a,0x99,0x99,0x99,0x99,0x99,0xe9,0x3f,0x20,0x1} |
197+
+ +---------------+------------------------------------------------------------------+
198+
| | False | {0x32,0x9,0x9,0x9a,0x99,0x99,0x99,0x99,0x99,0xe9,0x3f} |
199+
+-----------------+---------------+------------------------------------------------------------------+
200+
| 90% | True | {0x32,0xb,0x9,0xcd,0xcc,0xcc,0xcc,0xcc,0xcc,0xec,0x3f,0x20,0x1} |
201+
+ +---------------+------------------------------------------------------------------+
202+
| | False | {0x32,0x9,0x9,0xcd,0xcc,0xcc,0xcc,0xcc,0xcc,0xec,0x3f} |
203+
+-----------------+---------------+------------------------------------------------------------------+

0 commit comments

Comments
 (0)