pytorch/tools/codegen/api
Brian Hirsh 947c7a8215 add C++ namespacing logic to ctypes (#55047)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55047

Added namespaces to all of the `CTypes` printed in the codegen. This is pretty much required if we want to use codegen externally, since we can no longer assume that we're inside of the `at::` namespace.

Important changes are in `types.py`.

How do we add the notion of namespaces to C++ types without people having to write "at::Tensor" everywhere? Before this PR, `CType` held a raw string representing the type, i.e. `BaseCType("Tensor", binds)`. This PR introduces a set of singleton base C++ types in `types.py`, that know how to print their namespace. Instead, we'd write `BaseCType(tensorT, binds)`, where printing `tensorT` will properly print out "at::Tensor".

This also means that you can't create arbitrary `CTypes`. If we need a new C++ type in the codegen, we need to add it to the list in `types.py`.

One blip in the design: we don't want to change `RegistrationDeclarations.yaml`, since that'll break external backends that ingest it. I added separate functions to display types without the namespace that are used to create RegistrationDeclarations.yaml`. With an external codegen API though, we can eventually kill it :)

I also didn't realize until this PR that `Declarations.yaml` is still directly in use, by some python/autograd codegen. Rather than keep that yaml byte-for-byte compatible, I just updated the callsites in the autograd codegen to work with namespaces. In the NEXT pr, I try to clean up some of the autograd codegen to stop using raw strings to match against C++ types.

Test Plan: Imported from OSS

Reviewed By: bhosmer

Differential Revision: D27708349

Pulled By: bdhirsh

fbshipit-source-id: 56a4f81fc101795bcb9ee1f722121480fb2356ad
2021-04-16 11:43:06 -07:00
..
__init__.py Rewrite of ATen code generator (#42629) 2020-08-31 09:00:22 -07:00
autograd.py Return a CType instead of a string for returns, beef up CType (#55046) 2021-04-16 11:41:46 -07:00
cpp.py add C++ namespacing logic to ctypes (#55047) 2021-04-16 11:43:06 -07:00
dispatcher.py Return a CType instead of a string for returns, beef up CType (#55046) 2021-04-16 11:41:46 -07:00
meta.py Un-ignore F403 in .flake8 (#55838) 2021-04-13 09:24:07 -07:00
native.py add C++ namespacing logic to ctypes (#55047) 2021-04-16 11:43:06 -07:00
python.py add C++ namespacing logic to ctypes (#55047) 2021-04-16 11:43:06 -07:00
structured.py add C++ namespacing logic to ctypes (#55047) 2021-04-16 11:43:06 -07:00
translate.py add C++ namespacing logic to ctypes (#55047) 2021-04-16 11:43:06 -07:00
types.py add C++ namespacing logic to ctypes (#55047) 2021-04-16 11:43:06 -07:00