## Description
Gluon can have any hierarchy of Blocks where a top-level Block is not a 
HybridBlock, and lower-level blocks are HybridBlocks. These HybridBlocks can be 
dispersed throughout the model architecture. Users can optimize the parts of 
their model that support it by calling `hybridize` on the top level block to 
trigger a recursive call throughout all child blocks. But there is currently no 
way to export all HybridBlocks without going through and manually calling 
`export` on each one. Further, theres no way to reload those exported symbol 
files back without changing the model design and swapping those HybridBlocks 
for SymbolBlocks and than one-by-one calling `imports` to reload. 

I would like to ask for suggestions from the community and have an active 
discussion on the different ways to address this problem. To kick things off, 
here is a proposal to start the discussion around:

```
def save_cached_graphs(block):
    def _save_cached_graphs(blk, index):
        if isinstance(blk, mx.gluon.nn.HybridBlock):
            blk.export(blk.name + str(index))
        for child in blk._children.values():
            index += 1
            _save_cached_graphs(child, index)
    #save top-level block                                                       
                           
    index = 0
    _save_cached_graphs(block, index)

def load_cached_graphs(block):
    def _load_cached_graphs(blk, index):
        if isinstance(blk, mx.gluon.nn.HybridBlock):
            sym = symbol.load(blk.name + str(index) + '-symbol.json')
            blk._cached_graph = sym
        for child in blk._children.values():
            index += 1
            _load_cached_graphs(child, index)
    #load top-level block                                                       
                           
    index = 0
    _load_cached_graphs(block, index)
```

With these two functions, we can recursively export each hybrid block and then 
reload the symbols. Obviously the code is not complete or even functional 
(`_cached_graph` is actual a tuple of symbols and sym.var inputs). But should 
serve as a point of reference. 



-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/apache/incubator-mxnet/issues/19535

Reply via email to