Serialization - saving multiple models in a single file

I am trying to save several models in a single file. Strangely, I faced an issue that a single Linear layer cannot be saved and subsequently read, but a Linear inside Sequential can be saved and read.

The following code works ok:

	const std::string FN = "d:\\test.dat";
	struct mtype : torch::nn::Sequential 
	{
		mtype() : torch::nn::Sequential(torch::nn::Linear(5, 2)) {};
	};
	// torch::nn::Linear MLP(10,2);

	mtype MLP;
	torch::serialize::OutputArchive MLPArchive;
	
	for (int i = 0; i < 10; i++)  
	{	
		torch::serialize::OutputArchive nestedarchive;
		MLP->save(nestedarchive);
		MLPArchive.write(std::to_string(i), nestedarchive);
	}
	MLPArchive.save_to(FN);
        torch::serialize::InputArchive IA;
	IA.load_from(FN);	

but if i change saved model from Sequential to Linear as follows, than it throws an exception on the last line load_from(file). What’s the problem?

The following code throws an exception on the last line.

	const std::string FN = "d:\\test.dat";
	torch::nn::Linear MLP(10,2);
	torch::serialize::OutputArchive MLPArchive;
	
	for (int i = 0; i < 10; i++)  
	{	
		torch::serialize::OutputArchive nestedarchive;
		MLP->save(nestedarchive);
		MLPArchive.write(std::to_string(i), nestedarchive);
	}
	MLPArchive.save_to(FN);
        torch::serialize::InputArchive IA;
	IA.load_from(FN);	
1 Like

Just in case it may help somebody, I found that nested archive must be initiated with the same compilation unit as the main archive. For the above example:

torch::serialize::OutputArchive nestedarchive(MLPArchive.compilation_unit());
2 Likes