[PyTorch] Remove unnecessary iostream includes in headers (#61500)
authorScott Wolchok <swolchok@fb.com>
Fri, 20 Aug 2021 01:52:33 +0000 (18:52 -0700)
committerFacebook GitHub Bot <facebook-github-bot@users.noreply.github.com>
Fri, 20 Aug 2021 01:54:51 +0000 (18:54 -0700)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/61500

libstdc++ defines a static variable called `std::__ioinit` in iostream that adds global constructor size overhead to each translation that includes iostream. To reduce the size overhead from that, we can often include ostream instead.
ghstack-source-id: 136163529

Test Plan: buildsizebot some mobile apps

Reviewed By: dhruvbird

Differential Revision: D29648016

fbshipit-source-id: 9c3139712c71248513cc5032d21e77f3ecbae8fe

30 files changed:
aten/src/ATen/core/Formatting.cpp
aten/src/ATen/core/Formatting.h
aten/src/ATen/core/Vitals.cpp
aten/src/ATen/core/Vitals.h
aten/src/ATen/core/function_schema.cpp
aten/src/ATen/core/interned_strings_class.h
aten/src/ATen/core/ivalue.cpp
aten/src/ATen/core/jit_type.h
aten/src/ATen/cpu/vec/vec256/vec256.h
aten/src/ATen/cpu/vec/vec256/vec256_int.h
aten/src/ATen/cpu/vec/vec256/vec256_qint.h
aten/src/ATen/cpu/vml.h
aten/src/ATen/cudnn/Descriptors.cpp
aten/src/ATen/miopen/Descriptors.cpp
aten/src/ATen/native/quantized/cpu/qnnpack/include/pack_block_sparse.h
aten/src/ATen/native/quantized/cpu/qnnpack/src/pack_block_sparse.cc
c10/core/DispatchKey.h
c10/core/Layout.h
c10/core/MemoryFormat.h
c10/core/ScalarType.h
c10/util/Bitset.h
c10/util/complex.h
c10/util/either.h
c10/util/typeid.h
caffe2/core/init.cc
caffe2/core/operator.cc
caffe2/core/operator.h
caffe2/core/operator_schema.cc
caffe2/core/operator_schema.h
caffe2/operators/utility_ops.cc

index baf1691..dbbed6e 100644 (file)
@@ -232,6 +232,9 @@ void __printTensor(std::ostream& stream, Tensor& self, int64_t linesize)
   }
 }
 
