taichi-dev / taichi

Productive, portable, and performant GPU programming in Python.
https://taichi-lang.org
Apache License 2.0
25.54k stars 2.29k forks source link

Struct field with offset crashes #8554

Open VirgileUniv opened 4 months ago

VirgileUniv commented 4 months ago

Describe the bug when instaciating a taichi.Struct.field with shape and offset, taichi raise an exception error "shape cannot be None when offset is set"

To Reproduce

import taichi as ti
ti.init()

struct_field = ti.Struct.field(
    {"a": float}, 
    shape=(5,), 
    offset=(-1,)
)

Log/Screenshots

$ python .\sanboxxx.py
[Taichi] version 1.7.1, llvm 15.0.1, commit 0f143b2f, win, python 3.10.9
[Taichi] Starting on arch=x64
Traceback (most recent call last):
  File "C:\Users\MEEEE\Documents\M2_stage\internship-virgile-taichi\sanboxxx.py", line 4, in <module>
    struct_field = ti.Struct.field(
  File "C:\Users\MEEEE\Documents\M2_stage\internship-virgile-taichi\env\lib\site-packages\taichi\lang\util.py", line 334, in wrapped
    return func(*args, **kwargs)
  File "C:\Users\MEEEE\Documents\M2_stage\internship-virgile-taichi\env\lib\site-packages\taichi\lang\struct.py", line 305, in field
    field_dict[key] = impl.field(
  File "C:\Users\MEEEE\Documents\M2_stage\internship-virgile-taichi\env\lib\site-packages\taichi\lang\util.py", line 334, in wrapped
    return func(*args, **kwargs)
  File "C:\Users\MEEEE\Documents\M2_stage\internship-virgile-taichi\env\lib\site-packages\taichi\lang\impl.py", line 814, in field
    return _field(dtype, *args, **kwargs)
  File "C:\Users\MEEEE\Documents\M2_stage\internship-virgile-taichi\env\lib\site-packages\taichi\lang\util.py", line 334, in wrapped
    return func(*args, **kwargs)
  File "C:\Users\MEEEE\Documents\M2_stage\internship-virgile-taichi\env\lib\site-packages\taichi\lang\impl.py", line 733, in _field
    raise TaichiSyntaxError("shape cannot be None when offset is set")
taichi.lang.exception.TaichiSyntaxError: shape cannot be None when offset is set

Additional comments A simple fix seems to set the offset to None in the field classmethod of Struct :

class Struct:
    #...
    def field(
        #...
    ):
        #...
        for key, dtype in members.items():
            #...
            field_dict[key] = dtype.field(
                shape=None,
                name=field_name,
                offset=offset, ## <-- offset=None seems to make it work
                needs_grad=needs_grad,
                needs_dual=needs_dual,
            )
            #...
        #....