Skip to content

Commit 444b95f

Browse files
committed
Clarify on conflicting optimizer configurations
1 parent e00711b commit 444b95f

File tree

1 file changed

+79
-73
lines changed

1 file changed

+79
-73
lines changed

rfcs/20201027-modular-tensorflow-graph-c-api.md

+79-73
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ When initializing, TensorFlow loads the plugin and registers a new graph optimiz
4444

4545
### Struct/Function/Object Overview
4646
- Struct
47-
- Struct that should be filled by the plugin: `TP_OptimizerConfigFns`, `TP_Optimizer`, `TP_OptimizerRegistrationParams`
47+
- Struct that should be filled by the plugin: `TP_OptimizerConfigs`, `TP_Optimizer`, `TP_OptimizerRegistrationParams`
4848
- Struct that should be filled by the proper: `TF_GrapplerItem`, `TF_GraphProperties`, `TF_FunctionLibraryDefinition`
4949
- Function
5050
- Function that should be implemented by plugin:
@@ -66,7 +66,7 @@ When initializing, TensorFlow loads the plugin and registers a new graph optimiz
6666
6767
* **Registration**
6868
1. Core TensorFlow links to plugin's dynamic library and loads the function `TF_InitGraphPlugin`.
69-
2. In `TF_InitGraphPlugin`, plugin populates `TP_OptimizerRegistrationParams`, including `TP_OptimizerConfigFns` and `TP_Optimizer`.
69+
2. In `TF_InitGraphPlugin`, plugin populates `TP_OptimizerRegistrationParams`, including `TP_OptimizerConfigs` and `TP_Optimizer`.
7070
7171
* **TF_Buffer and protobuf class**
7272
@@ -140,7 +140,7 @@ This section describes user scenarios for plugin graph optimizer.
140140
141141
### Front-end python use case
142142
143-
Flag `use_plugin_optimizers` is provided for front-end python users to control the plugin graph optimizers.
143+
Flag `use_plugin_optimizers` is provided for front-end python users to turn on/off the plugin graph optimizers.
144144
```python
145145
## TF-1.x
146146
>> from tensorflow.core.protobuf import rewriter_config_pb2
@@ -151,26 +151,28 @@ Flag `use_plugin_optimizers` is provided for front-end python users to control t
151151
```
152152

153153
This API can be used to:
154-
* Turn on/off all registered plugin graph optimizers. By default, the registered optimizers are turned on, users can turn off them. If the registered optimizers are turned on and the graph device type is matched with registered device type, they would be runnning.
154+
* Turn on/off all registered plugin graph optimizers. By default, the registered optimizers are turned on if they are successfully registered, users can turn off them. If the registered optimizers are turned on and the graph device type is matched with registered device type, they would be runnning.
155155
* Use recommended configuration of existing optimizers.
156-
If pluggable graph optimizer is registered to a device type, e.g., GPU, it is optional for plugin authors to provide a recommended configuration indicate whether some of existing optimizers in proper can be turned on/off, by populating flags in `TP_OptimizerRegistrationParams`.
156+
If pluggable graph optimizer is registered to a device type, e.g., GPU, plugin authors can provide a recommended configuration indicate whether some of existing optimizers in proper can be turned on/off, by populating flags in `TP_OptimizerConfigs`. If user turns on the optimizer, the recommended configuration is automatically applied. Otherwise user-set configuration is used.
157157