+void print(const Tensor & t, int64_t linesize) {
+  print(std::cout,t,linesize);
+}
 std::ostream& print(std::ostream& stream, const Tensor & tensor_, int64_t linesize) {
   FormatGuard guard(stream);
   if(!tensor_.defined()) {
index 86ea603..55cfe7b 100644 (file)
@@ -2,7 +2,7 @@
 
 #include <c10/core/Scalar.h>
 #include <ATen/core/Tensor.h>
-#include <iostream>
+#include <ostream>
 
 
 namespace c10 {
@@ -18,9 +18,7 @@ TORCH_API std::ostream& print(
 static inline std::ostream& operator<<(std::ostream & out, const Tensor & t) {
   return print(out,t,80);
 }
-static inline void print(const Tensor & t, int64_t linesize=80) {
-  print(std::cout,t,linesize);
-}
+TORCH_API void print(const Tensor & t, int64_t linesize=80);
 
 static inline std::ostream& operator<<(std::ostream & out, Scalar s) {
   if (s.isFloatingPoint()) {
index edff521..76fc652 100644 (file)
@@ -1,5 +1,6 @@
 #include <ATen/core/Vitals.h>
 #include <cstdlib>
+#include <iostream>
 
 namespace at {
 namespace vitals {
index c64cf7e..48913c5 100644 (file)
@@ -1,8 +1,8 @@
 #pragma once
 #include <cstring>
-#include <iostream>
 #include <map>
 #include <memory>
+#include <ostream>
 #include <sstream>
 #include <unordered_map>
 
index cc6de61..a4319f0 100644 (file)
@@ -1,5 +1,7 @@
 #include <ATen/core/function_schema.h>
 
+#include <iostream>
+
 namespace c10 {
 
 void FunctionSchema::dump() const {
index 54303e0..8bbf329 100644 (file)
@@ -1,8 +1,6 @@
 #include <cstdint>
 #include <cstring>
-#include <iostream>
 #include <mutex>
-#include <sstream>
 #include <string>
 #include <unordered_map>
 #include <vector>
index 6fab54f..1404e01 100644 (file)
@@ -8,6 +8,7 @@
 #include <c10/util/StringUtil.h>
 #include <c10/util/hash.h>
 #include <cmath>
+#include <iostream>
 
 namespace c10 {
 bool _fastEqualsForContainer(const IValue& lhs, const IValue& rhs) {
index d733fbd..eee5aca 100644 (file)
@@ -9,10 +9,11 @@
 #include <c10/util/TypeList.h>
 #include <c10/util/Optional.h>
 
-#include <iostream>
+#include <array>
 #include <memory>
+#include <ostream>
+#include <sstream>
 #include <type_traits>
-#include <array>
 
 struct ClassType;
 namespace torch {
index 0d13458..906d8a8 100644 (file)
@@ -23,7 +23,7 @@
 #include <cstddef>
 #include <cstdint>
 #include <cstring>
-#include <iostream>
+#include <ostream>
 
 namespace at {
 namespace vec {
index ab8e1d0..5ee9919 100644 (file)
@@ -6,6 +6,7 @@
 #include <ATen/cpu/vec/intrinsics.h>
 #include <ATen/cpu/vec/vec_base.h>
 #include <c10/macros/Macros.h>
+#include <iostream>
 
 namespace at {
 namespace vec {
index b247d46..8cde485 100644 (file)
@@ -11,6 +11,7 @@
 #include <c10/util/quint8.h>
 
 #include <array>
+#include <iostream>
 
 // This file defines Vectorized<> for the quantized types.
 //
index b9cc47f..dbdef0b 100644 (file)
@@ -28,7 +28,6 @@
 #include <cstddef>
 #include <cstdint>
 #include <cstring>
-#include <iostream>
 #include <type_traits>
 
 #if AT_MKL_ENABLED() && !defined(__APPLE__)
index 873431c..f52280e 100644 (file)
@@ -2,7 +2,7 @@
 
 #include <ATen/ATen.h>
 
-#include <ostream>
+#include <iostream>
 #include <sstream>
 
 namespace at { namespace native {
index 6a64767..3887519 100644 (file)
@@ -1,6 +1,8 @@
 #include <ATen/miopen/Descriptors.h>
 #include <ATen/ATen.h>
 
+#include <iostream>
+
 namespace at { namespace native {
 
 namespace {
index 0f32929..62fdef2 100644 (file)
@@ -8,7 +8,6 @@
 
 #pragma once
 #include <cstdint>
-#include <iostream>
 #include <memory>
 #include <vector>
 #include <cassert>
@@ -33,25 +32,7 @@ typedef struct BCSRMatrix {
 #endif
   uint32_t col_block_size;  // input features block size
   uint32_t row_block_size;  // output features block size
-  void print() {
-    std::cout << "row block size:" << row_block_size << std::endl;
-    std::cout << "col block size:" << col_block_size << std::endl;
-    std::cout << "row ptr\n";
-    for (const auto& t : row_values) {
-      std::cout << t << ", ";
-    }
-    std::cout << std::endl;
-    std::cout << "col indices\n";
-    for (const auto& t : col_indices) {
-      std::cout << t << ", ";
-    }
-    std::cout << std::endl;
-    std::cout << "Actual values\n";
-    for (const auto& t : values) {
-      std::cout << (uint32_t)t << ", ";
-    }
-    std::cout << std::endl;
-  }
+  void print() const;
 } BCSRMatrix;
 
 std::unique_ptr<BCSRMatrix> generateBlockCSRMatrix(
index ca694df..6a61340 100644 (file)
@@ -6,6 +6,7 @@
  * LICENSE file in the root directory of this source tree.
  */
 #include <cassert>
+#include <iostream>
 
 #include <pack_block_sparse.h>
 
@@ -78,4 +79,24 @@ block_scanned:
   bcsr_mat.col_block_size = col_block_size;
   return bcsr_mat_ptr;
 }
+
+void BCSRMatrix::print() const {
+  std::cout << "row block size:" << row_block_size << std::endl;
+  std::cout << "col block size:" << col_block_size << std::endl;
+  std::cout << "row ptr\n";
+  for (const auto& t : row_values) {
+    std::cout << t << ", ";
+  }
+  std::cout << std::endl;
+  std::cout << "col indices\n";
+  for (const auto& t : col_indices) {
+    std::cout << t << ", ";
+  }
+  std::cout << std::endl;
+  std::cout << "Actual values\n";
+  for (const auto& t : values) {
+    std::cout << (uint32_t)t << ", ";
+  }
+  std::cout << std::endl;
+}
 } // namsepace qnnpack
index 9f21838..5b20a1c 100644 (file)
@@ -3,7 +3,7 @@
 #include <c10/macros/Macros.h>
 #include <c10/util/ArrayRef.h>
 #include <c10/util/Exception.h>
-#include <iostream>
+#include <ostream>
 #include <string>
 #include <vector>
 
index 44168eb..f37ceb1 100644 (file)
@@ -3,7 +3,7 @@
 #include <c10/core/Backend.h>
 #include <c10/util/Exception.h>
 
-#include <iostream>
+#include <ostream>
 
 namespace c10 {
 enum class Layout : int8_t { Strided, Sparse, SparseCsr, Mkldnn, NumOptions };
index ba4e056..8cafde1 100644 (file)
@@ -4,7 +4,7 @@
 #include <c10/util/ArrayRef.h>
 #include <c10/util/Exception.h>
 
-#include <iostream>
+#include <ostream>
 
 // Memory format is not the property of a Tensor. It is the way to tell an
 // operator how the result should be organized in memory and nothing more. That
index d652db5..f7b0710 100644 (file)
@@ -12,7 +12,7 @@
 
 #include <complex>
 #include <cstdint>
-#include <iostream>
+#include <ostream>
 
 namespace c10 {
 
index 6f7c4b9..bed04a4 100644 (file)
@@ -3,7 +3,6 @@
 #include <c10/macros/Macros.h>
 #include <c10/util/C++17.h>
 #include <c10/util/Optional.h>
-#include <iostream>
 #if defined(_MSC_VER)
 #include <intrin.h>
 #endif
index 2a565f8..67ed463 100644 (file)
@@ -1,7 +1,6 @@
 #pragma once
 
 #include <complex>
-#include <iostream>
 
 #include <c10/macros/Macros.h>
 
index da765b9..757663f 100644 (file)
@@ -6,7 +6,6 @@
 #include <c10/macros/Macros.h>
 #include <c10/util/C++17.h>
 #include <c10/util/Optional.h>
-#include <iostream>
 
 namespace c10 {
 /**
index e6a5822..240c69e 100644 (file)
@@ -4,7 +4,6 @@
 #include <cassert>
 #include <complex>
 #include <cstdlib>
-#include <iostream>
 #include <memory>
 #include <mutex>
 #include <type_traits>
index 5296658..bafbc82 100644 (file)
@@ -3,6 +3,7 @@
 #include "caffe2/core/scope_guard.h"
 
 #include <iomanip>
+#include <iostream>
 #include <mutex>
 
 C10_DEFINE_bool(
index 846ab8a..ca66f78 100644 (file)
@@ -1,6 +1,7 @@
 #include "caffe2/core/operator.h"
 
 #include <algorithm>
+#include <iostream>
 
 #include "caffe2/core/init.h"
 #include "caffe2/core/logging.h"
@@ -355,6 +356,17 @@ void SetOpEnginePref(
   }
 }
 
+DeviceTypeRegisterer::DeviceTypeRegisterer(DeviceType type, RegistryFunction func) {
+  if (gDeviceTypeRegistry()->count(type)) {
+    std::cerr << "Device type " << DeviceTypeName(type)
+              << "registered twice. This should not happen. Did you have "
+      "duplicated numbers assigned to different devices?";
+    std::exit(1);
+  }
+  // Calling the registry function to get the actual registry pointer.
+  gDeviceTypeRegistry()->emplace(type, func());
+}
+
 unique_ptr<OperatorBase> CreateOperator(
     const OperatorDef& operator_def,
     Workspace* ws,
index fc9a676..b840254 100644 (file)
@@ -1330,16 +1330,7 @@ typedef c10::Registry<
 TORCH_API std::map<DeviceType, OperatorRegistry*>* gDeviceTypeRegistry();
 
 struct TORCH_API DeviceTypeRegisterer {
-  explicit DeviceTypeRegisterer(DeviceType type, RegistryFunction func) {
-    if (gDeviceTypeRegistry()->count(type)) {
-      std::cerr << "Device type " << DeviceTypeName(type)
-                << "registered twice. This should not happen. Did you have "
-                   "duplicated numbers assigned to different devices?";
-      std::exit(1);
-    }
-    // Calling the registry function to get the actual registry pointer.
-    gDeviceTypeRegistry()->emplace(type, func());
-  }
+  explicit DeviceTypeRegisterer(DeviceType type, RegistryFunction func);
 };
 
 #if defined(_MSC_VER)
index fbfb8f4..29d0b3e 100644 (file)
@@ -1,6 +1,8 @@
 #include "caffe2/core/operator_schema.h"
 #include "caffe2/core/logging.h"
 
+#include <iostream>
+
 #include <c10/util/irange.h>
 
 namespace caffe2 {
@@ -520,6 +522,22 @@ C10_EXPORT std::ostream& operator<<(std::ostream& out, const OpSchema& schema) {
   return out;
 }
 
+OpSchema& OpSchemaRegistry::NewSchema(const string& key, const string& file, const int line) {
+  auto& m = map();
+  auto it = m.find(key);
+  if (it != m.end()) {
+    const auto& schema = it->second;
+    std::ios_base::Init init;
+    std::cerr << "Trying to register schema with name " << key
+              << " from file " << file << " line " << line
+              << ", but it is already registered from file " << schema.file()
+              << " line " << schema.line();
+    abort();
+  }
+  m.emplace(key, OpSchema(key, file, line));
+  return m[key];
+}
+
 CaffeMap<string, OpSchema>& OpSchemaRegistry::map() {
   static CaffeMap<string, OpSchema> map;
   return map;
index b19d5be..64f5ef3 100644 (file)
@@ -460,21 +460,7 @@ class TORCH_API OpSchema {
 class TORCH_API OpSchemaRegistry {
  public:
   static OpSchema&
-  NewSchema(const string& key, const string& file, const int line) {
-    auto& m = map();
-    auto it = m.find(key);
-    if (it != m.end()) {
-      const auto& schema = it->second;
-      std::ios_base::Init init;
-      std::cerr << "Trying to register schema with name " << key
-                << " from file " << file << " line " << line
-                << ", but it is already registered from file " << schema.file()
-                << " line " << schema.line();
-      abort();
-    }
-    m.emplace(key, OpSchema(key, file, line));
-    return m[key];
-  }
+  NewSchema(const string& key, const string& file, const int line);
 
   static const OpSchema* Schema(const string& key) {
     auto& m = map();
index 7b2a02f..8b5e116 100644 (file)
@@ -1,5 +1,6 @@
 #include "caffe2/operators/utility_ops.h"
 #include <cmath>
+#include <iostream>
 #include "caffe2/utils/eigen_utils.h"
 
 namespace caffe2 {