• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <c10/core/TensorOptions.h>
2 
3 #include <c10/core/Device.h>
4 #include <c10/core/Layout.h>
5 #include <c10/util/Optional.h>
6 
7 #include <iostream>
8 
9 namespace c10 {
10 
11 // Note: TensorOptions properties are all optional, but (almost) all have
12 // getters that supply a default when the corresponding property is missing.
13 // Here we print the values returned by the default-supplying getters for
14 // properties that have them, along with an annotation if the value is
15 // returned by default. This gives the full picture of both the object's
16 // internal state and what its getters will return.
17 
operator <<(std::ostream & stream,const TensorOptions & options)18 std::ostream& operator<<(std::ostream& stream, const TensorOptions& options) {
19   auto print = [&](const char* label, auto prop, bool has_prop) {
20     stream << label << std::boolalpha << prop << (has_prop ? "" : " (default)");
21   };
22 
23   print("TensorOptions(dtype=", options.dtype(), options.has_dtype());
24   print(", device=", options.device(), options.has_device());
25   print(", layout=", options.layout(), options.has_layout());
26   print(
27       ", requires_grad=", options.requires_grad(), options.has_requires_grad());
28   print(
29       ", pinned_memory=", options.pinned_memory(), options.has_pinned_memory());
30 
31   // note: default-supplying memory_format() getter not provided; no canonical
32   // default
33   stream << ", memory_format=";
34   if (options.has_memory_format()) {
35     // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
36     stream << *options.memory_format_opt();
37   } else {
38     stream << "(nullopt)";
39   }
40   stream << ")";
41 
42   return stream;
43 }
44 
45 } // namespace c10
46