Serializing and loading custom HybridBlock

#1

I have a model component that inherits from Hybridblock. It contains several fields as Python objects, which would be beneficial to save along with the symbol.

Is there a way to do that?
Currently using SymbolBlock.imports returns only SymbolBlock, and it forgets about parameters that are defined in the class.

If there’s no way to do that, how do you guys circumvent that in practice? Do you write custom loading functions in your blocks?

#2

I am not sure I fully understand your question. Can you provide a small example that reproduces the problem?

#3
class ExampleBlock(nn.HybridBlock):
    
    def __init__(self, n):
        self.n = n
        self.d = nn.Dense(n)
        
    def hybrid_forward(self, F, X):
        return self.d(X)

I can export ExampleBlocks to files, and then import them using SymbolicBlock.imports, but if I use that, the returned object has class SymbolicBlock and it doesn’t have field n.

#4

When you serialize your block using export(), what essentially gets stored is the computational graph (architecture of the model) and parameters of the model. The instances of the classes are not saved, so everything that is not related to the architecture and parameters is discarded, like self.n from your case.

I don’t think there is a way to do what you want with existing features of MXNet.

The only reliable approach would be to save only parameters of the block via save_parameters() method and pass it into recreated model. So, essentially, you don’t serialize the architecture of the model, but just reuse the same source file / helper method to create the structure of your model in runtime.