Skip to content

Python Generators rearrange input and output arguments silently #7669

@Copper280z

Description

@Copper280z

It appears that python generators will rearrange inputs and outputs so that in the generated function call all input arguments come first, then output arguments, even if this is not the order defined in the generator class.

This may be intended behavior, but I can't find it called out anywhere in the examples or documentation, and it seems to me like an unintuitive result. It seems like either an addition to the documentation or a more descriptive error message would be a helpful addition. I think this would be caught more readily in a C++ setting because of static typing, Python tools can't easily catch this problem because it's dynamically typed. It's also not immediately obvious to me how to inspect the code generated by a generator, but I've only spent a few minutes looking.

Minimal example:

import numpy as np
import halide as hl

x = hl.Var("x")
y = hl.Var("y")

@hl.generator(name="testgen")
class testgen(hl.Generator):
    output = hl.OutputBuffer(hl.UInt(8), 2)  # output defined first
    offset = hl.InputScalar(hl.UInt(8))  # input defined second
    
    def generate(self):
        self.output[x,y] = self.offset


if __name__ == "__main__":
    inp = 3
    out = np.empty((256,256), dtype=np.uint8)
    t = hl.Target("host")
    with hl.GeneratorContext(t):
        gen = testgen()
        
        test_filter = gen.compile_to_callable()
        
        test_filter(inp, out)  # <- this works
        
        test_filter(out, inp)  # <- this fails

result:

  File ...example.py:26
    test_filter(out, 3)  # <- this fails

HalideError: Unable to cast Python instance to C++ type (compile in debug mode for details)

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions