Skip to content

Implement InplaceVariables with a reference

Created by: jmitrevs

This is an alternate of #469 to fix the issues caused by InplaceVariables and optimzers removing their reference. This is how I see it:

  • The implementation in the master branch creates the reference of an InplaceVariable when it is created. This runs into problems with optimizers changing the graph and making the reference no longer valid. Adding hooks to remove_node and replace_node seemed too much machinery for variables that are used only in Reshape layers.
  • The fix in #469 completely removes InplaceVariables and changes the shape of the preceding node's output. However, the cnn_mnist test showed that this isn't really safe to do, because function_cpp calls often make use of the output variable shape, so changing the shape breaks the code generation.
  • This attempt tries to localize the InplaceVariable code without adding more machinery. InplaceVariables become more like regular variables, but they have a blank definition_cpp. Instead the Reshape layer gets a function_cpp that makes the connection between the input at output variable at that stage, after the optimzers have run. The function_cpp basically becomes a typedef for the type and defines the output variable to be a referece to the input variable. Note, unlike the previous option, this allows for the shapes to differ while effectively becoming a nop in the code. Of course in io_stream cases the Reshape is often changed to a Repack node. This PR has no effect on that case.

Both the new test_reshape and the existing test_cnn_mnist tests succeed.

Let me know what you think. I left #469 open because I am not sure what is the best direction, though of course this and that are mutually exclusive.

Merge request reports

Loading