Skip to content

Commit

Permalink
Add support for generating faithful at::cpu signatures (#51499)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #51499

I'm going to turn on at::cpu signatures on for all operators; before
I do it I want to make sure I'm at feature parity everywhere.

Signed-off-by: Edward Z. Yang <ezyang@fb.com>

Test Plan: Imported from OSS

Reviewed By: bhosmer

Differential Revision: D26187855

Pulled By: ezyang

fbshipit-source-id: 8fdfd9d843fc98435b1f1df8b475d3184d87dc96
  • Loading branch information
ezyang authored and facebook-github-bot committed Feb 3, 2021
1 parent 81c7c3b commit 333a0c8
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions tools/codegen/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,15 +401,16 @@ def gen_one(f: NativeFunction) -> Optional[str]:
# when CPUTensor class is a thing); nor do we generate fallback
# bindings for manual_cpp_binding functions.
cpp_sig_group = CppSignatureGroup.from_native_function(f, method=False, fallback_binding=False)
# TODO: generate faithful signatures too
cpp_sig = cpp_sig_group.signature

# Signature of the wrapper function we'll register to the dispatcher
sig = NativeSignature(f.func, prefix="wrapper_")

if self.target is Target.DECLARATION:
# namespace is handled by template
return f"TORCH_API {cpp_sig.decl()};\n"
result = f"TORCH_API {cpp_sig_group.signature.decl()};\n"
if cpp_sig_group.faithful_signature is not None:
result += f"TORCH_API {cpp_sig_group.faithful_signature.decl()};\n"
return result

elif self.target is Target.DEFINITION:

Expand Down Expand Up @@ -485,9 +486,7 @@ def gen_one(f: NativeFunction) -> Optional[str]:

# For an overview of what this template code looks like, see
# https://github.com/pytorch/rfcs/pull/9
return f"""\
namespace {{
sig_defn = f"""\
{self.gen_structured_class(
f, k,
class_name=class_name,
Expand All @@ -498,13 +497,25 @@ def gen_one(f: NativeFunction) -> Optional[str]:
{sig.defn()} {{
{sig_body_str}
}}
"""

}} // anonymous namespace
namespace {self.dispatch_key.lower()} {{
def generate_defn(cpp_sig: CppSignature) -> str:
return f"""
{cpp_sig.defn()} {{
return {sig.name()}({', '.join(e.expr for e in translate(cpp_sig.arguments(), sig.arguments()))});
}}
"""
cpp_defns = generate_defn(cpp_sig_group.signature)
if cpp_sig_group.faithful_signature is not None:
cpp_defns += generate_defn(cpp_sig_group.faithful_signature)

return f"""
namespace {{
{sig_defn}
}} // anonymous namespace
namespace {self.dispatch_key.lower()} {{
{cpp_defns}
}} // namespace {self.dispatch_key.lower()}
"""

Expand Down

0 comments on commit 333a0c8

Please sign in to comment.