158158
```cpp
159-
TF_Bool get_remapping() { return false; }
160-
TF_Bool get_auto_mixed_precision() { return true; }
161-
162159
void TF_InitGraphPlugin(TP_OptimizerRegistrationParams* params, TF_Status* status) {
163160
// Plugin authors can turn on/off some optimizers.
164-
params->config_fns->get_remapping = get_remapping;
165-
params->config_fns->get_auto_mixed_precision = get_auto_mixed_precision;
161+
params->configs->remapping = TF_TriState_Off;
162+
params->configs->auto_mixed_precision = TF_TriState_On;
166163
// ...
167164
}
168165
```
169166
170-
If user turns on the optimizer, the recommended configuration is automatically applied. If optimizers are turned off, default configuration is used.
171-
172-
When multiple plugins are registered and running for a graph, an error would be raised if their recommended configurations are different. Users should unload one of the plugins or disable plugin optimizers to resolve the conflict.
167+
When multiple plugins are successfully registered and running for a graph, their recommended configurations may differ with each other. If any of the config has turned off the optimizer, the optimizer will be disabled. The following table lists all possible scenarios. In the table, plugin's config is represented by tristates, `ON` means turn on optimizer, `OFF` means turn off optimzier, and `DEFAULT` means uses proper's config.
173168
169+
| Proper's config | Plugin1's config | Plugin2's config | Final config | Notes|
170+
|:-------------- |:-------------- |:-------------- |:-------------- |:-------------- |
171+
| ON | ON/DEFAULT | ON/DEFAULT | **ON**| The optimizer is enabled. |
172+
| ON | OFF | OFF | **OFF**| The optimizer is disabled, unless users manually unload the plugin. Grappler prints warnings to remind users that config has been changed based on plugin's config.|
173+
| ON | ON/DEFAULT | OFF | **OFF**| The optimizer is disabled if at least one plugin turns off it. Grappler prints warnings to remind users that config has been changed based on plugin's config, and potention performance regression may happen due to the conflict configs.|
174+
| ON | OFF | ON/DEFAULT | **OFF**| Same as previous scenario.|
175+
| OFF | ON/DEFAULT/OFF | ON/DEFAULT/OFF | **OFF**| The optimizer is always disabled when user turns off it. |
174176
175177
### Versioning Strategy and Stability
176178
@@ -191,35 +193,42 @@ This API can be used to:
191193
extern "C" {
192194
#endif
193195
196+
// TF_TriState is the C API typedef for tri-state.
197+
typedef enum TF_TriState {
198+
TF_TriState_Default = 0,
199+
TF_TriState_Off,
200+
TF_TriState_On,
201+
} TF_TriState;
202+
194203
// Flags indicating whether existing optimizers should be turned on/off.
195204
// It's optional for plugin to set functions to return true/false. If not
196-
// set, proper uses default configuration.
197-
typedef struct TP_OptimizerConfigFns {
205+
// set, proper uses configuration set by user.
206+
typedef struct TP_OptimizerConfigs {
198207
size_t struct_size;
199208
void* ext; // reserved for future use
200-
TF_Bool (*get_disable_model_pruning)();
201-
TF_Bool (*get_implementation_selector)();
202-
TF_Bool (*get_function_optimization)();
203-
TF_Bool (*get_common_subgraph_elimination)();
204-
TF_Bool (*get_arithmetic_optimization)();
205-
TF_Bool (*get_debug_stripper)();
206-
TF_Bool (*get_constant_folding)();
207-
TF_Bool (*get_shape_optimization)();
208-
TF_Bool (*get_auto_mixed_precision)();
209-
TF_Bool (*get_auto_mixed_precision_mkl)();
210-
TF_Bool (*get_pin_to_host_optimization)();
211-
TF_Bool (*get_arithmetic_optimization)();
212-
TF_Bool (*get_layout_optimizer)();
213-
TF_Bool (*get_remapping)();
214-
TF_Bool (*get_loop_optimization)();
215-
TF_Bool (*get_dependency_optimization)();
216-
TF_Bool (*get_memory_optimization)();
217-
TF_Bool (*get_auto_parallel)();
218-
TF_Bool (*get_scoped_allocator_optimization)();
219-
} TP_OptimizerConfigFns;
220-
221-
#define TP_OPTIMIZER_CONFIG_FNS_STRUCT_SIZE \
222-
TF_OFFSET_OF_END(TP_OptimizerConfigFns, get_scoped_allocator_optimization)
209+
TF_TriState disable_model_pruning;
210+
TF_TriState implementation_selector;
211+
TF_TriState function_optimization;
212+
TF_TriState common_subgraph_elimination;
213+
TF_TriState arithmetic_optimization;
214+
TF_TriState debug_stripper;
215+
TF_TriState constant_folding;
216+
TF_TriState shape_optimization;
217+
TF_TriState auto_mixed_precision;
218+
TF_TriState auto_mixed_precision_mkl;
219+
TF_TriState pin_to_host_optimization;
220+
TF_TriState arithmetic_optimization;
221+
TF_TriState layout_optimizer;
222+
TF_TriState remapping;
223+
TF_TriState loop_optimization;
224+
TF_TriState dependency_optimization;
225+
TF_TriState memory_optimization;
226+
TF_TriState auto_parallel;
227+
TF_TriState scoped_allocator_optimization;
228+
} TP_OptimizerConfigs;
229+
230+
#define TP_OPTIMIZER_CONFIGS_STRUCT_SIZE \
231+
TF_OFFSET_OF_END(TP_OptimizerConfigs, scoped_allocator_optimization)
223232
224233
// Struct for Optimizer. Plugin authors must provide an optimize function.
225234
// Creation and deletion functions are optional.
@@ -244,7 +253,7 @@ This API can be used to:
244253
245254
// device_type optimizer is registered.
246255
const char* device_type;
247-
TP_OptimizerConfigFns* config_fns;
256+
TP_OptimizerConfigs* configs;
248257
TP_Optimizer* optimizer;
249258
} TP_OptimizerRegistrationParams;
250259
@@ -272,16 +281,17 @@ This API can be used to:
272281
// Get TF_GrapplerItem from TF_Buffer.
273282
TF_GrapplerItem* TF_GetGrapplerItem(TF_Buffer* buffer);
274283

