microsoft / onnxruntime-extensions

onnxruntime-extensions: A specialized pre- and post- processing library for ONNX Runtime
MIT License
330 stars 89 forks source link

how to register custom operator with attributes of a list? #785

Open CapJunkrat opened 2 months ago

CapJunkrat commented 2 months ago

In the provided documents, it showed an example of custom operator with attribute "padding_length", which has type int64. (code listed below.) What if this attribute is a list? How should I modify the example code to accommadate this change? Since attribute with list type is pretty common, such as the kernel of a conv op, I assume the list type attribute is already supported.

@onnx_op(op_type="GPT2Tokenizer",
            inputs=[PyCustomOpDef.dt_string],
            outputs=[PyCustomOpDef.dt_int64, PyCustomOpDef.dt_int64],
            attrs={"padding_length": PyCustomOpDef.dt_int64})
def bpe_tokenizer(s, **kwargs):
    padding_length = kwargs["padding_length"]
    input_ids, attention_mask = cls.tokenizer.tokenizer_sentence([s[0]], padding_length)
    return input_ids, attention_mask
CapJunkrat commented 2 months ago

@wenbingl I found that in the function cast_attributes, only int, float and string types are supported. Is it true that list of ints/floats is not supported?

wenbingl commented 1 month ago

@wenbingl I found that in the function cast_attributes, only int, float and string types are supported. Is it true that list of ints/floats is not supported?

Yes, if they are vector, they can be input as an ONNX initializer, i.e. a tensor-like type.

CapJunkrat commented 1 month ago

Yes, if they are vector, they can be input as an ONNX initializer, i.e. a tensor-like type.

@wenbingl What if the operator attribute need to be a vector? Since it is quite common to have vector attributes(such as conv kernel & paddings), will this feature be supported soon?