-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Add math operator patches #7688
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
Add math operator patches #7688
Conversation
Users can use `a+b`, `a*10`.
|
will this pr handle |
|
|
||
| def monkey_patch_variable(): | ||
| def new_name(): | ||
| return unique_name("tmp") |
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.
Any more meaningful name?
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.
Done.
| tmp_name = new_name() | ||
| var = block.create_var(name=tmp_name, shape=shape, dtype=dtype) | ||
| block.append_op( | ||
| type="fill_constant", |
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.
Why do we use fill_constant_op in create_tensor while fill_op in create_scalar?
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.
Cool
|
|
||
| def astype(self, dtype): | ||
| """ | ||
| Cast a variable to data type. |
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.
Cast a variable to a specified data type.
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.
Cool, thanks.
| # add fill_op to self.block | ||
| other_var = create_scalar( | ||
| self.block, value=other_var, dtype=lhs_dtype) | ||
|
|
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.
Maybe we shall add a type check here to make sure the rhs have been cast to Variable correctly. For some users may send np.array to the other_var.
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.
The create_scalar and create_tensor have checked the value can be cast to float.
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.
OK, I see.
| else: | ||
| other_var = create_tensor_with_batchsize( | ||
| self, other_var, lhs_dtype) | ||
| else: |
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.
Why does the other_var is cast to Variable by different methods in the two branches of if reverse:?
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.
The reverse operator in Python means the left-hand operand is a Python value and the right-hand operand is a Variable.
In the elementwise-operators, the right-hand operand can be broadcasted. Supposed we write an a - 10, every element in a will be minus 10. However, for 10 - a, we do not implement the broadcast in C++ operators. So we should explicitly broadcast 10 to the same shape of a.
Getting the shape of a variable has two situations.
- The variable is a parameter. Its shape is decided at compile time and not related to the batch size. So we use
fill_constant. - The variable is a layer output. Its shape is decided at runtime and related to the batch size. So we use
fill_constant_batch_size_like.
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.
Great idea! Impressive.
No, it won't. But following this PR, we can overload all operators in Python. We can give an API just like numpy. |
JiayiFeng
left a comment
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.
LGTM
Users can use
a+b,a*10.