Skip to content

Conversation

@v0i0
Copy link
Contributor

@v0i0 v0i0 commented Nov 21, 2025

No description provided.

@fengxie
Copy link
Collaborator

fengxie commented Nov 24, 2025

@grypp @anakinxc to help review.

- The alignment is the maximum alignment of all objects
- The size is the maximum size of all objects
**Usage:**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usage looks really nice! It’s worth documenting this in an *.rst file. Maybe you could create a new file just for types and include the union there. We can fill in the rest of the regular types after your PT.

https://github.com/NVIDIA/cutlass/tree/main/media/docs/pythonDSL/cute_dsl_general/types.rst

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made claude draft it, someone's gotta look over it

self._align_of = max_alignment
self._size_of = struct.align_offset(max_size, max_alignment)

def __call__(self, base: Any) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def __call__(self, base: Any) -> None:
@dsl_user_op
def __call__(self, base: Any, *, loc=None, ip=None) -> None:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dsl_user_op does multiple things like generating location information. Let's add it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added. note that struct call does not have it, is that right?

if isinstance(obj, struct._AlignMeta):
obj = obj.dtype
if struct._is_scalar_type(obj):
new_obj = recast_ptr(base + off, dtype=obj)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
new_obj = recast_ptr(base + off, dtype=obj)
new_obj = recast_ptr(base + off, dtype=obj, loc=loc, ip=ip)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines +51 to +53
@cute.union
class BasicUnion:
as_int: cutlass.Int32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it's worth adding failing tests like

        @cute.union
        class BasicUnion:
            as_int: cutlass.Int32
            as_float  # no annotation 

@grypp
Copy link
Contributor

grypp commented Nov 24, 2025

The PR is in great shape — thanks for adding union support to our DSL. I just left a few minor comments.

Let's also wait @anakinxc's review

)
return cls

def size_in_bytes(self) -> int:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might also add

def __repr__(self) -> str:
    return f"<union {self._cls.__name__} size={self._size_of} align={self._align_of}>"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, not struct doesn't have a repr either btw

:return: The decorated union class.
"""

def __init__(self, cls):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably also implement __setitem__ for union to prevent user add new field after init?

Consider the following code

@cute.union
class value_union:
    as_int : cutlass.Int32
    as_float : cutlass.Float32

def foo():
   vu = value_union()
   vu.as_some_craziness = myStruct() # This should be rejected

   vu.__sizeof__() # what should be the size now?
   

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added. struct doesn't have that either btw

@@ -0,0 +1,190 @@
#################################################################################################
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brandon-yujie-sun @grypp @anakinxc seems we miss directory for testing :) Maybe we should consider add them. For this PR, I think it's okay to put it here.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, we should add test folder.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, it would be better to put it under test/python/CuTeDSL or something like that. cc @zekunf-nv whom also plans to add some tests to the repo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants