CNTK icon indicating copy to clipboard operation
CNTK copied to clipboard

UserFunction does not work in a loop

Open 5vision opened this issue 9 years ago • 1 comments

Here is the reproduction:

import numpy as np
from cntk.ops.functions import UserFunction
from cntk import output_variable, user_function, sequence, Axis, placeholder, sigmoid
from cntk.debugging import set_computation_network_trace_level

set_computation_network_trace_level(1)

class MyNode(UserFunction):
    def __init__(self, arg, name='MyNode'):
        super(MyNode, self).__init__([arg], name=name)

    def forward(self, argument, device=None, outputs_to_retain=None):
        return None, argument

    def backward(self, state, root_gradients):
        return root_gradients

    def infer_outputs(self):
        return [output_variable(self.inputs[0].shape, self.inputs[0].dtype, self.inputs[0].dynamic_axes)]

    @staticmethod
    def deserialize(inputs, name, state):
        return MyNode(inputs[0], name)

x = sequence.input_variable(shape=(42), sequence_axis=Axis('MySeq'))

h = placeholder(shape=x.shape, dynamic_axes=x.dynamic_axes)

p = x + h

#y = sigmoid(p)
y = user_function(MyNode(p))
#y = sequence.broadcast_as(y, x)

z = sequence.past_value(y)

y.replace_placeholders({h:z})

a = np.random.randn(10, 42).astype(np.float32)

r = y.eval(a)

which leads to the error:

Post-processing network...

2 roots:
	MySeq = DynamicAxis()
	UserDefinedFunction8 = UserDefinedV2Function()

Loop[0] --> Loop_UserDefinedFunction8 -> 3 nodes

	PastValue12	Plus5	UserDefinedFunction8

Validating network. 6 nodes to process in pass 1.

Validating --> MySeq = DynamicAxis() :  -> [1 x 1 x MySeq1]
Validating --> Input3 = InputValue() :  -> [42 x MySeq1]
Validating --> Constant11 = LearnableParameter() :  -> []
Validating --> PastValue12 = PastValue (UserDefinedFunction8, Constant11) : [], [] -> []
Validating --> Plus5 = Plus (Input3, PastValue12) : [42 x MySeq1], [] -> [42 x MySeq1]
Validating --> UserDefinedFunction8 = UserDefinedV2Function (Plus5) : [42 x MySeq1] -> [42 x MySeq2]

Validating network. 3 nodes to process in pass 2.

Validating --> PastValue12 = PastValue (UserDefinedFunction8, Constant11) : [42 x MySeq2], [] -> [42 x MySeq2]

Validating network. 1 nodes to process in pass 3.


Validating network, final pass.

WARNING: Node 'Plus5' (Plus operation): Dynamic axes mismatch between Node 'Input3' (InputValue operation) and Node 'PastValue12' (PastValue operation). If they are incompatible, this will fail later.
WARNING: Node 'Plus5' (Plus operation): Dynamic axes mismatch between Node 'Input3' (InputValue operation) and Node 'PastValue12' (PastValue operation). If they are incompatible, this will fail later.



Post-processing network complete.



Allocating matrices for forward and/or backward propagation.

Memory Sharing: Out of 6 matrices, 0 are shared as 0, and 6 are not shared.

Here are the ones that share memory:

Here are the ones that don't share memory:
	{MySeq : [1 x 1 x MySeq1]}
	{Constant11 : []}
	{Input3 : [42 x MySeq1]}
	{PastValue12 : [42 x MySeq2]}
	{Plus5 : [42 x MySeq1]}
	{UserDefinedFunction8 : [42 x MySeq2]}

Traceback (most recent call last):
<deleted>

RuntimeError: Evaluate: All nodes inside a recurrent loop must have a layout that is identical; mismatch found for nodes 'Plus5' ( x DynamicAxis1) vs. 'PastValue12' ( x MySeq2)

[CALL STACK]
[0x7f6a2709a24c]                                                       + 0x52d24c
[0x7f6a27359761]    Microsoft::MSR::CNTK::ComputationNetwork::SEQTraversalFlowControlNode::  BeginForwardProp  () + 0xd1
<deleted>

5vision avatar Jul 29 '17 13:07 5vision

Unfortunately this is currently by design, user defined functions cannot be part of the CNTK recurrent loop. I am closing this for now. For the next release (September) this work is not planned.

eldakms avatar Aug 23 '17 14:08 eldakms