Triton Error [CUDA]: an illegal memory access was encountered

aifartist: <p>I’m trying to leverage torch.compile(model) in an existing largish application called AUTOMATIC1111(stable difusion inference).  When run with a compiled model I get:<br>
<code>RuntimeError: Triton Error [CUDA]: an illegal memory access was encountered</code><br>
Through instrumention I’ve found that it appears to create on the fly PY benchmark programs that are run to determine the best way to do something.  The function generated in my /tmp dir is:</p>
<pre><code class="lang-auto">@triton.jit
def triton_mm(in_ptr0, arg_A, arg_B, out_ptr0):
    &lt;body deleted for brevity&gt;
    tl.store(out_ptr0 + (xindex + tl.zeros(mask.shape, tl.int32)), tmp1, mask)
</code></pre>
<p>My guess is that this last line of the function is writting outside the bounds of the output variable.  If I try to print the 5 var’s used in tl.store() I get:</p>
<pre><code class="lang-auto">out_ptr0   pointer&lt;fp16&gt;[constexpr[1]]
xindex     int32[constexpr[32],constexpr[32]]
mask.shape [constexpr[32], constexpr[32]]
tmp1       fp32[constexpr[32],constexpr[32]]
mask       int1[constexpr[32],constexpr[32]]
</code></pre>
<p>I’m still new to python.  What are these?  I want to see the actual values so I can see if the index offset is crazy.  What was printed look like types instead of values.  I tried to print “out_ptr0[0]” and got a syntax error.  Is it an array?<br>
NOTE:<br>
print(f"xxx = {xxx}") doesn’t work giving an<br>
<code>    NotImplementedError: Unsupported node: JoinedStr</code><br>
error as if this is some restricted python that can only print simple strings and values.  Also print(type(out_ptr0)) doesn’t work.</p>

aifartist: <p>I discovered that Triton doesn’t  “support printing a variable”.<br>
Really, I’m not joking.  There’s an alternative syntax to print some things but it doesn’t always work and if you want to print something like type(var) or var.size() then you can forget about it.</p>
<p>But that just makes it more of a challenge.<br>
Flying blind, but I did stumble on select_algorithm.py call-&gt;run which I believe is actually invoking the external jit triton_mm function.  This is REAL Python code so I can instrument it without issues.</p>

ptrblck: <p>Cross-post from <a href="https://discuss.pytorch.org/t/runtimeerror-internal-triton-ptx-codegen-error-ptx-version-7-4-does-not-support-target-sm-89/170671/9">here</a> with a follow-up.<br>
Could you post a minimal and executable code snippet to debug the issue or grab the stacktrace from <code>cuda-gdb</code>, please?</p>

aifartist: <blockquote>
<p>Cross-post from <a href="https://discuss.pytorch.org/t/runtimeerror-internal-triton-ptx-codegen-error-ptx-version-7-4-does-not-support-target-sm-89/170671/9">here</a> with a follow-up.</p>
</blockquote>
<p>Is that a comment or a request?  What I put “there” is off topic for the codegen issue.  After I posted there I decide this belonged as a new issue.  All the info there is now here.  Are you asking me to maje a note there that this discussion is now here?  Sorry for being dense.</p>
<blockquote>
<p>Could you post a minimal and executable code snippet to debug the issue</p>
</blockquote>
<p>Simplifying 100 thousand lines of code I didn’t write isn’t easy.<br>
I did figure out how to create a stand alone call to jit’ed kernel executed triton_mm matching the real one that crashes.  But when I run it there isn’t a problem</p>
<p>I’ll see if cuda-gdb catches the error at the first point it finds it so I can get a stack.</p>

ptrblck: <p>It’s a comment so that other users can track the previous discussion and avoid re-debugging potentially already discussed issues.</p>
<p>Yes, creating a minimal code snippet is often the challenging part, but let me know in case you were able to narrow down the issue from the 100 thousand lines of code repo.</p>

aifartist: <p>If Triton had basic printf type debugging, the oldest kind there is, I could have figured this out hours ago.  I’m 98% sure this is writting outside the out_ptr0 boundaries.  But the tl.printf() work around the Triton folks gave me today doesn’t work.  xindex likely has bad values.  In fact I can get the same corruption error in my stand alone test by just adding a big offset to out_ptr0 in tl.store() but that isn’t a real repo.</p>
<p>However, here you go:</p>
<pre><code class="lang-auto">CUDA Exception: Warp Out-of-range Address
The exception was triggered at PC 0x7f88d360eb90
</code></pre>
<pre><code class="lang-auto">Thread 91 "python3" received signal CUDA_EXCEPTION_5, Warp Out-of-range Address.
[Switching focus to CUDA kernel 0, grid 22591, block (0,0,0), thread (128,0,0), device 0, sm 0, warp 4, lane 0]
0x00007f88d360ebb0 in triton_mm_0d1d2d3d&lt;&lt;&lt;(40,1,1),(256,1,1)&gt;&gt;&gt; ()
</code></pre>
<p>There is just the one line in the stack trace.  x/i $pc gives me:</p>
<pre><code class="lang-auto">    HMMA.16816.F32 R4, R12, R8, R4 
</code></pre>
<pre><code class="lang-auto">R4 = 0x411a7319
R8 = 0x3943bb2d
R12 = 0x305439af
</code></pre>
<p>Frustrating because I can dump the array of xindex’s in my stand alone test case but in the real deal of doing torch compile the printf hack doesn’t print anything.</p>

aifartist: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> Should I open an issue on pytorch github, even though I don’t yet have a simple repro.  I think I could debug this to root cause with a little help there.  I have a lot of context that someone might recognize given I can print the inputs to triton_mm and I have the generated source.  Currently my stand alone attempt uses torch random data of the same shape as I’ve found being passed in the REAL caller.  But I’m not passing the exact same Tensor’s.  If I knew how to export a Tensor in the big app, I could use them in my small test.</p>
<p>Or should this be a Triton discussion on github Triton???</p>
<p>But I’ll keep it here if you’d like me to wait till I have a repo.</p>

ptrblck: <p>Yes, I think creating a GitHub issue in the PyTorch repo is a good idea (please tag me there or post the link here once it’s done), as we could continue debugging it there.<br>
Also, I agree with your feedback on the bad debugging experience, but unfortunately I’m also not familiar enough in the Triton stack yet to be able to point to to an easy way of isolating these memory violations.</p>

aifartist: <p>Will do tomorrow.  Need to sleep.  Thanks.</p>

marksaroufim: <p><a class="mention" href="/u/aifartist">@aifartist</a> there is a workaround to print stuff in triton and here’s a relevant issue if you’d like something better <a href="https://github.com/openai/triton/issues/517" class="inline-onebox" rel="noopener nofollow ugc">How to debug kernels · Issue #517 · openai/triton · GitHub</a></p>