gemini-code-assist[bot] commented on code in PR #410:
URL: https://github.com/apache/tvm-ffi/pull/410#discussion_r2687686498


##########
tests/release-extra/test_cute_dsl_compile.py:
##########
@@ -0,0 +1,56 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from __future__ import annotations
+
+import cutlass
+import pytest
+import torch
+from cutlass import cute
+
+
[email protected]
+def device_add_one(a: cute.Tensor) -> None:
+    a[0] += cutlass.Float16(1.0)
+
+
[email protected]
+def add_one(a: cute.Tensor) -> None:
+    device_add_one(a).launch(grid=(1, 1, 1), block=(1, 1, 1))
+
+
+def test_cute_dsl_compile() -> None:
+    dtype = cutlass.Float16
+    alignment_bytes = 16
+    divisibility = alignment_bytes * 8 // dtype.width
+
+    fake_tensor = cute.runtime.make_fake_compact_tensor(
+        dtype,
+        (cute.SymInt(), cute.SymInt(divisibility=divisibility)),
+        stride_order=(1, 0),
+        assumed_align=alignment_bytes,
+    )
+
+    compiled_add_one = cute.compile(add_one, fake_tensor, 
options="--enable-tvm-ffi")
+    # Pass in tensor with correct divisibility
+    a = torch.zeros((4, 16), device="cuda", dtype=torch.float16)
+    compiled_add_one(a)
+
+    # Pass in tensor with incorrect divisibility
+    with pytest.raises(ValueError):
+        b = torch.zeros((4, 18), device="cuda", dtype=torch.float16)
+        compiled_add_one(b)

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   This test is a good start, but it could be improved in two ways:
   
   1.  **Result Verification**: The test currently calls `compiled_add_one(a)` 
but doesn't verify that the tensor `a` is modified correctly. You should add an 
assertion to check that the kernel correctly adds 1 to the first element.
   2.  **CUDA Dependency**: This test requires a CUDA device. It's good 
practice to mark it so it's skipped on environments without CUDA. You can use 
`pytest.mark.skipif` for this.
   
   Here's a suggested implementation with these improvements:
   
   ```python
   @pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA not 
available")
   def test_cute_dsl_compile() -> None:
       dtype = cutlass.Float16
       alignment_bytes = 16
       divisibility = alignment_bytes * 8 // dtype.width
   
       fake_tensor = cute.runtime.make_fake_compact_tensor(
           dtype,
           (cute.SymInt(), cute.SymInt(divisibility=divisibility)),
           stride_order=(1, 0),
           assumed_align=alignment_bytes,
       )
   
       compiled_add_one = cute.compile(add_one, fake_tensor, 
options="--enable-tvm-ffi")
       # Pass in tensor with correct divisibility
       a = torch.zeros((4, 16), device="cuda", dtype=torch.float16)
       compiled_add_one(a)
   
       # Verify that the kernel correctly modified the tensor
       expected = torch.zeros((4, 16), device="cuda", dtype=torch.float16)
       expected[0, 0] = 1.0
       assert torch.equal(a, expected)
   
       # Pass in tensor with incorrect divisibility
       with pytest.raises(ValueError):
           b = torch.zeros((4, 18), device="cuda", dtype=torch.float16)
           compiled_add_one(b)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to