275-
// Get a set of node names that must be preserved. This includes feed and
276-
// fetch nodes, keep_ops, init_ops. Fills in `num_values` and `storage_size`,
277-
// they will be used in `TF_GetNodesToPreserveList`
284+
// Get a set of node names that must be preserved. They can not be transformed
285+
// or removed during the graph transformation. This includes feed and fetch
286+
// nodes, keep_ops, init_ops. Fills in `num_values` and `storage_size`, they
287+
// will be used in `TF_GetNodesToPreserveList`.
278288
void TF_GetNodesToPreserveSize(TF_GrapplerItem* item, int* num_values,
279289
int* storage_size);
280290

281-
// Get a set of node names that must be preserved. This includes feed and
282-
// fetch nodes, keep_ops, init_ops. Fills in
283-
// `values` and `lengths`, each of which must point to an array of length at
284-
// least `num_values`.
291+
// Get a set of node names that must be preserved. They can not be transformed
292+
// or removed during the graph transformation. This includes feed and fetch
293+
// nodes, keep_ops, init_ops. Fills in `values` and `lengths`, each of which
294+
// must point to an array of length at least `num_values`.
285295
//
286296
// The elements of values will point to addresses in `storage` which must be at
287297
// least `storage_size` bytes in length. `num_values` and `storage` can be
@@ -297,9 +307,8 @@ This API can be used to:
297307
void TF_GetFetchNodesSize(TF_GrapplerItem* item, int* num_values, int* storage_size);
298308

299309

300-
// Get a set of node names for fetch nodes. Fills in
301-
// `values` and `lengths`, each of which must point to an array of length at
302-
// least `num_values`.
310+
// Get a set of node names for fetch nodes. Fills in `values` and `lengths`,
311+
// each of which must point to an array of length at least `num_values`.
303312
//
304313
// The elements of values will point to addresses in `storage` which must be at
305314
// least `storage_size` bytes in length. `num_values` and `storage` can be
@@ -321,7 +330,7 @@ This API can be used to:
321330
// Create GraphProperties. The item must outlive the properties.
322331
TF_GraphProperties* TF_NewGraphProperties(TF_GrapplerItem* item);
323332

324-
// Destroy GraphProperties.
333+
// Delete GraphProperties.
325334
void TF_DeleteGraphProperties(TF_GraphProperties* p);
326335

327336
// Infer tensor shapes through abstract interpretation.
@@ -343,21 +352,21 @@ This API can be used to:
343352

344353
// Get the size of input OpInfo::TensorProperties given node name.
345354
void TF_GetInputPropertiesSize(TF_GraphProperties* g_prop, const char* name,
346-
int* max_size);
355+
int* size);
347356

348357
// Get the size of output OpInfo::TensorProperties given node name.
349358
void TF_GetOutputPropertiesSize(TF_GraphProperties* g_prop, const char* name,
350-
int* max_size);
359+
int* size);
351360

352361
// Get a list of input OpInfo::TensorProperties given node name.
353362
// OpInfo::TensorProperties is represented as TF_Buffer*.
354363
void TF_GetInputPropertiesList(TF_GraphProperties* g_prop, const char* name,
355-
TF_Buffer** prop, int max_size);
364+
TF_Buffer** prop, int size);
356365

357366
// Get a list of output OpInfo::TensorProperties given node name.
358367
// OpInfo::TensorProperties is represented as TF_Buffer*.
359368
void TF_GetOutputPropertiesList(TF_GraphProperties* g_prop, const char* name,
360-
TF_Buffer** prop, int max_size);
369+
TF_Buffer** prop, int size);
361370

