Convert a tensor with type caffe2::TypeMeta to std::vector

username: afshin67
cooked: <p>I have an tensor with type <code>caffe2::TypeMeta</code>, and I need to get its value as a regular <code>std::vector</code> object. I know how to convert a <code>Torch::Tensor</code> object with a regular type to a <code>std::vector</code> object, but the same procedure does not work for tensors with type <code>caffe2::TypeMeta</code>. I found an expensive way to do that, it needs to iterate over every single parameter on the <code>caffe2:TypeMeta</code> object which can be very expensive.</p>
<p>I faced these tensors with type <code>caffe2:TypeMeta</code> in the state of the optimizer, where I need to get the state value of different optimizer and modify them based on a given algorithm.</p>
<p>Below is the piece of the code which gets the tensors of <code>caffe2:TypeMeta</code>.<br>
I appreciate any comment on this.</p>
<pre><code class="lang-auto">
void get_state(torch::optim::Optimizer *optimizer){
    for (auto&amp; group : optimizer-&gt;param_groups()) {
        for (auto &amp;p : group.params()) {
            if (!p.grad().defined()) {
                continue;
            }

            auto grad = p.grad();
            TORCH_CHECK(!grad.is_sparse(),
                        "Adam does not support sparse gradients"/*, please consider SparseAdam instead*/);
            ska::flat_hash_map&lt;std::string, std::unique_ptr&lt;torch::optim::OptimizerParamState&gt;&gt;&amp; state_ = optimizer-&gt;state();
            auto param_state = state_.find(c10::guts::to_string(p.unsafeGetTensorImpl()));

            auto&amp; state = static_cast&lt;torch::optim::AdamParamState&amp;&gt;(*state_[c10::guts::to_string(p.unsafeGetTensorImpl())]);
            auto&amp; exp_avg = state.exp_avg();
            auto&amp; exp_avg_sq = state.exp_avg_sq();
            auto&amp; max_exp_avg_sq = state.max_exp_avg_sq();
            auto step_counter = state.step();

            // This works, but can be very expensive since I need to iterate over all the element for each of the tensors in the optimizer state
            if (exp_avg.dim() &gt; 1) {
                auto tmp_v2 = exp_avg[10][3].item().toDouble();
                std::cout &lt;&lt; tmp_v2 &lt;&lt; std::endl;
                tmp_v2 = exp_avg_sq[10][3].item().toDouble();
                std::cout &lt;&lt; tmp_v2 &lt;&lt; std::endl;
            }
                else
            {
                auto tmp_v2 = exp_avg[10].item().toDouble();
                std::cout &lt;&lt; tmp_v2 &lt;&lt; std::endl;
                tmp_v2 = exp_avg_sq[10].item().toDouble();
                std::cout &lt;&lt; tmp_v2 &lt;&lt; std::endl;
            }
            state.exp_avg(torch::ones_like(exp_avg));

            if (exp_avg.dim() &gt; 1) {
                auto tmp_v2 = exp_avg[10][3].item().toDouble();
                std::cout &lt;&lt; tmp_v2 &lt;&lt; std::endl;
                tmp_v2 = exp_avg_sq[10][3].item().toDouble();
                std::cout &lt;&lt; tmp_v2 &lt;&lt; std::endl;
            }
            else
            {
                auto tmp_v2 = exp_avg[10].item().toDouble();
                std::cout &lt;&lt; tmp_v2 &lt;&lt; std::endl;
                tmp_v2 = exp_avg_sq[10].item().toDouble();
                std::cout &lt;&lt; tmp_v2 &lt;&lt; std::endl;
            }

           // This does not work, it returns a memory segmentation fault error at the second line:
                auto g = torch::reshape(exp_avg, {-1,}).to(at::kDouble);
                auto v = std::vector&lt;double&gt;(g.data_ptr&lt;double&gt;(), g.data_ptr&lt;double&gt;() + g.numel());

        }
    }

}
</code></pre>