Skip to content

Conversation

@ysiraichi
Copy link
Collaborator

@ysiraichi ysiraichi commented Nov 17, 2025

This PR modifies clang-format configuration file, so that its include groups are tailored for PyTorch/XLA. In summary, the changes make it so includes in C++ files are grouped like:

/* System C Headers */
#include <unistd.h>

/* System C++ Headers */
#include <vector>

/* 
 * External Headers
 * - PyTorch Headers (e.g. torch/, c10/, and ATen/)
 * - abseil-cpp Headers
 * - OpenXLA Headers
 * 
 * Note that PyTorch headers should be included with angle-brackets so as to
 * keep consistency with PyTorch source code. This should also prevent a
 * header to be included twice.
 */
#include <torch/csrc/lazy/core/ir.h>
#include <c10/core/Scalar.h>
#include <ATen/ATen.h>
#include "absl/log/absl_check.h"
#include "xla/shape.h"

/* 
 * PyTorch/XLA Project Headers
 * - Source Headers
 * - Test Headers
 */
#include "torch_xla/csrc/tensor.h"
#include "test/cpp/cpp_test_util.h"

Key Changes:

  • Modified .clang-format so as to recognize the include groups mentioned above
  • Formatted all C++ files in the project
  • Made all PyTorch includes use angle-brackets

Update (Nov 18):

  • Merged "PyTorch Headers" with "External Headers"
  • Merged "Test Headers" with "PyTorch/XLA Source Headers"

Update (Nov 18):

  • Extract "PyTorch Headers" from "External Headers", making it into its own include group
    • Both PyTorch c10/util/logging_is_not_google_glog.h and abseil-cpp absl/log/log.h define a LOG() macro
    • Some include files from OpenXLA call that macro: if LOG() is redefined by PyTorch (i.e. it's included after abseil-cpp log file), and any of those OpenXLA files are included afterwards, they will use PyTorch LOG() definition rather than abseil-cpp. Raising the error.

Copy link
Collaborator

@zhanyong-wan zhanyong-wan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please conform to the Google style guide for include ordering: https://google.github.io/styleguide/cppguide.html#Names_and_Order_of_Includes

In particular:

In dir/foo.cc or dir/foo_test.cc, whose main purpose is to implement or test the stuff in dir2/foo2.h, order your includes as follows:

dir2/foo2.h.
A blank line
C system headers, and any other headers in angle brackets with the .h extension, e.g., <unistd.h>, <stdlib.h>, <Python.h>.
A blank line
C++ standard library headers (without file extension), e.g., <algorithm>, <cstddef>.
A blank line
Other libraries' .h files.
A blank line
Your project's .h files.

Separate each non-empty group with one blank line.

Within each section the includes should be ordered alphabetically.

Comment on lines +74 to +93
# 3. PyTorch headers:
# a) API headers: `#include <torch/*.h>`
# b) ATen headers: `#include <ATen/*.h>`
# c) c10 headers: `#include <c10/*.h>`
#
# Note that PyTorch headers should be included with angle-brackets so as to
# keep consistency with PyTorch source code. This should also prevent
# headers to be included twice because of different paths.
#
# Google C++ Style Guide dictates (3) and (4) should belong to the same
# group. However, this breaks PyTorch/XLA build. The problem is that both
# PyTorch and abseil-cpp (included by OpenXLA) define (among many) the
# `LOG()` macro. This leads to problems, such as OpenXLA functions calling
# PyTorch `LOG()` macro due to include order.
#
# In order to avoid such a problem, we make PyTorch into its own group,
# just after the system libraries. This works because including OpenXLA
# headers will redefine PyTorch `LOG()` macro, solving the problem above.
# This, however might fail one day, if PyTorch starts calling `LOG()` macro
# in one of the headers we include in PyTorch/XLA.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to move PyTorch headers to their own group, right after the C++ system libraries. Otherwise, we would get a build error where some OpenXLA files would call the LOG() macro defined by PyTorch, rather than the one defined by abseil-cpp. See the comment above for more details.

@ysiraichi ysiraichi merged commit 8f48082 into master Nov 19, 2025
20 checks passed
ysiraichi added a commit that referenced this pull request Dec 2, 2025
This is a follow-up from #9715.

This PR fixes the mentioned PR by adding both `ATen` and `c10` includes
to the `.clang-format` file. They were previously mentioned in that
file's comments, but were not added to the rules.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants