Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.

Commit ee01d15

Browse files
committed
Changing struct name with TP_ prefix.
Making TP_OptimizerBuilder as a struct.
1 parent 639e1dc commit ee01d15

File tree

1 file changed

+47
-39
lines changed

1 file changed

+47
-39
lines changed

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

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ When initializing, TensorFlow loads the plugin and registers a new graph optimiz
4848

4949
### Struct/Function/Object Overview
5050
- Struct
51-
- Struct that should be filled by the plugin: `P_OptimizerConfigFns`, `P_RegistrationParams`, `TF_OptimizerBuilder`
51+
- Struct that should be filled by the plugin: `TP_OptimizerConfigFns`, `TP_RegistrationParams`, `TP_OptimizerBuilder`
5252
- Struct that should be filled by the proper: `TF_GrapplerItem`, `TF_GraphProperties`, `TF_FunctionLibraryDefinition`
5353
- Function
5454
- Function that should be implemented by plugin:
5555
- Creation/Deletion/Optimization function of optimizer.
56-
- `P_MessageToBuffer`, `P_BufferToMessage`: Serialization/Deserialization of objects generated by protobuf.
56+
- `plugin::MessageToBuffer`, `plugin::BufferToMessage`: Serialization/Deserialization of objects generated by protobuf.
5757
- `TF_InitGraphPlugin`: Optimizer registration.
5858
- (Optional)Util function for graph transformation.
5959
- Object:
@@ -88,18 +88,18 @@ When initializing, TensorFlow loads the plugin and registers a new graph optimiz
8888
8989
After optimizing, plugin authors need to serialize the optimized `GraphDef` object into `TF_Buffer` as output.
9090
91-
Serialization function `MessageToBuffer` is already defined, desearization function `BufferToMessage` will be added in proper to convert plugin passed `TF_Buffer` back to proper's `GraphDef`. In plugin side, plugin authors need to define `P_MessageToBuffer` and `P_BufferToMessage`, which should be the same as those defined in proper, except the namespace of protobuf.
91+
Serialization function `tensorflow::MessageToBuffer` is already defined, desearization function `tensorflow::BufferToMessage` will be added in proper to convert plugin passed `TF_Buffer` back to proper's `GraphDef`. In plugin side, plugin authors need to define `plugin::MessageToBuffer` and `plugin::BufferToMessage`, which should be the same as those defined in proper, except the namespace of protobuf. To be noticed, these two are internal defined functions, they are not a part of C API interface.
9292
9393
Proper:
9494
```cpp
95-
Status MessageToBuffer(const tensorflow::protobuf::MessageLite& in, TF_Buffer* out);
96-
Status BufferToMessage(const TF_Buffer* in, tensorflow::protobuf::MessageLite& out);
95+
Status tensorflow::MessageToBuffer(const tensorflow::protobuf::MessageLite& in, TF_Buffer* out);
96+
Status tensorflow::BufferToMessage(const TF_Buffer* in, tensorflow::protobuf::MessageLite& out);
9797
```
9898

9999
Plugin:
100100
```cpp
101-
Status P_MessageToBuffer(const plugin::protobuf::MessageLite& in, TF_Buffer* out);
102-
Status P_BufferToMessage(const TF_Buffer* in, plugin::protobuf::MessageLite& out);
101+
Status plugin::MessageToBuffer(const plugin::protobuf::MessageLite& in, TF_Buffer* out);
102+
Status plugin::BufferToMessage(const TF_Buffer* in, plugin::protobuf::MessageLite& out);
103103
```
104104
105105
* **Util functions**
@@ -109,8 +109,8 @@ When initializing, TensorFlow loads the plugin and registers a new graph optimiz
109109
* **Registration**
110110
1. Core TensorFlow links to plugin's dynamic library and loads the function `TF_InitGraphPlugin`.
111111
2. In `TF_InitGraphPlugin`,
112-
- Plugin populates `P_RegistrationParams`.
113-
- Plugin creates an optimizer `TF_OptimizerBuilder` with creation/optimization/deletion functions.
112+
- Plugin populates `TP_RegistrationParams`.
113+
- Plugin populates `TP_OptimizerBuilder` with creation/optimization/deletion functions.
114114
- Plugin calls `TF_RegisterOptimizer` to register the optimizer with params.
115115
116116
### Supported User Scenarios
@@ -130,13 +130,13 @@ Plugin graph optimization is targeting backend device specific optimization, and
130130

131131
* **Configuring existing optimizers**
132132