362371
// Helper to maintain a map between function names in a given
363372
// FunctionDefLibrary and function definitions.
@@ -394,34 +403,34 @@ This API can be used to:
394403
auto* optimizer = new PluginOptimizer;
395404
return (void*)optimizer;
396405
}
397-
static void P_Delete(void* optimizer) {
406+
static void P_Destory(void* optimizer) {
398407
delete static_cast<PluginOptimizer*>(optimizer);
399408
}
400409
static void P_Optimize(
401410
void* optimizer, TF_Buffer* graph_buf,
402411
TF_Buffer* optimized_graph_buf, TF_Status* s) {
403412
// 1. Get TF_GrapplerItem from graph_buf(optional)
404413
TF_GrapplerItem* item = TF_GetGrapplerItem(graph_buf);
405-
414+
406415
// 2. Deserialize graph_buf into plugin::GraphDef
407416
plugin::GraphDef graph_def;
408417
plugin::BufferToMessage(graph_buf, graph_def);
409-
418+
410419
// 3. Infer shapes(optional)
411420
TF_GraphProperties g_prop = TF_NewGraphProperties(item);
412421
TF_InferStatically(g_prop, 0, 0, 0, 0);
413-
int max_size;
414-
TF_GetInputPropertiesSize(g_prop, "node1", &max_size);
415-
std::vector<TF_Buffer*> in_prop_buf(max_size);
416-
for (int i = 0; i < max_size; i++) {
422+
int size;
423+
TF_GetInputPropertiesSize(g_prop, "node1", &size);
424+
std::vector<TF_Buffer*> in_prop_buf(size);
425+
for (int i = 0; i < size; i++) {
417426
in_prop_buf[i] = TF_NewBuffer();
418427
}
419-
TF_GetInputPropertiesList(g_prop, "node1", in_prop_buf.data(), &max_size);
428+
TF_GetInputPropertiesList(g_prop, "node1", in_prop_buf.data(), &size);
420429
plugin::OpInfo::TensorProperties in_prop;
421430
plugin::BufferToMessage(in_prop_buf, in_prop);
422-
for (int i = 0; i < max_size; i++)
431+
for (int i = 0; i < size; i++)
423432
TF_DeleteBuffer(in_prop_buf[i]);
424-
433+
425434
// 4. Get OpDef(optional)
426435
TF_FunctionLibraryDefinition* f_lib = TF_NewFunctionLibraryDefinition(graph_buf);
427436
plugin::OpDef op_def;
@@ -431,9 +440,9 @@ This API can be used to:
431440
plugin::BufferToMessage(op_buf, op_def);
432441
TF_DeleteBuffer(op_buf);
433442
plugin::DataType dt = op_def.input_arg(0).type();
434-
443+
435444
// 5. Transform graph(optional)
436-
445+
437446
// 6. Serialize output plugin::GraphDef into optimized_graph_buf.
438447
plugin::MessageToBuffer(graph_def, optimized_graph_buf);
439448
TF_DeleteGraphProperties(g_prop);
@@ -444,14 +453,11 @@ This API can be used to:
444453
Define `TF_InitGraphPlugin` that TensorFlow will call when registering the plugin:
445454

446455
```cpp
447-
TF_Bool get_remapping() { return false; }
448-
TF_Bool get_auto_mixed_precision() { return true; }
449-
450456
void TF_InitGraphPlugin(TP_OptimizerRegistrationParams* params, TF_Status* status) {
451457
params->device_type = "GPU";
452458
// Define some flags indicating whether existing optimizers should be turned on/off
453-
params->config_fns->get_remapping = get_remapping;
454-
params->config_fns->get_auto_mixed_precision = get_auto_mixed_precision;
459+
params->configs->remapping = TF_TriState_Off;
460+
params->configs->auto_mixed_precision = TF_TriState_On;
455461
// ...
456462

457463
// Set functions to create a new optimizer.
@@ -475,9 +481,9 @@ This API can be used to:
475481
auto init_plugin_fn = reinterpret_cast<TF_InitGraphPlugin>(dso_symbol);
476482
477483
TP_OptimizerRegistrationParams params{TP_OPTIMIZER_REGISTRARION_PARAMS_STRUCT_SIZE};
478-
TP_OptimizerConfigFns config_fns{TP_OPTIMIZER_CONFIG_FNS_STRUCT_SIZE};
484+
TP_OptimizerConfigs configs{TP_OPTIMIZER_CONFIGS_STRUCT_SIZE};
479485
TP_Optimizer optimizer{TP_OPTIMIZER_STRUCT_SIZE};
480-
params->config_fns = &config_fns;
486+
params->configs = &configs;
481487
params->optimizer = &optimizer;
482488
483489
TF_Status* status = TF_NewStatus();

0 commit comments

Comments
 (0)