Repository navigation
[Tutorial] tutorial for tensorize - #1774
Conversation
anijain2305
left a comment
There was a problem hiding this comment.
Thanks @yzhliu for this tutorial. This is very helpful. I had so many questions when I was trying to use tensorize the first time. This tutorial is answering many of those questions.
| # | ||
| N, M, L = 1024, 512, 64 | ||
| A = tvm.placeholder((N, L), name='A') | ||
| B = tvm.placeholder((M, L), name='B') |
There was a problem hiding this comment.
Seems matrix B is transposed here. If not, A should be NxL and B should be LxM.
There was a problem hiding this comment.
Yes, it's A * B^T, maybe define it more clearly beforehand? or it is better to do A * B?
There was a problem hiding this comment.
I think a better definition will suffice here. I think it is necessary to have this here because it becomes easier later to map GEMV
| B = tvm.placeholder((M, L), name='B') | ||
| k = tvm.reduce_axis((0, L), name='k') | ||
| C = tvm.compute((N, M), lambda i, j: | ||
| tvm.sum(A[i, k] * B[j, k], axis=k), name='C') |
There was a problem hiding this comment.
If the matrix B is not supposed to be transposed, it will be B[k, j]
| ========= | ||
| **Author**: `Yizhi Liu <https://github.com/yzhliu>`_ | ||
|
|
||
| This is an introduction material on how to do tensorization in TVM. |
| as well as extend TVM to support new hardware architectures. | ||
|
|
||
| In this tutorial, we will demonstrate the usage of tensorization. | ||
| Notice that the examples are more for showing the functionality, |
There was a problem hiding this comment.
Rewrite suggestion - The purpose of this tutorial is to show the functionality and usage of tensorize instead of providing an efficient solution.
| ###################################################################### | ||
| # Schedule the Matmul | ||
| # ------------------- | ||
| # Now suppose we had an accelerator supports matrix-vector multiplication (GEMV) as a hardware primitive, |
There was a problem hiding this comment.
Rewrite suggestion - Now, suppose we have an accelerator that
| # Before scheduling the tensorization, we need to first define the intrinsic function for GEMV. | ||
| # It includes two parts, the first is computing definition of GEMV, | ||
| # TVM uses it to match the computing pattern in the original Matmul schedule. | ||
| # The second is the definition of how to execute GEMV on the device. |
There was a problem hiding this comment.
It might be more concrete to say the second portion is done in "intrin_func" if we are avoiding writing comments in the code.
| # Here :code:`tvm.decl_tensor_intrin` declares how to execute the computation :code:`c.op`. | ||
| # Our implementation simply takes the inputs and outputs, | ||
| # converts them to pointers and emit an external function call. | ||
| # Note that tensorization requires user to specify :code:`offset_factor`, |
There was a problem hiding this comment.
Thanks for adding the offset_factor description. It seems important but I was not able to fully understand the description. Maybe an example would be helpful here.
There was a problem hiding this comment.
yeah it is still not very clear given the description here. Actually offset_factor hasn't been used in current TVM for loading optimization, only CHECK(elem_offset % offset_factor == 0) is inserted into the codegen. I'm thinking about adding more details in the Buffer doc.
| b = np.random.uniform(size=get_const_tuple(B.shape)).astype(dtype) | ||
| c = tvm.nd.array(np.zeros(get_const_tuple(C.shape), dtype=dtype), ctx) | ||
| func(tvm.nd.array(a, ctx), tvm.nd.array(b, ctx), c) | ||
| np.testing.assert_array_almost_equal(c.asnumpy(), np.dot(a, b.T), decimal=2) |
There was a problem hiding this comment.
Is numpy.testing.assert_allclose any better?
| cc.access_ptr("w"), aa.access_ptr("r"), bb.access_ptr("r"), | ||
| m, l, bb.strides[0])) | ||
| return ib.get() | ||
| return _instrin(0), _instrin(1), _instrin(2) |
There was a problem hiding this comment.
Here, I think we need more details here. We can rewrite the code to have 3 functions - reduce_reset, reduce_update and body. This will make things clearer. I think this is important for first-time users. I had the same questions when I was trying to use tensorize.
| b = np.random.uniform(size=get_const_tuple(B.shape)).astype(dtype) | ||
| c = tvm.nd.array(np.zeros(get_const_tuple(C.shape), dtype=dtype), ctx) | ||
| func(tvm.nd.array(a, ctx), tvm.nd.array(b, ctx), c) | ||
| np.testing.assert_array_almost_equal(c.asnumpy(), np.dot(a, b.T), decimal=2) |
| ib = tvm.ir_builder.create() | ||
| x = ib.pointer("float32") | ||
| with ib.for_range(1, 10, name="i") as i: | ||
| with ib.for_range(0, 10, name="i") as i: |
There was a problem hiding this comment.
here is an i-1 in below expression
| @@ -0,0 +1,276 @@ | |||
| """ | |||
| Use Tensorize to Leverage Hardware Intrinsics | |||
| ========= | |||
| # which can take arbitrary size of reduce axis, but another axis needs to be no larger than 16. | ||
| # Thus we break down the matmul loops to make the innermost loops a (16x64) GEMV. | ||
| # | ||
| s = tvm.create_schedule(C.op) |
There was a problem hiding this comment.
Adding another print of the IR here might be nice for comparison with the IR with the loops split.
| # - within the inner most two loops, the index :code:`i` is fixed, | ||
| # the access to the matrix :code:`A` only varies by :code:`k`, | ||
| # which makes the access pattern of :code:`A` a "vector". | ||
| # In order to leverage our "virtual" hardware's GEMV instruction, |
There was a problem hiding this comment.
Maybe use 'hypothetical hardware', if you're trying to emphasize this is for a imaginary backend.
| # Define GEMV Tensorization Intrinsic | ||
| # ----------------------------------- | ||
| # Before scheduling the tensorization, we need to first define the intrinsic function for GEMV. | ||
| # It includes two parts, the first is computing definition of GEMV, |
There was a problem hiding this comment.
I would reword to:
It includes two parts, the first is a compute definition of GEMV.
c1c563b to
d422f6c
Compare
|
Thanks @anijain2305 @ZihengJiang @merrymercy @cowanmeg Pls review again. |
| # | ||
| N, M, L = 1024, 512, 64 | ||
| A = tvm.placeholder((N, L), name='A') | ||
| B = tvm.placeholder((M, L), name='B') |
There was a problem hiding this comment.
I think a better definition will suffice here. I think it is necessary to have this here because it becomes easier later to map GEMV
ZihengJiang
left a comment
There was a problem hiding this comment.
Other parts look good to me
| # For now :code:`bb.strides[0] == l`, | ||
| # but later we will see how they can differ with more complicated schedules. | ||
| # | ||
| # Note that we use :code:`tvm.var("ldw")` as the first stride dimension for :code:`B`. |
There was a problem hiding this comment.
BTW, I am curious whether 'ldw' has any meaning.
There was a problem hiding this comment.
I change it to s1 for stride1 instead.
|
@yzhliu can you act on the final review comments? |
|
Thanks @yzhliu @cowanmeg @ZihengJiang @anijain2305 @merrymercy this is now merged! |
|
Hi, have we considered writing tutorial with jupyter notebook? I notice that most tutorials are interactive. By interactive I mean, read a document segment, run a code segment, check the result of that code segment, and go on. Jupyter notebook seems very good at this. |
|
It is a Jupyter notebook. For this one, check out https://docs.tvm.ai/tutorials/language/tensorize.html#sphx-glr-tutorials-language-tensorize-py, go to the end of the page, you will see |
|
Yes. I have seen the web page. Thank you :)
Yida Wang <notifications@github.com> 于2019年2月26日周二 下午2:46写道:
… It is a Jupyter notebook. For this one, check out
https://docs.tvm.ai/tutorials/language/tensorize.html#sphx-glr-tutorials-language-tensorize-py,
go to the end of the page, you will see Download Jupyter notebook:
tensorize.ipynb
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#1774 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AEtTjXH-iVykEMeYCQaW9cNh03KC-givks5vRNg9gaJpZM4W5x5P>
.
--
以上,祝好
王振华
|
@cowanmeg @anijain2305 @ZihengJiang @tqchen Pls help to review.