Using torch::normal with the C++ frontend

dfalbel: <p>I am probably missing something, but I am having trouble using the torch::normal function in the C++ API. Here’s the minimal example:</p>
<pre><code class="lang-auto">#include &lt;torch/torch.h&gt;
#include &lt;iostream&gt;

int main() {
  torch::Tensor tensor = torch::rand({2, 3});
  auto x = torch::normal(tensor,1);
  std::cout &lt;&lt; x &lt;&lt; std::endl;
}
</code></pre>
<p>The compilation of this example fails with:</p>
<pre><code class="lang-auto">/Users/dfalbel/Documents/testtorch/example-app.cpp:6:12: error: too few arguments to function call, expected at least 3, have 2; did you mean 'at::normal'?
  auto x = torch::normal(tensor,1);
           ^~~~~
           at::normal
/Users/dfalbel/libtorch/include/ATen/Functions.h:13495:22: note: 'at::normal' declared here
static inline Tensor normal(const Tensor &amp; mean, double std, Generator * generator) {
                     ^
1 error generated.
</code></pre>
<p>This is can be fixed by using <code>at</code> namespace instead of <code>torch</code>. I could do that, but it seems to me that all signatures of <code>at::normal</code> (<a href="https://pytorch.org/cppdocs/api/function_namespaceat_1a0a688eac89d39cc2d841feb51f15dd7b.html#_CPPv4N2at6normalERK6TensordP9Generator" rel="nofollow noopener">1</a>, <a href="https://pytorch.org/cppdocs/api/function_namespaceat_1a3cd54ad074139d2a90c0b6fd211d383c.html#_CPPv4N2at6normalEdd11IntArrayRefP9GeneratorRK13TensorOptions" rel="nofollow noopener">2</a>, <a href="https://pytorch.org/cppdocs/api/function_namespaceat_1ac01b0a0fbfd6fbe2a9cc094c9fc92f71.html#_CPPv4N2at6normalERK6TensorRK6TensorP9Generator" rel="nofollow noopener">3</a>, <a href="https://pytorch.org/cppdocs/api/function_namespaceat_1af8598b49e964831d29ee519a45eb4ed3.html#_CPPv4N2at6normalEdRK6TensorP9Generator" rel="nofollow noopener">4</a>) should be exported in the <code>torch</code> namespace too? Does this make sense?</p>

dfalbel: <p>It seems that the reason is that when we autogenerate wrapers for some ATen functions <a href="https://github.com/pytorch/pytorch/blob/4e21157e01e07202b91274d19c1fb2849977d9e0/tools/autograd/gen_variable_factories.py" rel="nofollow noopener">here</a></p>
<p>we are generating a <a href="https://pytorch.org/cppdocs/api/function_namespacetorch_1af438ffddcfbd2dc8b7b040590f1e1c34.html#_CPPv4N5torch6normalEddN2at11IntArrayRefEPN2at9GeneratorERKN2at13TensorOptionsE" rel="nofollow noopener">single</a> <code>torch::normal</code> definition and not all it’s overloaded defs.</p>
<p>And this seems to happen because of <a href="https://github.com/pytorch/pytorch/blob/4e21157e01e07202b91274d19c1fb2849977d9e0/tools/autograd/gen_variable_factories.py#L40" rel="nofollow noopener">this line</a> which excludes from the <code>variable_factories.h</code> all functions that don’t have TensorOptions as an argument, thus excluding the other signatures.</p>
<p>Adding the following to my code fixes the compiler error, but i am pretty sure <strong>it’s wrong</strong> <img src="https://discuss.pytorch.org/images/emoji/apple/no_entry_sign.png?v=9" title=":no_entry_sign:" class="emoji" alt=":no_entry_sign:"> when in autograd contexts:</p>
<pre><code class="lang-auto">namespace torch {

torch::Tensor normal (const torch::Tensor &amp;mean, double std = 1, torch::Generator *generator = nullptr) {
  return at::normal(mean, std, generator);  
}

torch::Tensor normal (double mean, const torch::Tensor &amp;std, torch::Generator *generator = nullptr) {
  return at::normal(mean, std, generator);  
}

torch::Tensor normal (const torch::Tensor &amp;mean, const torch::Tensor &amp;std, torch::Generator *generator = nullptr) {
  return at::normal(mean, std, generator);  
}

} // namespace torch
</code></pre>