# How are python bindings created?

**URL:** <https://discuss.pytorch.org/t/how-are-python-bindings-created/46453>\
**Category:** Uncategorized\
**Created:** [May 28, 2019, 6:21pm UTC](https://discuss.pytorch.org/t/how-are-python-bindings-created/46453 "2019-05-28T18:21:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![PranovD](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/p/cdc98d/32.png) [@PranovD](https://discuss.pytorch.org/u/PranovD)\
**Post date:** [May 28, 2019, 6:21pm UTC](https://discuss.pytorch.org/t/how-are-python-bindings-created/46453/1 "2019-05-28T18:21:47Z")

</div>

I’m trying to figure out how the python bindings are generated as I see that pybind is used in some cases but it also seems like some python bindings are generated by custom files: [https://github.com/pytorch/pytorch/blob/master/tools/setup\_helpers/generate\_code.py](https://github.com/pytorch/pytorch/blob/master/tools/setup_helpers/generate_code.py)

Understanding how and why this works will be super helpful to me as I am trying to create C# bindings for libtorch.

---

<div class="post-metadata">

**Author:** ![colesbury](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/colesbury/32/26_2.png) [@colesbury](https://discuss.pytorch.org/u/colesbury)\
**Post date:** [May 28, 2019, 8:58pm UTC](https://discuss.pytorch.org/t/how-are-python-bindings-created/46453/2 "2019-05-28T20:58:09Z")

</div>

Most of the core PyTorch functions on the `torch` package and on `torch.Tensor` are generated from Declarations.yaml (\*) using [gen\_python\_functions.cpp](https://github.com/pytorch/pytorch/blob/master/tools/autograd/gen_python_functions.py). The generated files look like:

[python\_torch\_functions.cpp](https://gist.github.com/colesbury/9d93a8bcf72acabdd77c9ca805c6534c)  
[python\_variable\_methods.cpp](https://gist.github.com/973938719e613973aee1464f0bc7900e)

We don’t use pybind11 here for a few reasons:

1. We need to disambiguate multiple overloads in a PyTorch-specific way. Some of this behavior is due to historical Lua Torch function signatures. We try to avoid this ambiguity in new functions.
2. pybind11 tends to slow down compile time a lot

(\*) [Declarations.yaml](https://gist.github.com/a6a21d2cf40815258d4565f2373cf7d8) is generated from [native\_functions.yaml](https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/native_functions.yaml). There used to be other inputs that went into Declarations.yaml, but I’m not sure if that’s still the case. Anyways, Declarations.yaml is intended as the “public” machine-readable API specification for things like language bindings.

---

<div class="post-metadata">

**Author:** ![PranovD](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/p/cdc98d/32.png) [@PranovD](https://discuss.pytorch.org/u/PranovD)\
**Post date:** [May 28, 2019, 9:38pm UTC](https://discuss.pytorch.org/t/how-are-python-bindings-created/46453/4 "2019-05-28T21:38:01Z")

</div>

Because the bindings I am trying to make are in C# which supports method overloading, and compile time is not one of my constraints, would using a 3rd party bindings generator (similar to pybind) be recommended or are there other considerations?

---

<div class="post-metadata">

**Author:** ![colesbury](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/colesbury/32/26_2.png) [@colesbury](https://discuss.pytorch.org/u/colesbury)\
**Post date:** [May 28, 2019, 9:49pm UTC](https://discuss.pytorch.org/t/how-are-python-bindings-created/46453/5 "2019-05-28T21:49:20Z")

</div>

I don’t understand. How would pybind help with C#? Aren’t you trying to bind C++ functions in C#? (pybind11 binds C++ functions to Python)

---

<div class="post-metadata">

**Author:** ![PranovD](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/p/cdc98d/32.png) [@PranovD](https://discuss.pytorch.org/u/PranovD)\
**Post date:** [May 28, 2019, 9:51pm UTC](https://discuss.pytorch.org/t/how-are-python-bindings-created/46453/6 "2019-05-28T21:51:39Z")

</div>

I would not be using pybind I would be using a generator that would be for C# similar to the way pybind is for python, something like [http://www.swig.org/tutorial.html](http://www.swig.org/tutorial.html)

---

<div class="post-metadata">

**Author:** ![adampauls](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/adampauls/32/44406_2.png) [@adampauls](https://discuss.pytorch.org/u/adampauls)\
**Post date:** [November 30, 2021, 12:50am UTC](https://discuss.pytorch.org/t/how-are-python-bindings-created/46453/7 "2021-11-30T00:50:53Z")

</div>

I see that Declarations.yaml is generated when building pytorch. Ideally, when auto-generating bindings for libtorch, I wouldn’t have to run the complete build. Instead, I’d like to generate the Declarations.yaml file and then download the pre-built packages from [Start Locally | PyTorch](https://pytorch.org/get-started/locally/). Is it possible for me to only run the build step that generates Declarations.yaml given a download of pytorch source?

Thanks!

---

<div class="post-metadata">

**Author:** ![adampauls](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/adampauls/32/44406_2.png) [@adampauls](https://discuss.pytorch.org/u/adampauls)\
**Post date:** [November 30, 2021, 7:39pm UTC](https://discuss.pytorch.org/t/how-are-python-bindings-created/46453/8 "2021-11-30T19:39:29Z")

</div>

It seems that running `python -m tools.codegen.gen -s aten/src/ATen -d torch/share/ATen` from root of a source checkout puts Declarations.yaml in the expected place, though I’m not sure if it inappropriately puts other stuff in that folder as well.
