-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Numpy compatible dtype inference for tvm.convert
and tvm.const
#3861
Conversation
I find that a large portion of the existing tests relies on the assumption that |
tvm.convert
and tvm.const
@tqchen Currently, I convert |
I think it is good. |
Wait a moment...my question is, why bother? It seems to me that one can always manually set up dtype without issue... |
@junrushao1994 |
I see. Is there a more general approach in dealing with this issue? |
@junrushao1994 I think for const and convert the current one should be general enough. It mainly deals with the situation when the dtype of the input is not know beforehand and we need to inference dtype base on the data. |
Sounds good :-) |
Just took a look at your errors with TFLite,
The stack trace here indicates that in the implementation of pooling in TOPI, there are some type mismatches (I would assume something like i32 vs i64). Could you fix this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did another round of review. Some nits and one issue:
I suggest not to make tvm.const
's dtype optional. Rather, let's do stricter check to confirm if dtype is correct.
@@ -86,7 +103,7 @@ def const(value, dtype=None): | |||
value : int or float | |||
The input value | |||
|
|||
dtype : str | |||
dtype : str or None, optional |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure which one would be better
dtype : str or None, optional | |
dtype : Optional[str] |
elif isinstance(value, float): | ||
# We intentionally convert the float to float32 since it's more common in DL. | ||
dtype = 'float32' | ||
elif isinstance(value, int): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you like to check overflow?
@@ -73,22 +74,24 @@ def max_value(dtype): | |||
return _api_internal._max_value(dtype) | |||
|
|||
|
|||
def const(value, dtype): | |||
def const(value, dtype=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, why make it optional? As a compiler, we should be more serious about which type we would feed into the IR framework.
Okay I chat with @sxjscience offline, and agreed that we should make the behavior of |
for (size_t i = 0; i < t->shape.size(); ++i) { | ||
if (i >= pad_before.size()) { | ||
output_shape.push_back(t->shape[i]); | ||
} else { | ||
output_shape.push_back( | ||
tvm::ir::Simplify(t->shape[i] + pad_before[i] + pad_after[i])); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find that there are some data type mismatch problems in the implementation of TOPI. For example, here, pad_before
and pad_after
can be int64, but the t->shape[i]
is int32
. Due to the automatic type conversion logic in https://github.com/dmlc/tvm/blob/e8c6adc6fb700c6dd181b5b3f059c135d5fee6d5/src/lang/expr_operator.cc#L39-L50, the t->shape[i] + pad_before[i] + pad_after[i]
will have dtype=int64
. This causes the output_shape
to have both int32
and int64
types and will cause some error in the generated llvm code.
out_shape.push_back(shape[0]); | ||
out_shape.push_back(shape[1]); | ||
out_shape.push_back(cast(Int(32), shape[0])); | ||
out_shape.push_back(cast(Int(32), shape[1])); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so these shape could be int64 when passed in?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, they can be int64 because we only constrain them to be Expr.
auto out_height = output_size[0]; | ||
auto out_width = output_size[1]; | ||
auto out_height = cast(Int(32), output_size[0]); | ||
auto out_width = cast(Int(32), output_size[1]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fyi @anijain2305 Is it good in quantization?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is good. We can cast back to x->dtype if it is not FP32 just before the divide factor.
…pache#3861) * numpy compatible type inference * update * try to fix * fix * try to fix * fix lint * Update nn.h * cast to int32 * try to fix * fix again * retrigger ci
…pache#3861) * numpy compatible type inference * update * try to fix * fix * try to fix * fix lint * Update nn.h * cast to int32 * try to fix * fix again * retrigger ci
…pache#3861) * numpy compatible type inference * update * try to fix * fix * try to fix * fix lint * Update nn.h * cast to int32 * try to fix * fix again * retrigger ci
Thanks for contributing to TVM! Please refer to guideline https://docs.tvm.ai/contribute/ for useful information and tips. After the pull request is submitted, please request code reviews from Reviewers.