Skip to content

Commit 13f1cf1

Browse files
tlepley-cadenceopti-mix
authored andcommitted
[doc] Add documentation for external backends
1 parent 241d4e9 commit 13f1cf1

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

docs/Backends.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,14 @@ optimizations to take place.
194194
The `tools/ClassGen/Backends/CPU/CPUSpecificNodes.h` and
195195
`tools/ClassGen/Backends/CPU/CPUSpecificInstrs.h` files are included in
196196
`tools/ClassGen/NodeGen.cpp` and `tools/ClassGen/InstrGen.cpp`, respectively.
197+
198+
199+
#### External backends
200+
201+
External backends can be added to Glow without changing the Glow build infrastructure.
202+
203+
An external backend is provided as a single source directory. It can then be developped in a separate source management repository.
204+
205+
The external backend mechanism is for instance convenient for adding closed source backends to Glow.
206+
207+
The structure of external backends is defined [here](https://github.com/pytorch/glow/blob/master/docs/ExternalBackend.md).

docs/ExternalBackend.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
## External backends
2+
3+
An external backend can be added to Glow without changing the Glow build infrastructure. An external backend must be provided as a single source directory, and then can be developped in a separate source management repository.
4+
5+
### External backend structure
6+
7+
An external backend must have the following structure:
8+
```
9+
`-- <Name>
10+
|-- CMakeLists.txt
11+
|-- Backends
12+
| |-- CMakeLists.txt
13+
| |-- <main backend sources>
14+
|-- ClassGen
15+
| |-- CMakeLists.txt
16+
| |-- <ClassGen sources>
17+
`-- Tests
18+
|-- CMakeLists.txt
19+
|-- <Tests sources>
20+
```
21+
22+
To be integrated into Glow and compiled, the backend directory must be placed in the `externalbackends/` sub-directory of the Glow project.
23+
24+
25+
### Backend structure details
26+
27+
#### Defined cmake variables
28+
29+
When an external backend is detected and activated, the following CMake variable is defined:
30+
31+
- `EXTERNAL_BACKEND_NAME` : name derived from the external backend directory name. Can be used by the backend to name libraries for instance.
32+
33+
34+
There is the possibility to disable an detected external backend by setting the following CMake define to `OFF`:
35+
36+
- `GLOW_WITH_<BACKEND DIR NAME>`
37+
38+
39+
#### C++ define
40+
41+
When an external backend is activated, Glow adds a global C++ compiler define that can be used to enclose backend specific code in the generic Glow source code, based on the activation of this external backend:
42+
43+
- `GLOW_WITH_<BACKEND DIR NAME>`
44+
45+
46+
#### Backend Core : Mandatory
47+
48+
The core of the external backend is mandatory and located in `<Name>/Backends/`. It must contain a `CMakeLists.txt` file that will ensure the correct compilation of the backend and the good integration with Glow.
49+
50+
The external backend must respect the same interface as standard backends.
51+
It can be summarized the following way:
52+
53+
- Backend main class that inherits from the Glow `Backend` class. For example, class `<Name>Backend` in library `${EXTERNAL_BACKEND_NAME}`.
54+
- Backend factory registration in a backend specific factory library. For example, calling `REGISTER_GLOW_BACKEND_FACTORY(<Name>Factory, <Name>Backend, <Name>)` in library `${EXTERNAL_BACKEND_NAME}Factory`.
55+
56+
57+
The external backend is declared to Glow by setting the CMake `linked_factories` variable in the `<Name>/Backends/CMakeLists.txt`. Example:`set(linked_factories ${linked_factories} <Name>Factory PARENT_SCOPE)`
58+
59+
For more details, please refer to the general Glow backend documentation.
60+
61+
#### Top level CMake configuration file
62+
63+
Global configuration of the backend can be specified in a top level `<Name>/CMakeLists.txt`.
64+
65+
This file is optional.
66+
67+
#### Backend specific nodes and instructions
68+
69+
An external backend can optionally declare specific Glow nodes and instructions.
70+
In this case, the `<Name>/ClassGen/CMakeLists.txt` must be provided and backend specific nodes and instructions declaration headers declared.
71+
72+
CMake example:
73+
```
74+
set(VERIF_FILENAME <Name>SpecificNodesVerification.h)
75+
configure_file(${VERIF_FILENAME}
76+
${GLOW_BINARY_DIR}/glow/${VERIF_FILENAME} COPYONLY)
77+
78+
set(VERIF_FILENAME <Name>SpecificInstrsVerification.h)
79+
configure_file(${VERIF_FILENAME}
80+
${GLOW_BINARY_DIR}/glow/${VERIF_FILENAME} COPYONLY)
81+
```
82+
83+
These header files must be included in the generic glow code, respectivelly in `tools/ClassGen/NodeGen.cpp` and `tools/ClassGen/InstrGen.cpp`, at the end of the `main` function like standard backends.It is advised to enclose the backend specific include into `#ifdef GLOW_WITH_<BACKEND DIR NAME>`.
84+
85+
Example for specific nodes:
86+
```
87+
int main(int argc, char **argv) {
88+
// <generic glow code>
89+
90+
#include "Backends/CPU/CPUSpecificNodes.h"
91+
#include "Backends/OpenCL/OpenCLSpecificNodes.h"
92+
93+
#ifdef GLOW_WITH_<NAME>
94+
# include "<Name>/ClassGen/<Name>SpecificNodes.h"
95+
#endif
96+
97+
return 0;
98+
}
99+
100+
```
101+
102+
103+
#### Backend specific tests
104+
105+
Backend specific tests can be added. In this case, the `<Name>/Tests/CMakeLists.txt` must be provided. The Backend tests must be compiled as a library and registered to glow with the `add_glow_test` cmake function.
106+
107+
CMake example:
108+
```
109+
add_executable(<Name>Test
110+
<test sources>)
111+
112+
target_link_libraries(<Name>Test
113+
PRIVATE
114+
Backends
115+
ExecutionEngine
116+
Graph
117+
Optimizer
118+
gtest
119+
TestMain)
120+
121+
add_glow_test(<name>Test ${GLOW_BINARY_DIR}/tests/<name>Test --gtest_output=xml:Xtensa<name>Test.xml)`
122+
123+
```
124+
125+
126+
For details, please refer to the general Glow testing documentation.
127+
128+

0 commit comments

Comments
 (0)