133-
If pluggable graph optimizer is registered to a device type, e.g., GPU, its 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 `P_RegistrationParams`.
133+
If pluggable graph optimizer is registered to a device type, e.g., GPU, its 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_RegistrationParams`.
134134

135135
```cpp
136136
TF_Bool get_remapping() { return false; }
137137
TF_Bool get_auto_mixed_precision() { return true; }
138138

139-
void TF_InitGraphPlugin(P_RegistrationParams* params, TF_Status* status) {
139+
void TF_InitGraphPlugin(TP_OptimizerBuilder* builder, TP_RegistrationParams* params, TF_Status* status) {
140140
// Plugin authors can turn on/off some optimizers.
141141
params.config_fns.get_remapping = get_remapping;
142142
params.config_fns.get_auto_mixed_precision = get_auto_mixed_precision;
@@ -301,7 +301,7 @@ Plugin graph optimization is targeting backend device specific optimization, and
301301
// Flags indicating whether existing optimizers should be turned on/off.
302302
// It's optional for plugin to set functions to return true/false. If not
303303
// set, proper uses default configuration.
304-
typedef struct P_OptimizerConfigFns {
304+
typedef struct TP_OptimizerConfigFns {
305305
size_t struct_size;
306306
void* ext; // reserved for future use
307307
TF_Bool (*get_disable_model_pruning)();
@@ -323,12 +323,12 @@ Plugin graph optimization is targeting backend device specific optimization, and
323323
TF_Bool (*get_memory_optimization)();
324324
TF_Bool (*get_auto_parallel)();
325325
TF_Bool (*get_scoped_allocator_optimization)();
326-
} P_OptimizerConfigFns;
326+
} TP_OptimizerConfigFns;
327327
328-
#define P_OPTIMIZERCONFIG_FNS_STRUCT_SIZE \
329-
TF_OFFSET_OF_END(P_OptimizerConfigFns, get_scoped_allocator_optimization)
328+
#define TP_OPTIMIZERCONFIG_FNS_STRUCT_SIZE \
329+
TF_OFFSET_OF_END(TP_OptimizerConfigFns, get_scoped_allocator_optimization)
330330
331-
typedef struct P_RegistrationParams {
331+
typedef struct TP_RegistrationParams {
332332
size_t struct_size;
333333
void* ext; // reserved for future use
334334
@@ -338,24 +338,30 @@ Plugin graph optimization is targeting backend device specific optimization, and
338338
339339
// device_type optimizer is registered.
340340
const char* device_type;
341-
P_OptimizerConfigFns* config_fns;
342-
} P_RegistrationParams;
341+
TP_OptimizerConfigFns* config_fns;
342+
} TP_RegistrationParams;
343343
344-
#define P_REGISTRATIONPARAMS_STRUCT_SIZE \
345-
TF_OFFSET_OF_END(P_RegistrationParams, config_fns)
346-
347-
void TF_InitGraphPlugin(P_RegistrationParams* params, TF_Status* status);
344+
#define TP_REGISTRATIONPARAMS_STRUCT_SIZE \
345+
TF_OFFSET_OF_END(TP_RegistrationParams, config_fns)
348346
349347
// Struct for Optimizer builder. Plugin authors must provide an optimize function.
350348
// Creation and deletion functions are optional.
351-
typedef struct TF_OptimizerBuilder {
349+
typedef struct TP_OptimizerBuilder {
350+
size_t struct_size;
351+
void* ext; // reserved for future use
352+
352353
void* (*create_func)(),
353354
void (*optimize_func)(void*, TF_Buffer*, TF_Buffer*),
354355
void (*delete_func)(void*)
355-
} TF_OptimizerBuilder;
356+
} TP_OptimizerBuilder;
356357
357-
// Register plugin optimizer TF_OptimizerBuilder with P_RegistrationParams.
358-
void TF_RegisterOptimizer(TF_OptimizerBuilder* builder, P_RegistrationParams* params, TF_Status* status);
358+
#define TP_OPTIMIZERBUILDER_STRUCT_SIZE \
359+
TF_OFFSET_OF_END(TP_OptimizerBuilder, delete_func)
360+
361+
void TF_InitGraphPlugin(TP_OptimizerBuilder* builder, TP_RegistrationParams* params, TF_Status* status);
362+
363+
// Register plugin optimizer TP_OptimizerBuilder with TP_RegistrationParams.
364+
void TF_RegisterOptimizer(TP_OptimizerBuilder* builder, TP_RegistrationParams* params, TF_Status* status);
359365
360366
#ifdef __cplusplus
361367
} // extern "C"
@@ -386,7 +392,7 @@ Plugin graph optimization is targeting backend device specific optimization, and
386392

387393
// 2. Deserialize graph_buf into plugin::GraphDef
388394
plugin::GraphDef graph_def;
389-
P_BufferToMessage(graph_buf, graph_def);
395+
plugin::BufferToMessage(graph_buf, graph_def);
390396

391397
// 3. Infer shapes(optional)
392398
TF_GraphProperties g_prop = TF_NewGraphProperties(item);
@@ -399,7 +405,7 @@ Plugin graph optimization is targeting backend device specific optimization, and
399405
}
400406
TF_GetInputProperties(g_prop, "node1", in_prop_buf.data(), &max_size);
401407
plugin::OpInfo::TensorProperties in_prop;
402-
P_BufferToMessage(in_prop_buf, in_prop);
408+
plugin::BufferToMessage(in_prop_buf, in_prop);
403409
for (int i = 0; i < max_size; i++)
404410
TF_DeleteBuffer(in_prop_buf[i]);
405411

