Skip to content

[RFC] Encapsulate and standardize C++ Ops methods #2956

Closed
@datumbox

Description

@datumbox

🚀 Feature

Currently all the internal methods of the C++ Ops are public and this can lead to signature conflicts. To avoid the problem, we often use slightly different names for the same functionality across devices (nms_cpu_kernel vs nms_kernel). A better approach would be to encapsulate the methods and standardize the naming conventions across operators. Doing so will enforce good engineering standards, make it easier for first-time contributors to understand the code and potentially make it easier for us on the future to reduce the amount of duplicate/boilerplate code across Ops.

Motivation

While porting the C++ ops to use the dispatcher and autocast (#2796, #2797) I found it frustrating that the codebase looks substantially different from one operator to the other, the naming conventions are not standardized and that there is no separation between internal and public API methods. Moreover while making the changes, I've experienced some strange errors related to method signature conflicts.

Pitch

Naming Conventions:

  • Make the name of all operators lowercase to match the names of the Python Ops. For example: files, headers and methods containing DeformConv2d should become deform_conv2d
  • Ensure naming consistency within operators. Example: DeformConv.h should be deform_conv2d.h
  • Standardize the names of the public functions that the ops need to implement and make them consistent across devices. For example: *_forward_*, *_backward_* (partially done on previous PRs).

Encapsulation:

Alternatives

An alternative to anonymous namespaces could be refactoring the code to use classes and access modifiers. This will help us enforce the naming conventions more naturally (using abstract classes), nevertheless it's likely to increase complexity. This is because each operator receives different parameters and thus multiple Classes might be necessary per operator. Finally with an OO approach we might have performance considerations.

Note: It's worth noting that the changes introduced earlier for adopting the new PyTorch Dispatcher and Autocast introduced some BC breaking changes on the C++ methods. Thought these methods are not considered part of the public API, it's worth probably making all the necessary changes to the code before the release of the next stable version to ensure we won't affect our users twice.

cc @fmassa @cpuhrsch @vfdev-5 @mthrok

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions