Glow Low level IR queries
Hi Glow community,
I have below queries on glow Low Level IR.
Backends which do node at a time scheduling:
Glow's low-level IR is generated after traversing the graph in topological order or child-memory based order. Is there a way backends get a handle to specify the order of traversal for IRGen. Each Backend has its own requirement to schedule the nodes on their device. This option would allow backends to be have more control in scheduling nodes.
Graph based backends:
For backends which want to schedule a graph in total to device, knowing actual connections between nodes is necessary. Since LLIR is generated after graph traversal, I believe LLIR represents the graph as set of instructions related through memory rather than actual connections in the original graph level IR. Is there a way to get this nodes connection information from low level IR?
Thanks.
Hi @skc7,
Regarding Instruction ordering, I don't believe that there is a "plug-in" scheduling system when generating the IR, but nothing prevents a backend from reordering the IR once it is generated or scheduling the individual Instructions in any way it prefers. You may need to be careful about buffer reuse, however, depending on your circumstance. I'm not 100% sure I'm answering your question here, and example of what you want would make this clearer for me.
For graph based backends, it is possible to rebuild the graph via inspecting the operands of each Instruction, but what we generally do instead in these situations is use the high level Graph IR instead of the low level Instruction IR (see Habana & NNPI Backends). If the backend wants they can then generate LLIR for a transformed graph, or subgraphs, etc.
We do have two IR schedulers -- ChildMemSizeBased (default) and TopologicalSortBased. We don't have a way for the backend to specify which they prefer (easy to add), nor do we have a way to specify their own (a little more work). Do you have a specific use case/need in mind for specifying your own that isn't better covered by using one of the default ones and tweaking it a bit?
RE: understanding connections between instructions -- what is the motivation here? What are you wanting to do on LLIR instead of Nodes and why is it desirable to do it on LLIR instead?
Thanks @nickgg @jfix71 for the reply.
Here is the scenario I'm looking for in graph based backends: Backends can delay the actual creation of backend specific internal graph to runtime instead of compile time. When a bigger subgraph is assigned to the backend device to run, graph based backends can choose to split the subgraph further to more smaller subgraphs to be run on same device during runtime. The order of schedule of the smaller subgraphs may be decided by backend specific apis. In this scenario getting the graph connections along with memory optimizations is necessary. If Low level IR has the ability to provide the node connections along with memory requirements, backends can only work on LLIR and generate the smaller subgraphs required.
@skc7 Ok, so the idea is to generate LLIR but stop compilation there (at least for the Backend::compile() call), and then at runtime split the LLIR into subgraphs and JIT to device code based on some runtime info?
One question here is about the overhead of generating LLIR -- is it possible to just keep the Node-based IR around and split at that point, or is the overhead of generating LLIR too high?
It's totally doable to trace through LLIR to split out subgraphs based on dependencies, it's just going to be not as clean, you'll have to trace through alloc activations between ops. And there may be some weirdness here on buffer sharing too, I haven't thought through it fully.
Hi @jfix71
Yes, splitting the graph to subgraphs is easier to do on Glow graph. But to get node connections from low level IR requires some effort.
Here is an example. I'm trying to get graph connections from LLIR
Graph: main_After_TransformPostLowering.dot.pdf
Generated LLIR: function main declare { %input_1 = WeightVar float<1 x 1 x 8 x 8> mutable // size: 256 // Users: @in 2, @in 1 %input_2 = WeightVar float<1 x 1 x 8 x 8> mutable // size: 256 // Users: @in 3, @in 1 %A5 = WeightVar float<1 x 1 x 8 x 8> mutable // size: 256 // Users: @in 4, @out 2, @out 4
; size = 768 bytes }
code { 0 %Add_2_res = allocactivation { Ty: float<1 x 1 x 8 x 8>} // size: 256 // Users: @in 4, @out 3, @out 5, @in 3, @in 2, @out 1 1 %Add_2 = elementadd @out %Add_2_res, @in %input_1, @in %input_2 2 %Add_3 = elementadd @out %A5, @in %Add_2_res, @in %input_1 3 %Add_4 = elementadd @out %Add_2_res, @in %Add_2_res, @in %input_2 4 %Add_5 = elementadd @out %A5, @in %A5, @in %Add_2_res 5 %dealloc_Add_2_res = deallocactivation @out %Add_2_res // size: 256 }
LLIR has done these memory optimizations:
- since add can do inplace operation, Add_4 read and write to same Add_2_res buffer
- Add_3 wrote to output placeholder A5, Add_5 read from A5 and wrote to A5.
Users list:
- Users of Add_2_res are Add_1, Add_2, Add_3, Add_4
- Users of A5 are Add_3 and Add_5
If I have to rebuild the graph from LLIR, should I iterate through user list of buffers starting from input/output placeholders and get the buffer to Instruction order of access to the buffer, and try to rebuild the graph from there? Also if there are tensorview instructions in LLIR, how should that case be handled?
Thanks.
If I have to rebuild the graph from LLIR, should I iterate through user list of buffers starting from input/output placeholders and get the buffer to Instruction order of access to the buffer, and try to rebuild the graph from there?
Yeah, that'd be your best bet. But yes it does appear like things like buffer sharing is going to make this more difficult.
Also if there are tensorview instructions in LLIR, how should that case be handled?
It's hard to say -- tensorviews are basically just a view of some memory. It means if we have multiple instructions reading multiple tensorviews that all have the same input buffer then all users of the tensor views have a dependence on the original buffer. But they may have dependences on different (possibly independent) parts of that buffer, e.g. when we have a SliceNode.
This is why I am curious about what issues you have with doing this split at runtime based on the Node-based graph IR instead, and then doing low level IRGen from there. Is the overhead of generating LLIR too high?
Hi @jfix71
Thanks for the reply. I was just curious to figure out a way to generate graph back from LLIR. Yes, as u have suggested, splitting graph using graph IR is better approach. There is no overhead in generating LLIR at runtime.