Is there an equivalent of torch.distributions.MultivariateNormal in LibTorch, the C++ API for PyTorch?

username: DapengFeng
cooked: <p>Is there a way to implement a Multivariate Normal Distribution in the PyTorch C++ API?</p>

username: DapengFeng
cooked: <p>Eigen Multivariate Normal Distribution</p><aside class="onebox githubblob" data-onebox-src="https://github.com/beniz/eigenmvn/blob/master/eigenmvn.h">
  <header class="source">

      <a href="https://github.com/beniz/eigenmvn/blob/master/eigenmvn.h" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/beniz/eigenmvn/blob/master/eigenmvn.h" target="_blank" rel="noopener nofollow ugc">beniz/eigenmvn/blob/master/eigenmvn.h</a></h4>


      <pre><code class="lang-h">/**
 * Multivariate Normal distribution sampling using C++11 and Eigen matrices.
 * 
 * This is taken from http://stackoverflow.com/questions/16361226/error-while-creating-object-from-templated-class
 * (also see http://lost-found-wandering.blogspot.fr/2011/05/sampling-from-multivariate-normal-in-c.html)
 * 
 * I have been unable to contact the original author, and I've performed
 * the following modifications to the original code:
 * - removal of the dependency to Boost, in favor of straight C++11;
 * - ability to choose from Solver or Cholesky decomposition (supposedly faster);
 * - fixed Cholesky by using LLT decomposition instead of LDLT that was not yielding
 *   a correctly rotated variance 
 *   (see this http://stats.stackexchange.com/questions/48749/how-to-sample-from-a-multivariate-normal-given-the-pt-ldlt-p-decomposition-o )
 */

/**
 * Copyright (c) 2014 by Emmanuel Benazera beniz@droidnik.fr, All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
</code></pre>



  This file has been truncated. <a href="https://github.com/beniz/eigenmvn/blob/master/eigenmvn.h" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


username: DapengFeng
cooked: <p>I open an issue at Github.</p><aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/pytorch/issues/138387">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/issues/138387" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue" data-github-private-repo="false">
	  <svg width="60" height="60" class="github-icon" viewBox="0 0 14 16" aria-hidden="true"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/138387" target="_blank" rel="noopener nofollow ugc">torch::distributions module for C++ frontend API</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2024-10-19" data-time="12:11:10" data-timezone="UTC">12:11PM - 19 Oct 24 UTC</span>
      </div>


      <div class="user">
        <a href="https://github.com/DapengFeng" target="_blank" rel="noopener nofollow ugc">
          <img alt="DapengFeng" src="https://avatars.githubusercontent.com/u/18318646?v=4" class="onebox-avatar-inline" width="20" height="20">
          DapengFeng
        </a>
      </div>
    </div>

    <div class="labels">
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          module: cpp
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          triaged
        </span>
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">### 🚀 The feature, motivation and pitch

It seems that torch.distribution is o<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">nly support in pytorch, but not for libtorch, the C++ frontend. And some  C++ applications need to sample data from the distributions. So the implementation of the torch.distributions is helpful.

For example, 
https://discuss.pytorch.org/t/how-to-create-multivariate-normal-samples-in-c-given-mean-vector-and-covariance-matrix/123960

https://discuss.pytorch.org/t/how-to-use-torch-distributions-gumbel-in-c-api/172368

https://discuss.pytorch.org/t/how-to-create-multivariate-normal-samples-in-c-given-mean-vector-and-covariance-matrix/123960

https://discuss.pytorch.org/t/torch-distributions-categorical/45747

https://discuss.pytorch.org/t/is-there-an-equivalent-of-torch-distributions-multivariatenormal-in-libtorch-the-c-api-for-pytorch/211784/2

### Alternatives

_No response_

### Additional context

_No response_

cc @jbschlosser</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>