@@ -409,14 +415,14 @@ Plugin graph optimization is targeting backend device specific optimization, and
409415
TF_Buffer* op_buf = TF_NewBuffer();
410416
plugin::NodeDef node_def = graph_def.node(0);
411417
TF_LookUpOpDef(f_lib, node_def.name(), op_buf);
412-
P_BufferToMessage(op_buf, op_def);
418+
plugin::BufferToMessage(op_buf, op_def);
413419
TF_DeleteBuffer(op_buf);
414420
plugin::DataType dt = op_def.input_arg(0).type();
415421

416422
// 5. Transform graph(optional)
417423

418424
// 6. Serialize output plugin::GraphDef into optimized_graph_buf.
419-
P_MessageToBuffer(graph_def, optimized_graph_buf);
425+
plugin::MessageToBuffer(graph_def, optimized_graph_buf);
420426
TF_DeleteGraphProperties(g_prop);
421427
TF_DeleteFunctionLibraryDefinition(f_lib);
422428
}
@@ -428,16 +434,17 @@ Plugin graph optimization is targeting backend device specific optimization, and
428434
TF_Bool get_remapping() { return false; }
429435
TF_Bool get_auto_mixed_precision() { return true; }
430436
431-
void TF_InitGraphPlugin(P_RegistrationParams* params, TF_Status* status) {
432-
params.device_type = "GPU";
437+
void TF_InitGraphPlugin(TP_OptimizerBuilder* builder, TP_RegistrationParams* params, TF_Status* status) {
438+
params->device_type = "GPU";
433439
// Define some flags indicating whether existing optimizers should be turned on/off
434-
params.config_fns.get_remapping = get_remapping;
435-
params.config_fns.get_auto_mixed_precision = get_auto_mixed_precision;
440+
params->config_fns->get_remapping = get_remapping;
441+
params->config_fns->get_auto_mixed_precision = get_auto_mixed_precision;
436442
// ...
437443
438444
// Create a new builder and register it with TF_RegisterOptimizer
439-
TF_OptimizerBuilder* builder =
440-
TF_OptimizerBuilder(&P_Create, &P_Optimize, &P_Delete);
445+
builder->create_func = MyOptimizer_Create;
446+
builder->optimize_func = MyOptimizer_Optimize;
447+
builder->delete_func = MyOptimizer_Delete;
441448
TF_RegisterOptimizer(builder, params, status);
442449
if (TF_GetCode(status) != TF_OK) { /* handle errors */ }
443450
}
@@ -453,12 +460,13 @@ Plugin graph optimization is targeting backend device specific optimization, and
453460
tensorflow::Env* env = tensorflow::Env::Default();
454461
env->GetSymbolFromLibrary(dso_handle, "TF_InitGraphPlugin", &dso_symbol).IgnoreError();
455462

456-
using TF_InitGraphPlugin = void(*)(P_RegistrationParams*, TF_Status*);
463+
using TF_InitGraphPlugin = void(*)(TP_OptimizerBuilder*, TP_RegistrationParams*, TF_Status*);
457464
auto init_plugin_fn = reinterpret_cast<TF_InitGraphPlugin>(dso_symbol);
458465

459-
P_RegistrationParams params;
466+
TP_OptimizerBuilder builder;
467+
TP_RegistrationParams params;
460468
TF_Status status;
461-
init_plugin_fn(&params, status);
469+
init_plugin_fn(&builder, &params, status);
462470
}
463471
```
464472

0 commit comments

Comments
 (0)