Skip to content

Commit

Permalink
add fmt
Browse files Browse the repository at this point in the history
fmt is a formatting library for C++. It has several properties that make it nice
for inclusion in PyTorch:
- Widely used
- Basically copies how Python does it
- Support for all the compilers and platforms we care about
- Standards track (C++20)
- Small code size
- Header only
- The author works at FB (hi @vitaut).

This PR includes it as a submodule and sets up the build. It also
provides some example usage, including a `TORCH_CHECK_FMT` macro in c10
that should test all the builds we care about.

ghstack-source-id: cbae4e082d4af37a3c0e34e69987dcbab41f57e6
Pull Request resolved: #37241
  • Loading branch information
suo committed Apr 26, 2020
1 parent b198796 commit cd07346
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,7 @@
ignore = dirty
path = third_party/XNNPACK
url = https://github.com/google/XNNPACK.git
[submodule "third_party/fmt"]
ignore = dirty
path = third_party/fmt
url = https://github.com/fmtlib/fmt.git
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -1738,6 +1738,7 @@ cc_library(
"@foxi",
"@gloo",
"@onnx",
"@fmt",
] + if_cuda(
[
":caffe2_cpp_cuda",
Expand Down
6 changes: 6 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ new_local_repository(
path = "third_party/sleef",
)

new_local_repository(
name = "fmt",
build_file = "//third_party:fmt.BUILD",
path = "third_party/fmt",
)

new_patched_local_repository(
name = "tbb",
patches = [
Expand Down
27 changes: 26 additions & 1 deletion cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ if(USE_FBGEMM)
caffe2_update_option(USE_FBGEMM ON)
else()
caffe2_update_option(USE_FBGEMM OFF)
message(WARNING
message(WARNING
"Turning USE_FAKELOWP off as it depends on USE_FBGEMM.")
caffe2_update_option(USE_FAKELOWP OFF)
endif()
Expand Down Expand Up @@ -1552,3 +1552,28 @@ endif()
#
# End ATen checks
#

# Override install location of `fmt` libs.
#
# `fmt` uses CMAKE_INSTALL_LIBDIR to determine where to put libraries. PyTorch
# somewhat misbehaves and installs everything to TORCH_INSTALL_LIB_DIR. For
# consistency, make `fmt` do the same thing by temporarily overriding
# CMAKE_INSTALL_LIBDIR.
set(OLD_CMAKE_INSTALL_LIBDIR ${CMAKE_INSTALL_LIBDIR})
set(CMAKE_INSTALL_LIBDIR ${TORCH_INSTALL_LIB_DIR})

set(FMT_INSTALL ON CACHE BOOL " " FORCE)
add_subdirectory(${CMAKE_SOURCE_DIR}/third_party/fmt)

set(CMAKE_INSTALL_LIBDIR ${OLD_CMAKE_INSTALL_LIBDIR})

# Disable compiler feature checks for `fmt`.
#
# CMake compiles a little program to check compiler features. Some of our build
# configurations (notably the mobile build analyzer) will populate
# CMAKE_CXX_FLAGS in ways that break feature checks. Since we already know
# `fmt` is compatible with a superset of the compilers that PyTorch is, it
# shouldn't be too bad to just disable the checks.
set_target_properties(fmt PROPERTIES INTERFACE_COMPILE_FEATURES "")

list(APPEND Caffe2_DEPENDENCY_LIBS fmt::fmt)
3 changes: 3 additions & 0 deletions cmake/TorchConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ if(NOT @BUILD_SHARED_LIBS@)
find_library(TORCH_CPU_LIBRARY torch_cpu PATHS "${TORCH_INSTALL_PREFIX}/lib")
list(APPEND TORCH_LIBRARIES ${TORCH_CPU_LIBRARY})

find_library(FMT_LIBRARY fmt PATHS "${TORCH_INSTALL_PREFIX}/lib")
list(APPEND TORCH_LIBRARIES ${FMT_LIBRARY})

if(@USE_NNPACK@)
find_library(NNPACK_LIBRARY nnpack PATHS "${TORCH_INSTALL_PREFIX}/lib")
list(APPEND TORCH_LIBRARIES ${NNPACK_LIBRARY})
Expand Down
2 changes: 1 addition & 1 deletion ios/TestApp/benchmark/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
target.resources_build_phase.add_file_reference(config_file_ref, true)
end
puts "Linking static libraries..."
libs = ['libc10.a', 'libclog.a', 'libnnpack.a', 'libXNNPACK.a', 'libeigen_blas.a', 'libcpuinfo.a', 'libpytorch_qnnpack.a', 'libtorch_cpu.a', 'libtorch.a']
libs = ['libc10.a', 'libclog.a', 'libnnpack.a', 'libXNNPACK.a', 'libeigen_blas.a', 'libcpuinfo.a', 'libpytorch_qnnpack.a', 'libtorch_cpu.a', 'libtorch.a', 'libfmt.a']
targets.each do |target|
target.frameworks_build_phases.clear
for lib in libs do
Expand Down
2 changes: 1 addition & 1 deletion scripts/xcode_build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

# link static libraries
target.frameworks_build_phases.clear
libs = ['libc10.a', 'libclog.a', 'libnnpack.a', 'libXNNPACK.a', 'libeigen_blas.a', 'libcpuinfo.a', 'libpytorch_qnnpack.a', 'libtorch_cpu.a', 'libtorch.a']
libs = ['libc10.a', 'libclog.a', 'libnnpack.a', 'libXNNPACK.a', 'libeigen_blas.a', 'libcpuinfo.a', 'libpytorch_qnnpack.a', 'libtorch_cpu.a', 'libtorch.a', 'libfmt.a']
for lib in libs do
path = "#{install_path}/lib/#{lib}"
if File.exist?(path)
Expand Down
1 change: 1 addition & 0 deletions third_party/fmt
Submodule fmt added at 9bdd15
9 changes: 9 additions & 0 deletions third_party/fmt.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
name = "fmt",
hdrs = glob(["include/fmt/*.h",]),
srcs = glob(["src/*.cc",]),
includes = ["include"],
visibility = ["//visibility:public"],
)
12 changes: 6 additions & 6 deletions torch/csrc/jit/serialization/import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <caffe2/serialize/istream_adapter.h>

#include <ATen/ATen.h>
#include <fmt/core.h>

#include <fstream>
#include <string>
Expand Down Expand Up @@ -44,12 +45,11 @@ void postSetStateValidate(const IValue& v) {
if (attrType->kind() != TypeKind::OptionalType) {
TORCH_CHECK(
!slot.isNone(),
"The field '",
attrName,
"' was left unitialized after __setstate__, but expected a ",
"value of type '",
attrType->python_str(),
"'");
fmt::format(
"The field '{}' was left uninitialized after '__setstate__', "
"but expected a value of type '{}'",
attrName,
attrType->python_str()));
}
}
}
Expand Down

0 comments on commit cd07346

Please sign in to comment.