Implementing lazy execution in tape
Created by: kexinzhao
In Tape, we want to provide the lazy evaluation feature, so that we can use the following code to compute the value of a variable:
auto loss = softmax(linear2(linear1(input)), label); // compile time InferShape & InferVarType
LOG(INFO) << loss.value(); // Run forward up to loss
When value()
is called upon a variable, the variable must know the location of the operator handle in the global tape so that tape run from the current op position up to the location of the target op handle.
There are two ways of adding the needed info in variable class for lazy exe:
-
Add a
int op_position_
field- Pros: tape is implemented as a vector of op handles and has a built-in current_position field. A
op_position_
field in variable is easy to implement and understand by comparing it with current_location and overall size of the tape. - Cons:
OpHandle
does not store its position in the tape. If we later want to do kernel fusion or something similar to change thevector<ophandle>
in tape, the position of the ops needed to updated for variables generated by many ops.
- Pros: tape is implemented as a vector of op handles and has a built-in current_position field. A
-
Add a
weak_ptr<OpHandle> op_
field. (we don't consider shared_ptr here because there maybe cyclic referencing issue between OpHandle and Variable)- Pros: If there is change in
vector<ophandle>
in tape, we mostly don't need to change Variable. - Cons:
- weak_ptr may occupy more memory than
size_t
. - Hard to implement and error prone: we need to use
while
to compare the current OPHandle and the target OpHandle and run it until we hit the target position, but what if the variable's correponding OP is before the current location or there is no such OpHandle in the current tape.
- weak_ptr may occupy more memory than
- Pros: If there is change in
Based on the above comparison, I plan to go with the first solution.