Loading extra files in TorchScript from libTorch C++

DTSED: <p>Hi, I would like to know how to load a file included in traced .pt model that was added as an extra file in C++. In this case I save the model with PyTorch and try to load it with C++.<br>
This could be an example (from documentation):</p>
<pre><code class="lang-python">class MyModule(torch.nn.Module):
    def forward(self, x):
        return x + 10

m = torch.jit.script(MyModule())
# Save with extra files
extra_files = torch._C.ExtraFilesMap()
extra_files['module.meta'] = 'metadata'
torch.jit.save(m, 'scriptmodule.pt', _extra_files=extra_files)
</code></pre>
<p>module.meta could be a text file and I would like to read its content when loading model in C++ like this:</p>
<pre><code class="lang-cpp">#include &lt;torch/torch.h&gt;
#include &lt;torch/script.h&gt;

int main()
{
	torch::jit::script::Module module;
	try
	{
		std::string modulePath = "scriptmodule.pt";
		torch::jit::ExtraFilesMap extraFilesMap_;
		std::pair&lt;std::string, std::string&gt; moduleMeta("module.meta", "");
		extraFilesMap_.insert(moduleMeta);
		module = torch::jit::load(modelPath, torch::kCPU, extraFilesMap_);
		module.eval();
        for (auto const&amp; fileMap : extraFilesMap_)
		{
			std::cout &lt;&lt; fileMap.first &lt;&lt; std::endl;
			std::cout &lt;&lt; fileMap.second &lt;&lt; std::endl;
		}
        //How could open and read the content of the file "module.meta"
	}
	catch (const std::exception&amp; e)
	{
		const std::string errorMsg = e.what();
		std::cerr &lt;&lt; " Error: " &lt;&lt; errorMsg &lt;&lt; std::endl;
	}
    return 0;
 }
</code></pre>
<p>From here, extraFilesMap_ when doing iteration will print fileMap.first = module.meta and fileMap.second = metadata</p>
<p>How could I read that extra file content?</p>
<p>Thanks in advance</p>

glaringlee: <aside class="quote no-group" data-username="DTSED" data-post="1" data-topic="97529">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dtsed/48/12990_2.png" class="avatar"> DTSED:</div>
<blockquote>
<p><code>extraFilesMap</code></p>
</blockquote>
</aside>
<p>Please take a look at this<br>
‘<a href="https://github.com/pytorch/pytorch/blob/22401b850b2010119d3f674f495b7c2b2dfb710b/test/cpp/jit/test_save_load.cpp#L16" rel="noopener nofollow ugc">https://github.com/pytorch/pytorch/blob/22401b850b2010119d3f674f495b7c2b2dfb710b/test/cpp/jit/test_save_load.cpp#L16</a>’</p>
<p>It seems to me that the extra_files can store string only.<br>
If you want more info, please change the category to ‘jit’, jit people can provide more info.</p>

DTSED: <p>Thanks for your response <a class="mention" href="/u/glaringlee">@glaringlee</a> I have seen that the test is only for retrieving the std::pair&lt;std::string, std::string&gt; from extraFilesMap. I will change the category for trying to find more help for this.</p>

rvimieiro: <p>Check my <a href="https://stackoverflow.com/a/75030944/8682939" rel="noopener nofollow ugc">answer</a> here at stackoverflow. I wrote a code that works for me loading 2 double variables on c++ that were saved with extra_files on python with PyTorch.</p>

DTSED: <p>Thanks for your reply!, It gives me an example of how to use it</p>