BayesNet/html/libtorch/include/c10/util/Exception.h.gcov.html

758 lines
80 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>LCOV - coverage.info - libtorch/include/c10/util/Exception.h</title>
<link rel="stylesheet" type="text/css" href="../../../../gcov.css">
</head>
<body>
<table width="100%" border=0 cellspacing=0 cellpadding=0>
<tr><td class="title">LCOV - code coverage report</td></tr>
<tr><td class="ruler"><img src="../../../../glass.png" width=3 height=3 alt=""></td></tr>
<tr>
<td width="100%">
<table cellpadding=1 border=0 width="100%">
<tr>
<td width="10%" class="headerItem">Current view:</td>
<td width="10%" class="headerValue"><a href="../../../../index.html">top level</a> - <a href="index.html">libtorch/include/c10/util</a> - Exception.h<span style="font-size: 80%;"> (source / <a href="Exception.h.func-c.html">functions</a>)</span></td>
<td width="5%"></td>
<td width="5%"></td>
<td width="5%" class="headerCovTableHead">Coverage</td>
<td width="5%" class="headerCovTableHead" title="Covered + Uncovered code">Total</td>
<td width="5%" class="headerCovTableHead" title="Exercised code only">Hit</td>
</tr>
<tr>
<td class="headerItem">Test:</td>
<td class="headerValue">coverage.info</td>
<td></td>
<td class="headerItem">Lines:</td>
<td class="headerCovTableEntryLo">0.0&nbsp;%</td>
<td class="headerCovTableEntry">11</td>
<td class="headerCovTableEntry">0</td>
</tr>
<tr>
<td class="headerItem">Test Date:</td>
<td class="headerValue">2024-04-30 13:17:26</td>
<td></td>
<td class="headerItem">Functions:</td>
<td class="headerCovTableEntryLo">0.0&nbsp;%</td>
<td class="headerCovTableEntry">8</td>
<td class="headerCovTableEntry">0</td>
</tr>
<tr><td><img src="../../../../glass.png" width=3 height=3 alt=""></td></tr>
</table>
</td>
</tr>
<tr><td class="ruler"><img src="../../../../glass.png" width=3 height=3 alt=""></td></tr>
</table>
<table cellpadding=0 cellspacing=0 border=0>
<tr>
<td><br></td>
</tr>
<tr>
<td>
<pre class="sourceHeading"> Line data Source code</pre>
<pre class="source">
<span id="L1"><span class="lineNum"> 1</span> : #ifndef C10_UTIL_EXCEPTION_H_</span>
<span id="L2"><span class="lineNum"> 2</span> : #define C10_UTIL_EXCEPTION_H_</span>
<span id="L3"><span class="lineNum"> 3</span> : </span>
<span id="L4"><span class="lineNum"> 4</span> : #include &lt;c10/macros/Macros.h&gt;</span>
<span id="L5"><span class="lineNum"> 5</span> : #include &lt;c10/util/StringUtil.h&gt;</span>
<span id="L6"><span class="lineNum"> 6</span> : #include &lt;c10/util/variant.h&gt;</span>
<span id="L7"><span class="lineNum"> 7</span> : </span>
<span id="L8"><span class="lineNum"> 8</span> : #include &lt;cstddef&gt;</span>
<span id="L9"><span class="lineNum"> 9</span> : #include &lt;exception&gt;</span>
<span id="L10"><span class="lineNum"> 10</span> : #include &lt;string&gt;</span>
<span id="L11"><span class="lineNum"> 11</span> : #include &lt;vector&gt;</span>
<span id="L12"><span class="lineNum"> 12</span> : </span>
<span id="L13"><span class="lineNum"> 13</span> : #if defined(_MSC_VER) &amp;&amp; _MSC_VER &lt;= 1900</span>
<span id="L14"><span class="lineNum"> 14</span> : #define __func__ __FUNCTION__</span>
<span id="L15"><span class="lineNum"> 15</span> : #endif</span>
<span id="L16"><span class="lineNum"> 16</span> : </span>
<span id="L17"><span class="lineNum"> 17</span> : namespace c10 {</span>
<span id="L18"><span class="lineNum"> 18</span> : </span>
<span id="L19"><span class="lineNum"> 19</span> : /// The primary ATen error class.</span>
<span id="L20"><span class="lineNum"> 20</span> : /// Provides a complete error message with source location information via</span>
<span id="L21"><span class="lineNum"> 21</span> : /// `what()`, and a more concise message via `what_without_backtrace()`.</span>
<span id="L22"><span class="lineNum"> 22</span> : /// Don't throw this directly; use TORCH_CHECK/TORCH_INTERNAL_ASSERT instead.</span>
<span id="L23"><span class="lineNum"> 23</span> : ///</span>
<span id="L24"><span class="lineNum"> 24</span> : /// NB: c10::Error is handled specially by the default torch to suppress the</span>
<span id="L25"><span class="lineNum"> 25</span> : /// backtrace, see torch/csrc/Exceptions.h</span>
<span id="L26"><span class="lineNum"> 26</span> : class C10_API Error : public std::exception {</span>
<span id="L27"><span class="lineNum"> 27</span> : // The actual error message.</span>
<span id="L28"><span class="lineNum"> 28</span> : std::string msg_;</span>
<span id="L29"><span class="lineNum"> 29</span> : </span>
<span id="L30"><span class="lineNum"> 30</span> : // Context for the message (in order of decreasing specificity). Context will</span>
<span id="L31"><span class="lineNum"> 31</span> : // be automatically formatted appropriately, so it is not necessary to add</span>
<span id="L32"><span class="lineNum"> 32</span> : // extra leading/trailing newlines to strings inside this vector</span>
<span id="L33"><span class="lineNum"> 33</span> : std::vector&lt;std::string&gt; context_;</span>
<span id="L34"><span class="lineNum"> 34</span> : </span>
<span id="L35"><span class="lineNum"> 35</span> : // The C++ backtrace at the point when this exception was raised. This</span>
<span id="L36"><span class="lineNum"> 36</span> : // may be empty if there is no valid backtrace. (We don't use optional</span>
<span id="L37"><span class="lineNum"> 37</span> : // here to reduce the dependencies this file has.)</span>
<span id="L38"><span class="lineNum"> 38</span> : std::string backtrace_;</span>
<span id="L39"><span class="lineNum"> 39</span> : </span>
<span id="L40"><span class="lineNum"> 40</span> : // These two are derived fields from msg_stack_ and backtrace_, but we need</span>
<span id="L41"><span class="lineNum"> 41</span> : // fields for the strings so that we can return a const char* (as the</span>
<span id="L42"><span class="lineNum"> 42</span> : // signature of std::exception requires). Currently, the invariant</span>
<span id="L43"><span class="lineNum"> 43</span> : // is that these fields are ALWAYS populated consistently with respect</span>
<span id="L44"><span class="lineNum"> 44</span> : // to msg_stack_ and backtrace_.</span>
<span id="L45"><span class="lineNum"> 45</span> : std::string what_;</span>
<span id="L46"><span class="lineNum"> 46</span> : std::string what_without_backtrace_;</span>
<span id="L47"><span class="lineNum"> 47</span> : </span>
<span id="L48"><span class="lineNum"> 48</span> : // This is a little debugging trick: you can stash a relevant pointer</span>
<span id="L49"><span class="lineNum"> 49</span> : // in caller, and then when you catch the exception, you can compare</span>
<span id="L50"><span class="lineNum"> 50</span> : // against pointers you have on hand to get more information about</span>
<span id="L51"><span class="lineNum"> 51</span> : // where the exception came from. In Caffe2, this is used to figure</span>
<span id="L52"><span class="lineNum"> 52</span> : // out which operator raised an exception.</span>
<span id="L53"><span class="lineNum"> 53</span> : const void* caller_;</span>
<span id="L54"><span class="lineNum"> 54</span> : </span>
<span id="L55"><span class="lineNum"> 55</span> : public:</span>
<span id="L56"><span class="lineNum"> 56</span> : // PyTorch-style Error constructor. NB: the implementation of this</span>
<span id="L57"><span class="lineNum"> 57</span> : // is actually in Logging.cpp</span>
<span id="L58"><span class="lineNum"> 58</span> : Error(SourceLocation source_location, std::string msg);</span>
<span id="L59"><span class="lineNum"> 59</span> : </span>
<span id="L60"><span class="lineNum"> 60</span> : // Caffe2-style error message</span>
<span id="L61"><span class="lineNum"> 61</span> : Error(</span>
<span id="L62"><span class="lineNum"> 62</span> : const char* file,</span>
<span id="L63"><span class="lineNum"> 63</span> : const uint32_t line,</span>
<span id="L64"><span class="lineNum"> 64</span> : const char* condition,</span>
<span id="L65"><span class="lineNum"> 65</span> : const std::string&amp; msg,</span>
<span id="L66"><span class="lineNum"> 66</span> : const std::string&amp; backtrace,</span>
<span id="L67"><span class="lineNum"> 67</span> : const void* caller = nullptr);</span>
<span id="L68"><span class="lineNum"> 68</span> : </span>
<span id="L69"><span class="lineNum"> 69</span> : // Base constructor</span>
<span id="L70"><span class="lineNum"> 70</span> : Error(std::string msg, std::string backtrace, const void* caller = nullptr);</span>
<span id="L71"><span class="lineNum"> 71</span> : </span>
<span id="L72"><span class="lineNum"> 72</span> : // Add some new context to the message stack. The last added context</span>
<span id="L73"><span class="lineNum"> 73</span> : // will be formatted at the end of the context list upon printing.</span>
<span id="L74"><span class="lineNum"> 74</span> : // WARNING: This method is O(n) in the size of the stack, so don't go</span>
<span id="L75"><span class="lineNum"> 75</span> : // wild adding a ridiculous amount of context to error messages.</span>
<span id="L76"><span class="lineNum"> 76</span> : void add_context(std::string msg);</span>
<span id="L77"><span class="lineNum"> 77</span> : </span>
<span id="L78"><span class="lineNum"> 78</span> : const std::string&amp; msg() const {</span>
<span id="L79"><span class="lineNum"> 79</span> : return msg_;</span>
<span id="L80"><span class="lineNum"> 80</span> : }</span>
<span id="L81"><span class="lineNum"> 81</span> : </span>
<span id="L82"><span class="lineNum"> 82</span> : const std::vector&lt;std::string&gt;&amp; context() const {</span>
<span id="L83"><span class="lineNum"> 83</span> : return context_;</span>
<span id="L84"><span class="lineNum"> 84</span> : }</span>
<span id="L85"><span class="lineNum"> 85</span> : </span>
<span id="L86"><span class="lineNum"> 86</span> : const std::string&amp; backtrace() const {</span>
<span id="L87"><span class="lineNum"> 87</span> : return backtrace_;</span>
<span id="L88"><span class="lineNum"> 88</span> : }</span>
<span id="L89"><span class="lineNum"> 89</span> : </span>
<span id="L90"><span class="lineNum"> 90</span> : /// Returns the complete error message, including the source location.</span>
<span id="L91"><span class="lineNum"> 91</span> : /// The returned pointer is invalidated if you call add_context() on</span>
<span id="L92"><span class="lineNum"> 92</span> : /// this object.</span>
<span id="L93"><span class="lineNum"> 93</span> <span class="tlaUNC tlaBgUNC"> 0 : const char* what() const noexcept override {</span></span>
<span id="L94"><span class="lineNum"> 94</span> <span class="tlaUNC"> 0 : return what_.c_str();</span></span>
<span id="L95"><span class="lineNum"> 95</span> : }</span>
<span id="L96"><span class="lineNum"> 96</span> : </span>
<span id="L97"><span class="lineNum"> 97</span> : const void* caller() const noexcept {</span>
<span id="L98"><span class="lineNum"> 98</span> : return caller_;</span>
<span id="L99"><span class="lineNum"> 99</span> : }</span>
<span id="L100"><span class="lineNum"> 100</span> : </span>
<span id="L101"><span class="lineNum"> 101</span> : /// Returns only the error message string, without source location.</span>
<span id="L102"><span class="lineNum"> 102</span> : /// The returned pointer is invalidated if you call add_context() on</span>
<span id="L103"><span class="lineNum"> 103</span> : /// this object.</span>
<span id="L104"><span class="lineNum"> 104</span> : const char* what_without_backtrace() const noexcept {</span>
<span id="L105"><span class="lineNum"> 105</span> : return what_without_backtrace_.c_str();</span>
<span id="L106"><span class="lineNum"> 106</span> : }</span>
<span id="L107"><span class="lineNum"> 107</span> : </span>
<span id="L108"><span class="lineNum"> 108</span> : private:</span>
<span id="L109"><span class="lineNum"> 109</span> : void refresh_what();</span>
<span id="L110"><span class="lineNum"> 110</span> : std::string compute_what(bool include_backtrace) const;</span>
<span id="L111"><span class="lineNum"> 111</span> : };</span>
<span id="L112"><span class="lineNum"> 112</span> : </span>
<span id="L113"><span class="lineNum"> 113</span> : class C10_API Warning {</span>
<span id="L114"><span class="lineNum"> 114</span> : public:</span>
<span id="L115"><span class="lineNum"> 115</span> : class C10_API UserWarning {};</span>
<span id="L116"><span class="lineNum"> 116</span> : class C10_API DeprecationWarning {};</span>
<span id="L117"><span class="lineNum"> 117</span> : </span>
<span id="L118"><span class="lineNum"> 118</span> : using warning_variant_t = c10::variant&lt;UserWarning, DeprecationWarning&gt;;</span>
<span id="L119"><span class="lineNum"> 119</span> : </span>
<span id="L120"><span class="lineNum"> 120</span> : Warning(</span>
<span id="L121"><span class="lineNum"> 121</span> : warning_variant_t type,</span>
<span id="L122"><span class="lineNum"> 122</span> : const SourceLocation&amp; source_location,</span>
<span id="L123"><span class="lineNum"> 123</span> : std::string msg,</span>
<span id="L124"><span class="lineNum"> 124</span> : bool verbatim);</span>
<span id="L125"><span class="lineNum"> 125</span> : </span>
<span id="L126"><span class="lineNum"> 126</span> : Warning(</span>
<span id="L127"><span class="lineNum"> 127</span> : warning_variant_t type,</span>
<span id="L128"><span class="lineNum"> 128</span> : SourceLocation source_location,</span>
<span id="L129"><span class="lineNum"> 129</span> : const char* msg,</span>
<span id="L130"><span class="lineNum"> 130</span> : bool verbatim);</span>
<span id="L131"><span class="lineNum"> 131</span> : </span>
<span id="L132"><span class="lineNum"> 132</span> : Warning(</span>
<span id="L133"><span class="lineNum"> 133</span> : warning_variant_t type,</span>
<span id="L134"><span class="lineNum"> 134</span> : SourceLocation source_location,</span>
<span id="L135"><span class="lineNum"> 135</span> : ::c10::detail::CompileTimeEmptyString msg,</span>
<span id="L136"><span class="lineNum"> 136</span> : bool verbatim);</span>
<span id="L137"><span class="lineNum"> 137</span> : </span>
<span id="L138"><span class="lineNum"> 138</span> : // Getters for members</span>
<span id="L139"><span class="lineNum"> 139</span> : warning_variant_t type() const;</span>
<span id="L140"><span class="lineNum"> 140</span> : const SourceLocation&amp; source_location() const;</span>
<span id="L141"><span class="lineNum"> 141</span> : const std::string&amp; msg() const;</span>
<span id="L142"><span class="lineNum"> 142</span> : bool verbatim() const;</span>
<span id="L143"><span class="lineNum"> 143</span> : </span>
<span id="L144"><span class="lineNum"> 144</span> : private:</span>
<span id="L145"><span class="lineNum"> 145</span> : // The type of warning</span>
<span id="L146"><span class="lineNum"> 146</span> : warning_variant_t type_;</span>
<span id="L147"><span class="lineNum"> 147</span> : </span>
<span id="L148"><span class="lineNum"> 148</span> : // Where the warning happened.</span>
<span id="L149"><span class="lineNum"> 149</span> : SourceLocation source_location_;</span>
<span id="L150"><span class="lineNum"> 150</span> : </span>
<span id="L151"><span class="lineNum"> 151</span> : // The actual warning message.</span>
<span id="L152"><span class="lineNum"> 152</span> : std::string msg_;</span>
<span id="L153"><span class="lineNum"> 153</span> : </span>
<span id="L154"><span class="lineNum"> 154</span> : // See note: [Verbatim Warnings]</span>
<span id="L155"><span class="lineNum"> 155</span> : bool verbatim_;</span>
<span id="L156"><span class="lineNum"> 156</span> : };</span>
<span id="L157"><span class="lineNum"> 157</span> : </span>
<span id="L158"><span class="lineNum"> 158</span> : using UserWarning = Warning::UserWarning;</span>
<span id="L159"><span class="lineNum"> 159</span> : using DeprecationWarning = Warning::DeprecationWarning;</span>
<span id="L160"><span class="lineNum"> 160</span> : </span>
<span id="L161"><span class="lineNum"> 161</span> : // Issue a warning with a given message. Dispatched to the current</span>
<span id="L162"><span class="lineNum"> 162</span> : // warning handler.</span>
<span id="L163"><span class="lineNum"> 163</span> : void C10_API warn(const Warning&amp; warning);</span>
<span id="L164"><span class="lineNum"> 164</span> : </span>
<span id="L165"><span class="lineNum"> 165</span> : class C10_API WarningHandler {</span>
<span id="L166"><span class="lineNum"> 166</span> : public:</span>
<span id="L167"><span class="lineNum"> 167</span> : virtual ~WarningHandler() = default;</span>
<span id="L168"><span class="lineNum"> 168</span> : /// The default warning handler. Prints the message to stderr.</span>
<span id="L169"><span class="lineNum"> 169</span> : virtual void process(const Warning&amp; warning);</span>
<span id="L170"><span class="lineNum"> 170</span> : };</span>
<span id="L171"><span class="lineNum"> 171</span> : </span>
<span id="L172"><span class="lineNum"> 172</span> : namespace WarningUtils {</span>
<span id="L173"><span class="lineNum"> 173</span> : </span>
<span id="L174"><span class="lineNum"> 174</span> : // Note: [Verbatim Warnings]</span>
<span id="L175"><span class="lineNum"> 175</span> : // Warnings originating in C++ code can appear out-of-place to Python users:</span>
<span id="L176"><span class="lineNum"> 176</span> : // a user runs a line in Python, but the warning references a line in C++.</span>
<span id="L177"><span class="lineNum"> 177</span> : // Some parts of PyTorch, like the JIT, are cognizant of this mismatch</span>
<span id="L178"><span class="lineNum"> 178</span> : // and take care to map warnings back to the user's program, but most</span>
<span id="L179"><span class="lineNum"> 179</span> : // of PyTorch simply throws a context-free warning. To allow warning</span>
<span id="L180"><span class="lineNum"> 180</span> : // handlers to add context where appropriate, warn takes the</span>
<span id="L181"><span class="lineNum"> 181</span> : // &quot;verbatim&quot; flag. When this is false a warning handler might append</span>
<span id="L182"><span class="lineNum"> 182</span> : // the C++ warning to a Python warning message that relates the warning</span>
<span id="L183"><span class="lineNum"> 183</span> : // back to the user's program. Callers who have already accounted for</span>
<span id="L184"><span class="lineNum"> 184</span> : // context in their warnings should set verbatim to true so their warnings</span>
<span id="L185"><span class="lineNum"> 185</span> : // appear without modification.</span>
<span id="L186"><span class="lineNum"> 186</span> : </span>
<span id="L187"><span class="lineNum"> 187</span> : /// Sets the global warning handler. This is not thread-safe, so it should</span>
<span id="L188"><span class="lineNum"> 188</span> : /// generally be called once during initialization or while holding the GIL</span>
<span id="L189"><span class="lineNum"> 189</span> : /// for programs that use python.</span>
<span id="L190"><span class="lineNum"> 190</span> : /// User is responsible for keeping the WarningHandler alive until</span>
<span id="L191"><span class="lineNum"> 191</span> : /// it is not needed.</span>
<span id="L192"><span class="lineNum"> 192</span> : C10_API void set_warning_handler(WarningHandler* handler) noexcept(true);</span>
<span id="L193"><span class="lineNum"> 193</span> : /// Gets the global warning handler.</span>
<span id="L194"><span class="lineNum"> 194</span> : C10_API WarningHandler* get_warning_handler() noexcept(true);</span>
<span id="L195"><span class="lineNum"> 195</span> : </span>
<span id="L196"><span class="lineNum"> 196</span> : class C10_API WarningHandlerGuard {</span>
<span id="L197"><span class="lineNum"> 197</span> : WarningHandler* prev_handler_;</span>
<span id="L198"><span class="lineNum"> 198</span> : </span>
<span id="L199"><span class="lineNum"> 199</span> : public:</span>
<span id="L200"><span class="lineNum"> 200</span> : WarningHandlerGuard(WarningHandler* new_handler)</span>
<span id="L201"><span class="lineNum"> 201</span> : : prev_handler_(c10::WarningUtils::get_warning_handler()) {</span>
<span id="L202"><span class="lineNum"> 202</span> : c10::WarningUtils::set_warning_handler(new_handler);</span>
<span id="L203"><span class="lineNum"> 203</span> : }</span>
<span id="L204"><span class="lineNum"> 204</span> : ~WarningHandlerGuard() {</span>
<span id="L205"><span class="lineNum"> 205</span> : c10::WarningUtils::set_warning_handler(prev_handler_);</span>
<span id="L206"><span class="lineNum"> 206</span> : }</span>
<span id="L207"><span class="lineNum"> 207</span> : };</span>
<span id="L208"><span class="lineNum"> 208</span> : </span>
<span id="L209"><span class="lineNum"> 209</span> : /// The TORCH_WARN_ONCE macro is difficult to test for. Use</span>
<span id="L210"><span class="lineNum"> 210</span> : /// setWarnAlways(true) to turn it into TORCH_WARN, which can be</span>
<span id="L211"><span class="lineNum"> 211</span> : /// tested for more easily.</span>
<span id="L212"><span class="lineNum"> 212</span> : C10_API void set_warnAlways(bool) noexcept(true);</span>
<span id="L213"><span class="lineNum"> 213</span> : C10_API bool get_warnAlways(void) noexcept(true);</span>
<span id="L214"><span class="lineNum"> 214</span> : </span>
<span id="L215"><span class="lineNum"> 215</span> : // A RAII guard that sets warn_always (not thread-local) on</span>
<span id="L216"><span class="lineNum"> 216</span> : // construction, and sets it back to the original value upon destruction.</span>
<span id="L217"><span class="lineNum"> 217</span> : struct C10_API WarnAlways {</span>
<span id="L218"><span class="lineNum"> 218</span> : public:</span>
<span id="L219"><span class="lineNum"> 219</span> : explicit WarnAlways(bool setting = true);</span>
<span id="L220"><span class="lineNum"> 220</span> : ~WarnAlways();</span>
<span id="L221"><span class="lineNum"> 221</span> : </span>
<span id="L222"><span class="lineNum"> 222</span> : private:</span>
<span id="L223"><span class="lineNum"> 223</span> : bool prev_setting;</span>
<span id="L224"><span class="lineNum"> 224</span> : };</span>
<span id="L225"><span class="lineNum"> 225</span> : </span>
<span id="L226"><span class="lineNum"> 226</span> : } // namespace WarningUtils</span>
<span id="L227"><span class="lineNum"> 227</span> : </span>
<span id="L228"><span class="lineNum"> 228</span> : // Used in ATen for out-of-bound indices that can reasonably only be detected</span>
<span id="L229"><span class="lineNum"> 229</span> : // lazily inside a kernel (See: advanced indexing). These turn into</span>
<span id="L230"><span class="lineNum"> 230</span> : // IndexError when they cross to Python.</span>
<span id="L231"><span class="lineNum"> 231</span> : class C10_API IndexError : public Error {</span>
<span id="L232"><span class="lineNum"> 232</span> : using Error::Error;</span>
<span id="L233"><span class="lineNum"> 233</span> : };</span>
<span id="L234"><span class="lineNum"> 234</span> : </span>
<span id="L235"><span class="lineNum"> 235</span> : // Used in ATen for invalid values. These turn into</span>
<span id="L236"><span class="lineNum"> 236</span> : // ValueError when they cross to Python.</span>
<span id="L237"><span class="lineNum"> 237</span> : class C10_API ValueError : public Error {</span>
<span id="L238"><span class="lineNum"> 238</span> : using Error::Error;</span>
<span id="L239"><span class="lineNum"> 239</span> : };</span>
<span id="L240"><span class="lineNum"> 240</span> : </span>
<span id="L241"><span class="lineNum"> 241</span> : // Used in ATen for invalid types. These turn into</span>
<span id="L242"><span class="lineNum"> 242</span> : // TypeError when they cross to Python.</span>
<span id="L243"><span class="lineNum"> 243</span> : class C10_API TypeError : public Error {</span>
<span id="L244"><span class="lineNum"> 244</span> : using Error::Error;</span>
<span id="L245"><span class="lineNum"> 245</span> : };</span>
<span id="L246"><span class="lineNum"> 246</span> : </span>
<span id="L247"><span class="lineNum"> 247</span> : // Used in ATen for functionality that is not implemented. These turn into</span>
<span id="L248"><span class="lineNum"> 248</span> : // NotImplementedError when they cross to Python.</span>
<span id="L249"><span class="lineNum"> 249</span> : class C10_API NotImplementedError : public Error {</span>
<span id="L250"><span class="lineNum"> 250</span> : using Error::Error;</span>
<span id="L251"><span class="lineNum"> 251</span> : };</span>
<span id="L252"><span class="lineNum"> 252</span> : </span>
<span id="L253"><span class="lineNum"> 253</span> : // Used in ATen for non finite indices. These turn into</span>
<span id="L254"><span class="lineNum"> 254</span> : // ExitException when they cross to Python.</span>
<span id="L255"><span class="lineNum"> 255</span> : class C10_API EnforceFiniteError : public Error {</span>
<span id="L256"><span class="lineNum"> 256</span> : using Error::Error;</span>
<span id="L257"><span class="lineNum"> 257</span> : };</span>
<span id="L258"><span class="lineNum"> 258</span> : </span>
<span id="L259"><span class="lineNum"> 259</span> : // Used in Onnxifi backend lowering. These turn into</span>
<span id="L260"><span class="lineNum"> 260</span> : // ExitException when they cross to Python.</span>
<span id="L261"><span class="lineNum"> 261</span> : class C10_API OnnxfiBackendSystemError : public Error {</span>
<span id="L262"><span class="lineNum"> 262</span> : using Error::Error;</span>
<span id="L263"><span class="lineNum"> 263</span> : };</span>
<span id="L264"><span class="lineNum"> 264</span> : </span>
<span id="L265"><span class="lineNum"> 265</span> : // Used for numerical errors from the linalg module. These</span>
<span id="L266"><span class="lineNum"> 266</span> : // turn into LinAlgError when they cross into Python.</span>
<span id="L267"><span class="lineNum"> 267</span> : class C10_API LinAlgError : public Error {</span>
<span id="L268"><span class="lineNum"> 268</span> : using Error::Error;</span>
<span id="L269"><span class="lineNum"> 269</span> : };</span>
<span id="L270"><span class="lineNum"> 270</span> : </span>
<span id="L271"><span class="lineNum"> 271</span> : class C10_API OutOfMemoryError : public Error {</span>
<span id="L272"><span class="lineNum"> 272</span> : using Error::Error;</span>
<span id="L273"><span class="lineNum"> 273</span> : };</span>
<span id="L274"><span class="lineNum"> 274</span> : </span>
<span id="L275"><span class="lineNum"> 275</span> : // Used for collective communication library errors from the distributed module.</span>
<span id="L276"><span class="lineNum"> 276</span> : // These turn into DistBackendError when they cross into Python.</span>
<span id="L277"><span class="lineNum"> 277</span> : class C10_API DistBackendError : public Error {</span>
<span id="L278"><span class="lineNum"> 278</span> : using Error::Error;</span>
<span id="L279"><span class="lineNum"> 279</span> : };</span>
<span id="L280"><span class="lineNum"> 280</span> : </span>
<span id="L281"><span class="lineNum"> 281</span> : // A utility function to return an exception std::string by prepending its</span>
<span id="L282"><span class="lineNum"> 282</span> : // exception type before its what() content</span>
<span id="L283"><span class="lineNum"> 283</span> : C10_API std::string GetExceptionString(const std::exception&amp; e);</span>
<span id="L284"><span class="lineNum"> 284</span> : </span>
<span id="L285"><span class="lineNum"> 285</span> : } // namespace c10</span>
<span id="L286"><span class="lineNum"> 286</span> : </span>
<span id="L287"><span class="lineNum"> 287</span> : // Private helper macro for implementing TORCH_INTERNAL_ASSERT and TORCH_CHECK</span>
<span id="L288"><span class="lineNum"> 288</span> : //</span>
<span id="L289"><span class="lineNum"> 289</span> : // Note: In the debug build With MSVC, __LINE__ might be of long type (a.k.a</span>
<span id="L290"><span class="lineNum"> 290</span> : // int32_t), which is different from the definition of `SourceLocation` that</span>
<span id="L291"><span class="lineNum"> 291</span> : // requires unsigned int (a.k.a uint32_t) and may cause a compile error with the</span>
<span id="L292"><span class="lineNum"> 292</span> : // message: error C2397: conversion from 'long' to 'uint32_t' requires a</span>
<span id="L293"><span class="lineNum"> 293</span> : // narrowing conversion Here the static cast is used to pass the build. if this</span>
<span id="L294"><span class="lineNum"> 294</span> : // is used inside a lambda the __func__ macro expands to operator(), which isn't</span>
<span id="L295"><span class="lineNum"> 295</span> : // very useful, but hard to fix in a macro so suppressing the warning.</span>
<span id="L296"><span class="lineNum"> 296</span> : #define C10_THROW_ERROR(err_type, msg) \</span>
<span id="L297"><span class="lineNum"> 297</span> : throw ::c10::err_type( \</span>
<span id="L298"><span class="lineNum"> 298</span> : {__func__, __FILE__, static_cast&lt;uint32_t&gt;(__LINE__)}, msg)</span>
<span id="L299"><span class="lineNum"> 299</span> : </span>
<span id="L300"><span class="lineNum"> 300</span> : // Private helper macro for workaround MSVC misexpansion of nested macro</span>
<span id="L301"><span class="lineNum"> 301</span> : // invocations involving __VA_ARGS__. See</span>
<span id="L302"><span class="lineNum"> 302</span> : // https://stackoverflow.com/questions/5134523/msvc-doesnt-expand-va-args-correctly</span>
<span id="L303"><span class="lineNum"> 303</span> : #define C10_EXPAND_MSVC_WORKAROUND(x) x</span>
<span id="L304"><span class="lineNum"> 304</span> : </span>
<span id="L305"><span class="lineNum"> 305</span> : // On nvcc, C10_UNLIKELY thwarts missing return statement analysis. In cases</span>
<span id="L306"><span class="lineNum"> 306</span> : // where the unlikely expression may be a constant, use this macro to ensure</span>
<span id="L307"><span class="lineNum"> 307</span> : // return statement analysis keeps working (at the cost of not getting the</span>
<span id="L308"><span class="lineNum"> 308</span> : // likely/unlikely annotation on nvcc).</span>
<span id="L309"><span class="lineNum"> 309</span> : // https://github.com/pytorch/pytorch/issues/21418</span>
<span id="L310"><span class="lineNum"> 310</span> : //</span>
<span id="L311"><span class="lineNum"> 311</span> : // Currently, this is only used in the error reporting macros below. If you</span>
<span id="L312"><span class="lineNum"> 312</span> : // want to use it more generally, move me to Macros.h</span>
<span id="L313"><span class="lineNum"> 313</span> : //</span>
<span id="L314"><span class="lineNum"> 314</span> : // TODO: Brian Vaughan observed that we might be able to get this to work on</span>
<span id="L315"><span class="lineNum"> 315</span> : // nvcc by writing some sort of C++ overload that distinguishes constexpr inputs</span>
<span id="L316"><span class="lineNum"> 316</span> : // from non-constexpr. Since there isn't any evidence that losing C10_UNLIKELY</span>
<span id="L317"><span class="lineNum"> 317</span> : // in nvcc is causing us perf problems, this is not yet implemented, but this</span>
<span id="L318"><span class="lineNum"> 318</span> : // might be an interesting piece of C++ code for an intrepid bootcamper to</span>
<span id="L319"><span class="lineNum"> 319</span> : // write.</span>
<span id="L320"><span class="lineNum"> 320</span> : #if defined(__CUDACC__)</span>
<span id="L321"><span class="lineNum"> 321</span> : #define C10_UNLIKELY_OR_CONST(e) e</span>
<span id="L322"><span class="lineNum"> 322</span> : #else</span>
<span id="L323"><span class="lineNum"> 323</span> : #define C10_UNLIKELY_OR_CONST(e) C10_UNLIKELY(e)</span>
<span id="L324"><span class="lineNum"> 324</span> : #endif</span>
<span id="L325"><span class="lineNum"> 325</span> : </span>
<span id="L326"><span class="lineNum"> 326</span> : // ----------------------------------------------------------------------------</span>
<span id="L327"><span class="lineNum"> 327</span> : // Error reporting macros</span>
<span id="L328"><span class="lineNum"> 328</span> : // ----------------------------------------------------------------------------</span>
<span id="L329"><span class="lineNum"> 329</span> : </span>
<span id="L330"><span class="lineNum"> 330</span> : #ifdef STRIP_ERROR_MESSAGES</span>
<span id="L331"><span class="lineNum"> 331</span> : #define TORCH_RETHROW(e, ...) throw</span>
<span id="L332"><span class="lineNum"> 332</span> : #else</span>
<span id="L333"><span class="lineNum"> 333</span> : #define TORCH_RETHROW(e, ...) \</span>
<span id="L334"><span class="lineNum"> 334</span> : do { \</span>
<span id="L335"><span class="lineNum"> 335</span> : e.add_context(::c10::str(__VA_ARGS__)); \</span>
<span id="L336"><span class="lineNum"> 336</span> : throw; \</span>
<span id="L337"><span class="lineNum"> 337</span> : } while (false)</span>
<span id="L338"><span class="lineNum"> 338</span> : #endif</span>
<span id="L339"><span class="lineNum"> 339</span> : </span>
<span id="L340"><span class="lineNum"> 340</span> : // A utility macro to provide assert()-like functionality; that is, enforcement</span>
<span id="L341"><span class="lineNum"> 341</span> : // of internal invariants in code. It supports an arbitrary number of extra</span>
<span id="L342"><span class="lineNum"> 342</span> : // arguments (evaluated only on failure), which will be printed in the assert</span>
<span id="L343"><span class="lineNum"> 343</span> : // failure message using operator&lt;&lt; (this is useful to print some variables</span>
<span id="L344"><span class="lineNum"> 344</span> : // which may be useful for debugging.)</span>
<span id="L345"><span class="lineNum"> 345</span> : //</span>
<span id="L346"><span class="lineNum"> 346</span> : // Usage:</span>
<span id="L347"><span class="lineNum"> 347</span> : // TORCH_INTERNAL_ASSERT(should_be_true);</span>
<span id="L348"><span class="lineNum"> 348</span> : // TORCH_INTERNAL_ASSERT(x == 0, &quot;x = &quot;, x);</span>
<span id="L349"><span class="lineNum"> 349</span> : //</span>
<span id="L350"><span class="lineNum"> 350</span> : // Assuming no bugs in PyTorch, the conditions tested by this macro should</span>
<span id="L351"><span class="lineNum"> 351</span> : // always be true; e.g., it should be possible to disable all of these</span>
<span id="L352"><span class="lineNum"> 352</span> : // conditions without changing observable user behavior. If you would like to</span>
<span id="L353"><span class="lineNum"> 353</span> : // do error reporting for user input, please use TORCH_CHECK instead.</span>
<span id="L354"><span class="lineNum"> 354</span> : //</span>
<span id="L355"><span class="lineNum"> 355</span> : // NOTE: It is SAFE to use this macro in production code; on failure, this</span>
<span id="L356"><span class="lineNum"> 356</span> : // simply raises an exception, it does NOT unceremoniously quit the process</span>
<span id="L357"><span class="lineNum"> 357</span> : // (unlike assert()).</span>
<span id="L358"><span class="lineNum"> 358</span> : //</span>
<span id="L359"><span class="lineNum"> 359</span> : #ifdef STRIP_ERROR_MESSAGES</span>
<span id="L360"><span class="lineNum"> 360</span> : #define TORCH_INTERNAL_ASSERT(cond, ...) \</span>
<span id="L361"><span class="lineNum"> 361</span> : if (C10_UNLIKELY_OR_CONST(!(cond))) { \</span>
<span id="L362"><span class="lineNum"> 362</span> : ::c10::detail::torchCheckFail( \</span>
<span id="L363"><span class="lineNum"> 363</span> : __func__, \</span>
<span id="L364"><span class="lineNum"> 364</span> : __FILE__, \</span>
<span id="L365"><span class="lineNum"> 365</span> : static_cast&lt;uint32_t&gt;(__LINE__), \</span>
<span id="L366"><span class="lineNum"> 366</span> : #cond &quot; INTERNAL ASSERT FAILED at &quot; C10_STRINGIZE(__FILE__)); \</span>
<span id="L367"><span class="lineNum"> 367</span> : }</span>
<span id="L368"><span class="lineNum"> 368</span> : #else</span>
<span id="L369"><span class="lineNum"> 369</span> : // It would be nice if we could build a combined string literal out of</span>
<span id="L370"><span class="lineNum"> 370</span> : // the TORCH_INTERNAL_ASSERT prefix and a user-provided string literal</span>
<span id="L371"><span class="lineNum"> 371</span> : // as the first argument, but there doesn't seem to be any good way to</span>
<span id="L372"><span class="lineNum"> 372</span> : // do that while still supporting having a first argument that isn't a</span>
<span id="L373"><span class="lineNum"> 373</span> : // string literal.</span>
<span id="L374"><span class="lineNum"> 374</span> : #define TORCH_INTERNAL_ASSERT(cond, ...) \</span>
<span id="L375"><span class="lineNum"> 375</span> : if (C10_UNLIKELY_OR_CONST(!(cond))) { \</span>
<span id="L376"><span class="lineNum"> 376</span> : ::c10::detail::torchInternalAssertFail( \</span>
<span id="L377"><span class="lineNum"> 377</span> : __func__, \</span>
<span id="L378"><span class="lineNum"> 378</span> : __FILE__, \</span>
<span id="L379"><span class="lineNum"> 379</span> : static_cast&lt;uint32_t&gt;(__LINE__), \</span>
<span id="L380"><span class="lineNum"> 380</span> : #cond \</span>
<span id="L381"><span class="lineNum"> 381</span> : &quot; INTERNAL ASSERT FAILED at &quot; C10_STRINGIZE(__FILE__) &quot;:&quot; C10_STRINGIZE( \</span>
<span id="L382"><span class="lineNum"> 382</span> : __LINE__) &quot;, please report a bug to PyTorch. &quot;, \</span>
<span id="L383"><span class="lineNum"> 383</span> : c10::str(__VA_ARGS__)); \</span>
<span id="L384"><span class="lineNum"> 384</span> : }</span>
<span id="L385"><span class="lineNum"> 385</span> : #endif</span>
<span id="L386"><span class="lineNum"> 386</span> : </span>
<span id="L387"><span class="lineNum"> 387</span> : // A utility macro to make it easier to test for error conditions from user</span>
<span id="L388"><span class="lineNum"> 388</span> : // input. Like TORCH_INTERNAL_ASSERT, it supports an arbitrary number of extra</span>
<span id="L389"><span class="lineNum"> 389</span> : // arguments (evaluated only on failure), which will be printed in the error</span>
<span id="L390"><span class="lineNum"> 390</span> : // message using operator&lt;&lt; (e.g., you can pass any object which has</span>
<span id="L391"><span class="lineNum"> 391</span> : // operator&lt;&lt; defined. Most objects in PyTorch have these definitions!)</span>
<span id="L392"><span class="lineNum"> 392</span> : //</span>
<span id="L393"><span class="lineNum"> 393</span> : // Usage:</span>
<span id="L394"><span class="lineNum"> 394</span> : // TORCH_CHECK(should_be_true); // A default error message will be provided</span>
<span id="L395"><span class="lineNum"> 395</span> : // // in this case; but we recommend writing an</span>
<span id="L396"><span class="lineNum"> 396</span> : // // explicit error message, as it is more</span>
<span id="L397"><span class="lineNum"> 397</span> : // // user friendly.</span>
<span id="L398"><span class="lineNum"> 398</span> : // TORCH_CHECK(x == 0, &quot;Expected x to be 0, but got &quot;, x);</span>
<span id="L399"><span class="lineNum"> 399</span> : //</span>
<span id="L400"><span class="lineNum"> 400</span> : // On failure, this macro will raise an exception. If this exception propagates</span>
<span id="L401"><span class="lineNum"> 401</span> : // to Python, it will convert into a Python RuntimeError.</span>
<span id="L402"><span class="lineNum"> 402</span> : //</span>
<span id="L403"><span class="lineNum"> 403</span> : // NOTE: It is SAFE to use this macro in production code; on failure, this</span>
<span id="L404"><span class="lineNum"> 404</span> : // simply raises an exception, it does NOT unceremoniously quit the process</span>
<span id="L405"><span class="lineNum"> 405</span> : // (unlike CHECK() from glog.)</span>
<span id="L406"><span class="lineNum"> 406</span> : //</span>
<span id="L407"><span class="lineNum"> 407</span> : #define TORCH_CHECK_WITH(error_t, cond, ...) \</span>
<span id="L408"><span class="lineNum"> 408</span> : TORCH_CHECK_WITH_MSG(error_t, cond, &quot;&quot;, __VA_ARGS__)</span>
<span id="L409"><span class="lineNum"> 409</span> : </span>
<span id="L410"><span class="lineNum"> 410</span> : #ifdef STRIP_ERROR_MESSAGES</span>
<span id="L411"><span class="lineNum"> 411</span> : #define TORCH_CHECK_MSG(cond, type, ...) \</span>
<span id="L412"><span class="lineNum"> 412</span> : (#cond #type &quot; CHECK FAILED at &quot; C10_STRINGIZE(__FILE__))</span>
<span id="L413"><span class="lineNum"> 413</span> : #define TORCH_CHECK_WITH_MSG(error_t, cond, type, ...) \</span>
<span id="L414"><span class="lineNum"> 414</span> : if (C10_UNLIKELY_OR_CONST(!(cond))) { \</span>
<span id="L415"><span class="lineNum"> 415</span> : C10_THROW_ERROR(Error, TORCH_CHECK_MSG(cond, type, __VA_ARGS__)); \</span>
<span id="L416"><span class="lineNum"> 416</span> : }</span>
<span id="L417"><span class="lineNum"> 417</span> : #else</span>
<span id="L418"><span class="lineNum"> 418</span> : namespace c10 {</span>
<span id="L419"><span class="lineNum"> 419</span> : namespace detail {</span>
<span id="L420"><span class="lineNum"> 420</span> : template &lt;typename... Args&gt;</span>
<span id="L421"><span class="lineNum"> 421</span> <span class="tlaUNC"> 0 : decltype(auto) torchCheckMsgImpl(const char* /*msg*/, const Args&amp;... args) {</span></span>
<span id="L422"><span class="lineNum"> 422</span> <span class="tlaUNC"> 0 : return ::c10::str(args...);</span></span>
<span id="L423"><span class="lineNum"> 423</span> : }</span>
<span id="L424"><span class="lineNum"> 424</span> <span class="tlaUNC"> 0 : inline C10_API const char* torchCheckMsgImpl(const char* msg) {</span></span>
<span id="L425"><span class="lineNum"> 425</span> <span class="tlaUNC"> 0 : return msg;</span></span>
<span id="L426"><span class="lineNum"> 426</span> : }</span>
<span id="L427"><span class="lineNum"> 427</span> : // If there is just 1 user-provided C-string argument, use it.</span>
<span id="L428"><span class="lineNum"> 428</span> <span class="tlaUNC"> 0 : inline C10_API const char* torchCheckMsgImpl(</span></span>
<span id="L429"><span class="lineNum"> 429</span> : const char* /*msg*/,</span>
<span id="L430"><span class="lineNum"> 430</span> : const char* args) {</span>
<span id="L431"><span class="lineNum"> 431</span> <span class="tlaUNC"> 0 : return args;</span></span>
<span id="L432"><span class="lineNum"> 432</span> : }</span>
<span id="L433"><span class="lineNum"> 433</span> : } // namespace detail</span>
<span id="L434"><span class="lineNum"> 434</span> : } // namespace c10</span>
<span id="L435"><span class="lineNum"> 435</span> : </span>
<span id="L436"><span class="lineNum"> 436</span> : #define TORCH_CHECK_MSG(cond, type, ...) \</span>
<span id="L437"><span class="lineNum"> 437</span> : (::c10::detail::torchCheckMsgImpl( \</span>
<span id="L438"><span class="lineNum"> 438</span> : &quot;Expected &quot; #cond \</span>
<span id="L439"><span class="lineNum"> 439</span> : &quot; to be true, but got false. &quot; \</span>
<span id="L440"><span class="lineNum"> 440</span> : &quot;(Could this error message be improved? If so, &quot; \</span>
<span id="L441"><span class="lineNum"> 441</span> : &quot;please report an enhancement request to PyTorch.)&quot;, \</span>
<span id="L442"><span class="lineNum"> 442</span> : ##__VA_ARGS__))</span>
<span id="L443"><span class="lineNum"> 443</span> : #define TORCH_CHECK_WITH_MSG(error_t, cond, type, ...) \</span>
<span id="L444"><span class="lineNum"> 444</span> : if (C10_UNLIKELY_OR_CONST(!(cond))) { \</span>
<span id="L445"><span class="lineNum"> 445</span> : C10_THROW_ERROR(error_t, TORCH_CHECK_MSG(cond, type, __VA_ARGS__)); \</span>
<span id="L446"><span class="lineNum"> 446</span> : }</span>
<span id="L447"><span class="lineNum"> 447</span> : #endif</span>
<span id="L448"><span class="lineNum"> 448</span> : </span>
<span id="L449"><span class="lineNum"> 449</span> : namespace c10 {</span>
<span id="L450"><span class="lineNum"> 450</span> : namespace detail {</span>
<span id="L451"><span class="lineNum"> 451</span> : </span>
<span id="L452"><span class="lineNum"> 452</span> : [[noreturn]] C10_API void torchCheckFail(</span>
<span id="L453"><span class="lineNum"> 453</span> : const char* func,</span>
<span id="L454"><span class="lineNum"> 454</span> : const char* file,</span>
<span id="L455"><span class="lineNum"> 455</span> : uint32_t line,</span>
<span id="L456"><span class="lineNum"> 456</span> : const std::string&amp; msg);</span>
<span id="L457"><span class="lineNum"> 457</span> : [[noreturn]] C10_API void torchCheckFail(</span>
<span id="L458"><span class="lineNum"> 458</span> : const char* func,</span>
<span id="L459"><span class="lineNum"> 459</span> : const char* file,</span>
<span id="L460"><span class="lineNum"> 460</span> : uint32_t line,</span>
<span id="L461"><span class="lineNum"> 461</span> : const char* msg);</span>
<span id="L462"><span class="lineNum"> 462</span> : </span>
<span id="L463"><span class="lineNum"> 463</span> : // The c10::str() call that creates userMsg can have 1 of 3 return</span>
<span id="L464"><span class="lineNum"> 464</span> : // types depending on the number and types of arguments passed to</span>
<span id="L465"><span class="lineNum"> 465</span> : // TORCH_INTERNAL_ASSERT. 0 arguments will get a</span>
<span id="L466"><span class="lineNum"> 466</span> : // CompileTimeEmptyString, 1 const char * will be passed straight</span>
<span id="L467"><span class="lineNum"> 467</span> : // through, and anything else will get converted to std::string.</span>
<span id="L468"><span class="lineNum"> 468</span> : [[noreturn]] C10_API void torchInternalAssertFail(</span>
<span id="L469"><span class="lineNum"> 469</span> : const char* func,</span>
<span id="L470"><span class="lineNum"> 470</span> : const char* file,</span>
<span id="L471"><span class="lineNum"> 471</span> : uint32_t line,</span>
<span id="L472"><span class="lineNum"> 472</span> : const char* condMsg,</span>
<span id="L473"><span class="lineNum"> 473</span> : const char* userMsg);</span>
<span id="L474"><span class="lineNum"> 474</span> <span class="tlaUNC"> 0 : [[noreturn]] inline C10_API void torchInternalAssertFail(</span></span>
<span id="L475"><span class="lineNum"> 475</span> : const char* func,</span>
<span id="L476"><span class="lineNum"> 476</span> : const char* file,</span>
<span id="L477"><span class="lineNum"> 477</span> : uint32_t line,</span>
<span id="L478"><span class="lineNum"> 478</span> : const char* condMsg,</span>
<span id="L479"><span class="lineNum"> 479</span> : ::c10::detail::CompileTimeEmptyString /*userMsg*/) {</span>
<span id="L480"><span class="lineNum"> 480</span> <span class="tlaUNC"> 0 : torchCheckFail(func, file, line, condMsg);</span></span>
<span id="L481"><span class="lineNum"> 481</span> : }</span>
<span id="L482"><span class="lineNum"> 482</span> : [[noreturn]] C10_API void torchInternalAssertFail(</span>
<span id="L483"><span class="lineNum"> 483</span> : const char* func,</span>
<span id="L484"><span class="lineNum"> 484</span> : const char* file,</span>
<span id="L485"><span class="lineNum"> 485</span> : uint32_t line,</span>
<span id="L486"><span class="lineNum"> 486</span> : const char* condMsg,</span>
<span id="L487"><span class="lineNum"> 487</span> : const std::string&amp; userMsg);</span>
<span id="L488"><span class="lineNum"> 488</span> : </span>
<span id="L489"><span class="lineNum"> 489</span> : } // namespace detail</span>
<span id="L490"><span class="lineNum"> 490</span> : } // namespace c10</span>
<span id="L491"><span class="lineNum"> 491</span> : </span>
<span id="L492"><span class="lineNum"> 492</span> : #ifdef STRIP_ERROR_MESSAGES</span>
<span id="L493"><span class="lineNum"> 493</span> : #define TORCH_CHECK(cond, ...) \</span>
<span id="L494"><span class="lineNum"> 494</span> : if (C10_UNLIKELY_OR_CONST(!(cond))) { \</span>
<span id="L495"><span class="lineNum"> 495</span> : ::c10::detail::torchCheckFail( \</span>
<span id="L496"><span class="lineNum"> 496</span> : __func__, \</span>
<span id="L497"><span class="lineNum"> 497</span> : __FILE__, \</span>
<span id="L498"><span class="lineNum"> 498</span> : static_cast&lt;uint32_t&gt;(__LINE__), \</span>
<span id="L499"><span class="lineNum"> 499</span> : TORCH_CHECK_MSG(cond, &quot;&quot;, __VA_ARGS__)); \</span>
<span id="L500"><span class="lineNum"> 500</span> : }</span>
<span id="L501"><span class="lineNum"> 501</span> : #else</span>
<span id="L502"><span class="lineNum"> 502</span> : #define TORCH_CHECK(cond, ...) \</span>
<span id="L503"><span class="lineNum"> 503</span> : if (C10_UNLIKELY_OR_CONST(!(cond))) { \</span>
<span id="L504"><span class="lineNum"> 504</span> : ::c10::detail::torchCheckFail( \</span>
<span id="L505"><span class="lineNum"> 505</span> : __func__, \</span>
<span id="L506"><span class="lineNum"> 506</span> : __FILE__, \</span>
<span id="L507"><span class="lineNum"> 507</span> : static_cast&lt;uint32_t&gt;(__LINE__), \</span>
<span id="L508"><span class="lineNum"> 508</span> : TORCH_CHECK_MSG(cond, &quot;&quot;, ##__VA_ARGS__)); \</span>
<span id="L509"><span class="lineNum"> 509</span> : }</span>
<span id="L510"><span class="lineNum"> 510</span> : #endif</span>
<span id="L511"><span class="lineNum"> 511</span> : </span>
<span id="L512"><span class="lineNum"> 512</span> : // An utility macro that does what `TORCH_CHECK` does if compiled in the host</span>
<span id="L513"><span class="lineNum"> 513</span> : // code, otherwise does nothing. Supposed to be used in the code shared between</span>
<span id="L514"><span class="lineNum"> 514</span> : // host and device code as an alternative for `TORCH_CHECK`.</span>
<span id="L515"><span class="lineNum"> 515</span> : #if defined(__CUDACC__) || defined(__HIPCC__)</span>
<span id="L516"><span class="lineNum"> 516</span> : #define TORCH_CHECK_IF_NOT_ON_CUDA(cond, ...)</span>
<span id="L517"><span class="lineNum"> 517</span> : #else</span>
<span id="L518"><span class="lineNum"> 518</span> : #define TORCH_CHECK_IF_NOT_ON_CUDA(cond, ...) TORCH_CHECK(cond, ##__VA_ARGS__)</span>
<span id="L519"><span class="lineNum"> 519</span> : #endif</span>
<span id="L520"><span class="lineNum"> 520</span> : </span>
<span id="L521"><span class="lineNum"> 521</span> : // Debug only version of TORCH_INTERNAL_ASSERT. This macro only checks in debug</span>
<span id="L522"><span class="lineNum"> 522</span> : // build, and does nothing in release build. It is appropriate to use</span>
<span id="L523"><span class="lineNum"> 523</span> : // in situations where you want to add an assert to a hotpath, but it is</span>
<span id="L524"><span class="lineNum"> 524</span> : // too expensive to run this assert on production builds.</span>
<span id="L525"><span class="lineNum"> 525</span> : #ifdef NDEBUG</span>
<span id="L526"><span class="lineNum"> 526</span> : // Optimized version - generates no code.</span>
<span id="L527"><span class="lineNum"> 527</span> : #define TORCH_INTERNAL_ASSERT_DEBUG_ONLY(...) \</span>
<span id="L528"><span class="lineNum"> 528</span> : while (false) \</span>
<span id="L529"><span class="lineNum"> 529</span> : C10_EXPAND_MSVC_WORKAROUND(TORCH_INTERNAL_ASSERT(__VA_ARGS__))</span>
<span id="L530"><span class="lineNum"> 530</span> : #else</span>
<span id="L531"><span class="lineNum"> 531</span> : #define TORCH_INTERNAL_ASSERT_DEBUG_ONLY(...) \</span>
<span id="L532"><span class="lineNum"> 532</span> : C10_EXPAND_MSVC_WORKAROUND(TORCH_INTERNAL_ASSERT(__VA_ARGS__))</span>
<span id="L533"><span class="lineNum"> 533</span> : #endif</span>
<span id="L534"><span class="lineNum"> 534</span> : </span>
<span id="L535"><span class="lineNum"> 535</span> : // TODO: We're going to get a lot of similar looking string literals</span>
<span id="L536"><span class="lineNum"> 536</span> : // this way; check if this actually affects binary size.</span>
<span id="L537"><span class="lineNum"> 537</span> : </span>
<span id="L538"><span class="lineNum"> 538</span> : // Like TORCH_CHECK, but raises LinAlgError instead of Error.</span>
<span id="L539"><span class="lineNum"> 539</span> : #define TORCH_CHECK_LINALG(cond, ...) \</span>
<span id="L540"><span class="lineNum"> 540</span> : TORCH_CHECK_WITH_MSG(LinAlgError, cond, &quot;LINALG&quot;, __VA_ARGS__)</span>
<span id="L541"><span class="lineNum"> 541</span> : </span>
<span id="L542"><span class="lineNum"> 542</span> : // Like TORCH_CHECK, but raises IndexErrors instead of Errors.</span>
<span id="L543"><span class="lineNum"> 543</span> : #define TORCH_CHECK_INDEX(cond, ...) \</span>
<span id="L544"><span class="lineNum"> 544</span> : TORCH_CHECK_WITH_MSG(IndexError, cond, &quot;INDEX&quot;, __VA_ARGS__)</span>
<span id="L545"><span class="lineNum"> 545</span> : </span>
<span id="L546"><span class="lineNum"> 546</span> : // Like TORCH_CHECK, but raises ValueErrors instead of Errors.</span>
<span id="L547"><span class="lineNum"> 547</span> : #define TORCH_CHECK_VALUE(cond, ...) \</span>
<span id="L548"><span class="lineNum"> 548</span> : TORCH_CHECK_WITH_MSG(ValueError, cond, &quot;VALUE&quot;, __VA_ARGS__)</span>
<span id="L549"><span class="lineNum"> 549</span> : </span>
<span id="L550"><span class="lineNum"> 550</span> : // Like TORCH_CHECK, but raises TypeErrors instead of Errors.</span>
<span id="L551"><span class="lineNum"> 551</span> : #define TORCH_CHECK_TYPE(cond, ...) \</span>
<span id="L552"><span class="lineNum"> 552</span> : TORCH_CHECK_WITH_MSG(TypeError, cond, &quot;TYPE&quot;, __VA_ARGS__)</span>
<span id="L553"><span class="lineNum"> 553</span> : </span>
<span id="L554"><span class="lineNum"> 554</span> : // Like TORCH_CHECK, but raises NotImplementedErrors instead of Errors.</span>
<span id="L555"><span class="lineNum"> 555</span> : #define TORCH_CHECK_NOT_IMPLEMENTED(cond, ...) \</span>
<span id="L556"><span class="lineNum"> 556</span> : TORCH_CHECK_WITH_MSG(NotImplementedError, cond, &quot;TYPE&quot;, __VA_ARGS__)</span>
<span id="L557"><span class="lineNum"> 557</span> : </span>
<span id="L558"><span class="lineNum"> 558</span> : #ifdef STRIP_ERROR_MESSAGES</span>
<span id="L559"><span class="lineNum"> 559</span> : #define WARNING_MESSAGE_STRING(...) \</span>
<span id="L560"><span class="lineNum"> 560</span> : ::c10::detail::CompileTimeEmptyString {}</span>
<span id="L561"><span class="lineNum"> 561</span> : #else</span>
<span id="L562"><span class="lineNum"> 562</span> : #define WARNING_MESSAGE_STRING(...) ::c10::str(__VA_ARGS__)</span>
<span id="L563"><span class="lineNum"> 563</span> : #endif</span>
<span id="L564"><span class="lineNum"> 564</span> : </span>
<span id="L565"><span class="lineNum"> 565</span> : // Report a warning to the user. Accepts an arbitrary number of extra</span>
<span id="L566"><span class="lineNum"> 566</span> : // arguments which are concatenated into the warning message using operator&lt;&lt;</span>
<span id="L567"><span class="lineNum"> 567</span> : //</span>
<span id="L568"><span class="lineNum"> 568</span> : #ifdef DISABLE_WARN</span>
<span id="L569"><span class="lineNum"> 569</span> : #define _TORCH_WARN_WITH(...) ((void)0);</span>
<span id="L570"><span class="lineNum"> 570</span> : #else</span>
<span id="L571"><span class="lineNum"> 571</span> : #define _TORCH_WARN_WITH(warning_t, ...) \</span>
<span id="L572"><span class="lineNum"> 572</span> : ::c10::warn(::c10::Warning( \</span>
<span id="L573"><span class="lineNum"> 573</span> : warning_t(), \</span>
<span id="L574"><span class="lineNum"> 574</span> : {__func__, __FILE__, static_cast&lt;uint32_t&gt;(__LINE__)}, \</span>
<span id="L575"><span class="lineNum"> 575</span> : WARNING_MESSAGE_STRING(__VA_ARGS__), \</span>
<span id="L576"><span class="lineNum"> 576</span> : false));</span>
<span id="L577"><span class="lineNum"> 577</span> : #endif</span>
<span id="L578"><span class="lineNum"> 578</span> : </span>
<span id="L579"><span class="lineNum"> 579</span> : #define TORCH_WARN(...) _TORCH_WARN_WITH(::c10::UserWarning, __VA_ARGS__);</span>
<span id="L580"><span class="lineNum"> 580</span> : </span>
<span id="L581"><span class="lineNum"> 581</span> : #define TORCH_WARN_DEPRECATION(...) \</span>
<span id="L582"><span class="lineNum"> 582</span> : _TORCH_WARN_WITH(::c10::DeprecationWarning, __VA_ARGS__);</span>
<span id="L583"><span class="lineNum"> 583</span> : </span>
<span id="L584"><span class="lineNum"> 584</span> : // Report a warning to the user only once. Accepts an arbitrary number of extra</span>
<span id="L585"><span class="lineNum"> 585</span> : // arguments which are concatenated into the warning message using operator&lt;&lt;</span>
<span id="L586"><span class="lineNum"> 586</span> : //</span>
<span id="L587"><span class="lineNum"> 587</span> : #define _TORCH_WARN_ONCE(...) \</span>
<span id="L588"><span class="lineNum"> 588</span> : C10_UNUSED static const auto C10_ANONYMOUS_VARIABLE(torch_warn_once_) = \</span>
<span id="L589"><span class="lineNum"> 589</span> : [&amp;] { \</span>
<span id="L590"><span class="lineNum"> 590</span> : TORCH_WARN(__VA_ARGS__); \</span>
<span id="L591"><span class="lineNum"> 591</span> : return true; \</span>
<span id="L592"><span class="lineNum"> 592</span> : }()</span>
<span id="L593"><span class="lineNum"> 593</span> : </span>
<span id="L594"><span class="lineNum"> 594</span> : #ifdef DISABLE_WARN</span>
<span id="L595"><span class="lineNum"> 595</span> : #define TORCH_WARN_ONCE(...) ((void)0);</span>
<span id="L596"><span class="lineNum"> 596</span> : #else</span>
<span id="L597"><span class="lineNum"> 597</span> : #define TORCH_WARN_ONCE(...) \</span>
<span id="L598"><span class="lineNum"> 598</span> : if (::c10::WarningUtils::get_warnAlways()) { \</span>
<span id="L599"><span class="lineNum"> 599</span> : TORCH_WARN(__VA_ARGS__); \</span>
<span id="L600"><span class="lineNum"> 600</span> : } else { \</span>
<span id="L601"><span class="lineNum"> 601</span> : _TORCH_WARN_ONCE(__VA_ARGS__); \</span>
<span id="L602"><span class="lineNum"> 602</span> : }</span>
<span id="L603"><span class="lineNum"> 603</span> : #endif</span>
<span id="L604"><span class="lineNum"> 604</span> : </span>
<span id="L605"><span class="lineNum"> 605</span> : // Report an error with a specific argument</span>
<span id="L606"><span class="lineNum"> 606</span> : // NOTE: using the argument name in TORCH_CHECK's message is preferred</span>
<span id="L607"><span class="lineNum"> 607</span> : #define TORCH_CHECK_ARG(cond, argN, ...) \</span>
<span id="L608"><span class="lineNum"> 608</span> : TORCH_CHECK(cond, &quot;invalid argument &quot;, argN, &quot;: &quot;, __VA_ARGS__)</span>
<span id="L609"><span class="lineNum"> 609</span> : </span>
<span id="L610"><span class="lineNum"> 610</span> : // ----------------------------------------------------------------------------</span>
<span id="L611"><span class="lineNum"> 611</span> : // Deprecated macros</span>
<span id="L612"><span class="lineNum"> 612</span> : // ----------------------------------------------------------------------------</span>
<span id="L613"><span class="lineNum"> 613</span> : </span>
<span id="L614"><span class="lineNum"> 614</span> : namespace c10 {</span>
<span id="L615"><span class="lineNum"> 615</span> : namespace detail {</span>
<span id="L616"><span class="lineNum"> 616</span> : </span>
<span id="L617"><span class="lineNum"> 617</span> : /*</span>
<span id="L618"><span class="lineNum"> 618</span> : // Deprecation disabled until we fix sites in our codebase</span>
<span id="L619"><span class="lineNum"> 619</span> : C10_DEPRECATED_MESSAGE(&quot;AT_ERROR(msg) is deprecated, use TORCH_CHECK(false, msg)</span>
<span id="L620"><span class="lineNum"> 620</span> : instead.&quot;)</span>
<span id="L621"><span class="lineNum"> 621</span> : */</span>
<span id="L622"><span class="lineNum"> 622</span> : inline void deprecated_AT_ERROR() {}</span>
<span id="L623"><span class="lineNum"> 623</span> : </span>
<span id="L624"><span class="lineNum"> 624</span> : /*</span>
<span id="L625"><span class="lineNum"> 625</span> : // Deprecation disabled until we fix sites in our codebase</span>
<span id="L626"><span class="lineNum"> 626</span> : C10_DEPRECATED_MESSAGE(&quot;AT_ASSERT is deprecated, if you mean to indicate an</span>
<span id="L627"><span class="lineNum"> 627</span> : internal invariant failure, use &quot; \</span>
<span id="L628"><span class="lineNum"> 628</span> : &quot;TORCH_INTERNAL_ASSERT instead; if you mean to do user</span>
<span id="L629"><span class="lineNum"> 629</span> : error checking, use &quot; \ &quot;TORCH_CHECK. See</span>
<span id="L630"><span class="lineNum"> 630</span> : https://github.com/pytorch/pytorch/issues/20287 for more details.&quot;)</span>
<span id="L631"><span class="lineNum"> 631</span> : */</span>
<span id="L632"><span class="lineNum"> 632</span> <span class="tlaUNC"> 0 : inline void deprecated_AT_ASSERT() {}</span></span>
<span id="L633"><span class="lineNum"> 633</span> : </span>
<span id="L634"><span class="lineNum"> 634</span> : /*</span>
<span id="L635"><span class="lineNum"> 635</span> : // Deprecation disabled until we fix sites in our codebase</span>
<span id="L636"><span class="lineNum"> 636</span> : C10_DEPRECATED_MESSAGE(&quot;AT_ASSERTM is deprecated, if you mean to indicate an</span>
<span id="L637"><span class="lineNum"> 637</span> : internal invariant failure, use &quot; \</span>
<span id="L638"><span class="lineNum"> 638</span> : &quot;TORCH_INTERNAL_ASSERT instead; if you mean to do user</span>
<span id="L639"><span class="lineNum"> 639</span> : error checking, use &quot; \ &quot;TORCH_CHECK. See</span>
<span id="L640"><span class="lineNum"> 640</span> : https://github.com/pytorch/pytorch/issues/20287 for more details.&quot;)</span>
<span id="L641"><span class="lineNum"> 641</span> : */</span>
<span id="L642"><span class="lineNum"> 642</span> : inline void deprecated_AT_ASSERTM() {}</span>
<span id="L643"><span class="lineNum"> 643</span> : </span>
<span id="L644"><span class="lineNum"> 644</span> : } // namespace detail</span>
<span id="L645"><span class="lineNum"> 645</span> : } // namespace c10</span>
<span id="L646"><span class="lineNum"> 646</span> : </span>
<span id="L647"><span class="lineNum"> 647</span> : // Deprecated alias; this alias was deprecated because people kept mistakenly</span>
<span id="L648"><span class="lineNum"> 648</span> : // using it for user error checking. Use TORCH_INTERNAL_ASSERT or TORCH_CHECK</span>
<span id="L649"><span class="lineNum"> 649</span> : // instead. See https://github.com/pytorch/pytorch/issues/20287 for more</span>
<span id="L650"><span class="lineNum"> 650</span> : // details.</span>
<span id="L651"><span class="lineNum"> 651</span> : #define AT_ASSERT(...) \</span>
<span id="L652"><span class="lineNum"> 652</span> : do { \</span>
<span id="L653"><span class="lineNum"> 653</span> : ::c10::detail::deprecated_AT_ASSERT(); \</span>
<span id="L654"><span class="lineNum"> 654</span> : C10_EXPAND_MSVC_WORKAROUND(TORCH_INTERNAL_ASSERT(__VA_ARGS__)); \</span>
<span id="L655"><span class="lineNum"> 655</span> : } while (false)</span>
<span id="L656"><span class="lineNum"> 656</span> : </span>
<span id="L657"><span class="lineNum"> 657</span> : // Deprecated alias, like AT_ASSERT. The new TORCH_INTERNAL_ASSERT macro</span>
<span id="L658"><span class="lineNum"> 658</span> : // supports both 0-ary and variadic calls, so having a separate</span>
<span id="L659"><span class="lineNum"> 659</span> : // message-accepting macro is not necessary.</span>
<span id="L660"><span class="lineNum"> 660</span> : //</span>
<span id="L661"><span class="lineNum"> 661</span> : // NB: we MUST include cond explicitly here, as MSVC will miscompile the macro</span>
<span id="L662"><span class="lineNum"> 662</span> : // expansion, shunting all of __VA_ARGS__ to cond. An alternate workaround</span>
<span id="L663"><span class="lineNum"> 663</span> : // can be seen at</span>
<span id="L664"><span class="lineNum"> 664</span> : // https://stackoverflow.com/questions/5134523/msvc-doesnt-expand-va-args-correctly</span>
<span id="L665"><span class="lineNum"> 665</span> : #define AT_ASSERTM(cond, ...) \</span>
<span id="L666"><span class="lineNum"> 666</span> : do { \</span>
<span id="L667"><span class="lineNum"> 667</span> : ::c10::detail::deprecated_AT_ASSERTM(); \</span>
<span id="L668"><span class="lineNum"> 668</span> : C10_EXPAND_MSVC_WORKAROUND(TORCH_INTERNAL_ASSERT(cond, __VA_ARGS__)); \</span>
<span id="L669"><span class="lineNum"> 669</span> : } while (false)</span>
<span id="L670"><span class="lineNum"> 670</span> : </span>
<span id="L671"><span class="lineNum"> 671</span> : // Deprecated alias; this alias was deprecated because it represents extra API</span>
<span id="L672"><span class="lineNum"> 672</span> : // surface that makes it hard for people to understand what macro to use.</span>
<span id="L673"><span class="lineNum"> 673</span> : // Use TORCH_CHECK(false, ...) or TORCH_INTERNAL_ASSERT(false, ...) to</span>
<span id="L674"><span class="lineNum"> 674</span> : // unconditionally fail at a line of code.</span>
<span id="L675"><span class="lineNum"> 675</span> : #define AT_ERROR(...) \</span>
<span id="L676"><span class="lineNum"> 676</span> : do { \</span>
<span id="L677"><span class="lineNum"> 677</span> : ::c10::detail::deprecated_AT_ERROR(); \</span>
<span id="L678"><span class="lineNum"> 678</span> : C10_EXPAND_MSVC_WORKAROUND(TORCH_CHECK(false, ::c10::str(__VA_ARGS__))); \</span>
<span id="L679"><span class="lineNum"> 679</span> : } while (false)</span>
<span id="L680"><span class="lineNum"> 680</span> : </span>
<span id="L681"><span class="lineNum"> 681</span> : #endif // C10_UTIL_EXCEPTION_H_</span>
</pre>
</td>
</tr>
</table>
<br>
<table width="100%" border=0 cellspacing=0 cellpadding=0>
<tr><td class="ruler"><img src="../../../../glass.png" width=3 height=3 alt=""></td></tr>
<tr><td class="versionInfo">Generated by: <a href="https://github.com//linux-test-project/lcov" target="_parent">LCOV version 2.0-1</a></td></tr>
</table>
<br>
</body>
</html>