Adding recurrence support to user defined functions. This enables UDF to be called inside recurrent loops.

This commit is contained in:
Jaliya Ekanayake 2018-03-22 11:44:09 -07:00
Родитель 65961c9c19
Коммит 73c2046e88
3 изменённых файлов: 357 добавлений и 61 удалений

Просмотреть файл

@ -21,14 +21,12 @@ class OutputMultiplexerNode;
// of which can be part of a CNTK computation network.
// The actual implementation of the operation itself is external to the CNTK engine.
// -----------------------------------------------------------------------
// TODO: We currently only support external nodes that cannot be part of CNTK recurrent loops
template <class ElemType>
class UserDefinedV2FunctionNode final : public ComputationNodeNonLooping<ElemType>, public MultiOutputNode<ElemType>
class UserDefinedV2FunctionNode final : public ComputationNode<ElemType>, public MultiOutputNode<ElemType>
{
typedef ComputationNodeNonLooping<ElemType> Base; UsingComputationNodeMembersBoilerplate;
typedef ComputationNode<ElemType> Base; UsingComputationNodeMembersBoilerplate;
static const std::wstring TypeName() { return L"UserDefinedV2Function"; }
friend class OutputMultiplexerNode<ElemType>;
public:
@ -39,14 +37,24 @@ public:
LogicError("UserDefinedV2FunctionNode ctor should never be called with externalFunction == nullptr");
}
virtual bool ForceDynamicValidation() const override
virtual bool ForceDynamicValidation() const override
{
auto outputs = m_externalFunction->Outputs();
return std::any_of(outputs.begin(), outputs.end(), [](const ::CNTK::Variable& output) { return output.Shape().HasFreeDimension(); });
}
virtual void ForwardPropNonLooping() override
// This function is called in both PAR and SEQ modes of execution.
// In PAR mode, all frames are included at once and the MBLayout of the
// function defines the entire output.
// In the SEQ mode, we need to call UDF with input corresponding to each
// frame. The produced output also needs to be properly positioned in the
// final output matrix.
virtual void ForwardProp(const FrameRange& fr) override
{
bool inSEQMode = !fr.IsAllFrames();
// The first output value is set as this node's output. Others are mapped
// using OutputMultiplexerNode when creating the computation network.
this->m_outputsValue[0] = m_value;
// Get the arguments of the external function
@ -61,35 +69,73 @@ public:
continue;
auto argumentVar = arguments[j++];
// MBLayout and the frame has to point to the correct slice of the
// data in the SEQ mode. For PAR mode, this function is called
// only once with all frames.
MBLayoutPtr layout = make_shared<MBLayout>();
FrameRange inputFr = fr;
if (inSEQMode)
{
layout->InitAsFrameMode(inputFr.m_pMBLayout->GetNumParallelSequences());
}
else
{
layout = input.GetMBLayout();
inputFr = fr.WithLayout(input.GetMBLayout());
}
auto inputValueForFrame = input.ValueFor(inputFr);
auto argumentShape = ::CNTK::AsNDShape(input.GetSampleLayout());
auto argumentValue = ::CNTK::Utils::GetValueObjectFromCNTKImplMatrixAndMBLayout(argumentShape, argumentVar.DynamicAxes(), input.Value(), input.GetMBLayout());
// Get the argument value pointer for the provided frame.
auto argumentValue =
::CNTK::Utils::GetValueObjectFromCNTKImplMatrixAndMBLayout(
argumentShape,
argumentVar.DynamicAxes(),
inputValueForFrame, // only for the particular frame.
layout); // layout for the frame.
argumentValues.insert(std::make_pair(argumentVar, argumentValue));
}
assert(j == arguments.size());
auto outputs = m_externalFunction->Outputs();
// TODO: Instead of passing null for output values, we should have the forward call directly produce the outputs in the output Value() of this node
std::unordered_map<::CNTK::Variable, ::CNTK::ValuePtr> outputValues;
for (auto output : outputs)
outputValues.insert({output, nullptr});
{
outputValues.insert({ output, nullptr });
}
std::unordered_set<::CNTK::Variable> outputsToRetainBackwardStateFor;
if (Environment().IsTraining())
outputsToRetainBackwardStateFor.insert(outputs.begin(), outputs.end());
auto computeDevice = ::CNTK::AsDeviceDescriptor(InputRef(0).Value().GetDeviceId());
m_currentBackpropStatePtr = m_externalFunction->Forward(argumentValues, outputValues, computeDevice, outputsToRetainBackwardStateFor);
// Copy the computed output
m_currentBackpropStatePtr = m_externalFunction->Forward(
argumentValues,
outputValues,
computeDevice,
outputsToRetainBackwardStateFor);
// Copy the computed output to MultiOutputNode node.
for (size_t i = 0; i < outputs.size(); ++i)
{
auto output = outputs[i];
::CNTK::NDShape inferredVarShape;
auto outputMatrixAndLayout = ::CNTK::Utils::GetCNTKImplMatrixAndMBLayoutFromValueObject<ElemType>(output, outputValues[output], &inferredVarShape);
// Call this function to retrieve the computer output matrix.
// The shape is based on what we have provided in the forward.
auto outputMatrixAndLayout =
::CNTK::Utils::GetCNTKImplMatrixAndMBLayoutFromValueObject<ElemType>(
output,
outputValues[output],
&inferredVarShape);
if (inferredVarShape.IsUnknown() || inferredVarShape.HasUnboundDimension())
LogicError("The output shape '%S' of an external user defined Function '%S' must be fully defined.", inferredVarShape.AsString().c_str(), m_externalFunction->AsString().c_str());
LogicError("The output shape '%S' of an external user defined Function '%S' "
"must be fully defined.", inferredVarShape.AsString().c_str(),
m_externalFunction->AsString().c_str());
if (output.Shape().HasFreeDimension())
{
@ -98,7 +144,20 @@ public:
SetDims(this->m_outputsShape[i], HasMBLayout());
}
this->m_outputsValue[i]->SetValue(*outputMatrixAndLayout.first);
if (inSEQMode)
{
// Replace only a column of the output value corresponding to the
// input frame.
//size_t numCols = outputMatrixAndLayout.first->GetNumCols();
size_t numCols = fr.m_pMBLayout->GetNumParallelSequences();
size_t startCol = fr.timeIdxInSeq * numCols;
this->m_outputsValue[i]->SetColumnSlice(*outputMatrixAndLayout.first, startCol, numCols);
}
else
{
// Set the entire output value.
this->m_outputsValue[i]->SetValue(*outputMatrixAndLayout.first);
}
if ((this->m_outputsMBLayout[i] != nullptr) && (outputMatrixAndLayout.second == nullptr))
LogicError("The UserDefinedFunction node has a non-null output MBLayout but none found from the '%S' user Function::Forward output Value", m_externalFunction->Name().c_str());
@ -106,10 +165,13 @@ public:
LogicError("The UserDefinedFunction node does not have an output MBLayout but the '%S' user Function::Forward output Value has a non-null layout", m_externalFunction->Name().c_str());
else if ((this->m_outputsMBLayout[i] == nullptr) && (outputMatrixAndLayout.second == nullptr))
;
else
else if (!inSEQMode)
{
if (this->m_outputsHasNewMBLayout[i])
{
// Update the layout only in PARMode (!SEQMode).
this->m_outputsMBLayout[i]->CopyFrom(outputMatrixAndLayout.second);
}
else
{
if (*this->m_outputsMBLayout[i] != *outputMatrixAndLayout.second)
@ -122,11 +184,19 @@ public:
}
}
virtual void BackpropToNonLooping(size_t /*inputIndex*/) override
// Similar to forward, this function also getting called from both PAR and
// SEQ modes of execution. Here we need to get the gradient corresponding
// to the frame and place it in the proper location in the SEQ mode.
// PAR Mode is a single invocation for the whole gradient matrix.
virtual void BackpropTo(const size_t inputIndex, const FrameRange& fr) override
{
if (m_currentBackpropStatePtr == nullptr)
return;
bool inSEQMode = !fr.IsAllFrames();
// Similar to the output, the gradient 0 is set to this node's
// gradient. other values are handled by OutputMultiplexerNode.
this->m_outputsGradient[0] = m_gradient;
std::unordered_map<::CNTK::Variable, ::CNTK::ValuePtr> outputGradientValues;
@ -139,29 +209,52 @@ public:
{
auto output = outputs[i];
// MBLayout and the frame has to point to the correct slice of the
// data in the SEQ mode. For PAR mode, this function is called
// only once with all frames.
MBLayoutPtr layout = make_shared<MBLayout>();
std::shared_ptr<Matrix<ElemType>> outputGradient;
if (inSEQMode)
{
layout->InitAsFrameMode(fr.m_pMBLayout->GetNumParallelSequences());
size_t numCols = fr.m_pMBLayout->GetNumParallelSequences();
size_t startCol = fr.timeIdxInSeq * numCols;
outputGradient = std::make_shared<Matrix<ElemType>>(this->m_outputsGradient[i]->ColumnSlice(startCol, numCols));
}
else
{
layout = this->m_outputsMBLayout[i];
outputGradient = this->m_outputsGradient[i];
}
// TODO: We unpack the same output gradients each time this method is called for a different input.
// We should be able to cache the unpacked values during backpropagation of gradients to the first
// We should be able to cache the unpacked values during back-propagation of gradients to the first
// input, and reuse them for subsequence inputs.
::CNTK::ValuePtr gradientValue;
if (output.NeedsGradient())
gradientValue = ::CNTK::Utils::GetValueObjectFromCNTKImplMatrixAndMBLayout(::CNTK::AsNDShape(this->m_outputsShape[i]), output.DynamicAxes(), *this->m_outputsGradient[i], this->m_outputsMBLayout[i]);
gradientValue =
::CNTK::Utils::GetValueObjectFromCNTKImplMatrixAndMBLayout(
::CNTK::AsNDShape(this->m_outputsShape[i]),
output.DynamicAxes(),
*outputGradient,
layout);
outputGradientValues.insert({ output, gradientValue });
}
std::vector<::CNTK::Variable> externalFunctionUniqueInputs;
auto externalFunctionInputs = m_externalFunction->Inputs();
for (auto input : externalFunctionInputs)
{
if (std::find(externalFunctionUniqueInputs.begin(), externalFunctionUniqueInputs.end(), input) == externalFunctionUniqueInputs.end())
externalFunctionUniqueInputs.push_back(input);
}
std::unordered_map<::CNTK::Variable, size_t> externalFunctionUniqueInputs;
std::unordered_map<::CNTK::Variable, ::CNTK::ValuePtr> inputGradientValues;
for (size_t i = 0; i < externalFunctionUniqueInputs.size(); ++i)
auto externalFunctionInputs = m_externalFunction->Inputs();
for (int i = 0; i < externalFunctionInputs.size(); ++i)
{
if (InputRef(i).NeedsGradient())
inputGradientValues.insert({ externalFunctionUniqueInputs[i], nullptr });
if (externalFunctionUniqueInputs.find(externalFunctionInputs[i]) == externalFunctionUniqueInputs.end())
{
externalFunctionUniqueInputs.insert({ externalFunctionInputs[i], i });
if (InputRef(i).NeedsGradient())
{
inputGradientValues.insert({ externalFunctionInputs[i], nullptr });
}
}
}
m_externalFunction->Backward(m_currentBackpropStatePtr, outputGradientValues, inputGradientValues);
@ -169,37 +262,133 @@ public:
// Accumulate the computed input gradient value into the existing input gradient value
// TODO: We should directly pass the actual input gradient tensor to the Backward method
// instead of allocating a new value and accumulating it ourselves
for (size_t i = 0; i < externalFunctionUniqueInputs.size(); ++i)
//for (size_t i = 0; i < externalFunctionUniqueInputs.size(); ++i)
for (auto it = externalFunctionUniqueInputs.begin(); it != externalFunctionUniqueInputs.end(); ++it)
{
if (!InputRef(i).NeedsGradient())
auto& inputNode = InputRef(it->second);
if (!inputNode.NeedsGradient())
continue;
InputRef(i).LazyZeroGradient(this); // set gradient to 0 if this is the first time
inputNode.LazyZeroGradient(this); // set gradient to 0 if this is the first time
auto input = externalFunctionUniqueInputs[i];
auto input = it->first;
auto inputGradientValue = inputGradientValues[input];
if (!inputGradientValue)
continue;
auto newInputGradientMatrixAndLayout = ::CNTK::Utils::GetCNTKImplMatrixAndMBLayoutFromValueObject<ElemType>(input, inputGradientValue);
InputRef(i).Gradient() += *newInputGradientMatrixAndLayout.first;
// Get the input gradient for the particular input.
auto newInputGradientMatrixAndLayout =
::CNTK::Utils::GetCNTKImplMatrixAndMBLayoutFromValueObject<ElemType>(
input,
inputGradientValue);
if (*InputRef(i).GetMBLayout() != *newInputGradientMatrixAndLayout.second)
LogicError("The MBLayout 'NumSequences=%zu, NumTimeSteps=%zu' of the Input(%zu) gradient computed by the external function '%S' does not match the expected MBLayout 'NumSequences=%zu, NumTimeSteps=%zu'.",
newInputGradientMatrixAndLayout.second->GetNumSequences(), newInputGradientMatrixAndLayout.second->GetNumTimeSteps(),
i, this->GetName().c_str(),
InputRef(i).GetMBLayout()->GetNumSequences(), InputRef(i).GetMBLayout()->GetNumTimeSteps());
// Set the gradient based on the current frame.
if (inputNode.HasMBLayout() && inSEQMode)
{
inputNode.GradientFor(fr) += *newInputGradientMatrixAndLayout.first;
}
else
{
inputNode.Gradient() += *newInputGradientMatrixAndLayout.first;
if (*inputNode.GetMBLayout() != *newInputGradientMatrixAndLayout.second)
LogicError("The MBLayout 'NumSequences=%zu, NumTimeSteps=%zu' of the Input(%zu)"
" gradient computed by the external function '%S' does not match the"
" expected MBLayout 'NumSequences=%zu, NumTimeSteps=%zu'.",
newInputGradientMatrixAndLayout.second->GetNumSequences(),
newInputGradientMatrixAndLayout.second->GetNumTimeSteps(),
it->second, this->GetName().c_str(),
inputNode.GetMBLayout()->GetNumSequences(),
inputNode.GetMBLayout()->GetNumTimeSteps());
}
}
m_currentBackpropStatePtr = nullptr;
// Set the back-prop state to null when the last time frame
// (actually the first due to backward calling) is executed.
if (!inSEQMode || fr.timeIdxInSeq == 0)
{
m_currentBackpropStatePtr = nullptr;
}
}
virtual void Validate(bool isFinalValidationPass) override
{
Base::Validate(isFinalValidationPass);
// For UDF we need to infer the MBLayout for the function.
// The following code, will find the first output that has
// dynamic axes similar to one of the inputs and use the
// MBLayout of that input as the UDF's MBLayout.
auto outputs = m_externalFunction->Outputs();
bool layoutNotInitialized = (m_pMBLayout == nullptr);
if (layoutNotInitialized)
{
bool matchingDynamicAxesFound = false;
int matchCount;
auto arguments = m_externalFunction->Arguments();
for (size_t outputIndex = 0; outputIndex < outputs.size() && !matchingDynamicAxesFound; ++outputIndex)
{
auto output = outputs[outputIndex];
auto outputDynamicAxes = output.DynamicAxes();
auto numInputs = GetNumInputs();
assert(numInputs > 0);
size_t argIndex = 0;
ComputationNodePtr minRankedIniputPtr = nullptr;
for (size_t inputIndex = 0; inputIndex < numInputs; ++inputIndex)
{
auto& input = InputRef(inputIndex);
if (input.template Is<LearnableParameter<ElemType>>() || (!input.HasMBLayout()))
{
continue;
}
auto inputDynamicAxes = arguments[argIndex++].DynamicAxes();
// The number of output dynamic axes should be equal or less
// than the input dynamic axes.
if (outputDynamicAxes.size() > inputDynamicAxes.size())
{
continue;
}
matchCount = 0;
for (size_t k = 0; k < outputDynamicAxes.size(); ++k)
{
if (inputDynamicAxes[k] == outputDynamicAxes[k])
{
++matchCount;
}
}
if (matchCount == outputDynamicAxes.size())
{
// Pick the input with the smallest rank.
if (minRankedIniputPtr == nullptr ||
(minRankedIniputPtr->GetSampleLayout().GetRank() > input.GetSampleLayout().GetRank()))
{
minRankedIniputPtr = Input(inputIndex);
}
matchingDynamicAxesFound = true;
}
}
if (matchingDynamicAxesFound)
{
LinkToMBLayout(minRankedIniputPtr->GetMBLayout());
}
}
if (!matchingDynamicAxesFound)
{
InferMBLayoutFromInputsForStandardCase(isFinalValidationPass);
}
}
for (size_t i = 0; i < outputs.size(); ++i)
{
auto output = outputs[i];
@ -211,23 +400,13 @@ public:
DataTypeName(::CNTK::AsDataType<ElemType>()));
}
auto outputNDShape = output.Shape();
this->m_outputsMBLayout[i] = m_pMBLayout;
if (layoutNotInitialized)
{
auto outputDynamicAxes = output.DynamicAxes();
if (outputDynamicAxes.empty())
{
this->m_outputsHasNewMBLayout[i] = true;
this->m_outputsMBLayout[i] = nullptr;
}
else
{
this->m_outputsMBLayout[i] = make_shared<MBLayout>(); // this generates a new layout
this->m_outputsMBLayout[i]->SetUniqueAxisName(InternalDynamicAxisNameFromDynamicAxes(output.DynamicAxes()));
this->m_outputsHasNewMBLayout[i] = true;
}
this->m_outputsHasNewMBLayout[i] = true;
}
auto outputNDShape = output.Shape();
for (size_t k = 0; k < outputNDShape.Rank(); ++k)
{
if ((outputNDShape[k] == ::CNTK::NDShape::FreeDimension) || (outputNDShape[k] == ::CNTK::NDShape::InferredDimension))
@ -235,12 +414,8 @@ public:
}
this->m_outputsShape[i] = ::CNTK::AsTensorShape(outputNDShape);
if (i == 0)
{
if (layoutNotInitialized)
m_pMBLayout = this->m_outputsMBLayout[i];
SetDims(this->m_outputsShape[i], HasMBLayout());
}
}
@ -253,5 +428,4 @@ private:
template class UserDefinedV2FunctionNode<float>;
template class UserDefinedV2FunctionNode<double>;
}}}

Просмотреть файл

@ -30,6 +30,23 @@ test_ignores=(
test_warnings
# Fails tolerance sometimes
test_conv3d_transpose
# These tests fail because we enabled recurrence for user defined function (UDF)s.
# Keras performs a reshaping of variables to match batch and sequence axes in CNTK format
# but this causes the shape mismatch at BeginBackprop() because we validate input shapes
# for UDF functions before backpropagation. Latest Keras vesion (2.1.5 as of 03-22-2018)
# seems to fix this issue but till we upgrade to that version or later, we need to
# ignore these failing tests.
test_masking
test_sequential_temporal_sample_weights
test_sequential_model_saving
test_return_sequences
test_dropout
test_implementation_mode
test_specify_initial_state_keras_tensor
test_specify_initial_state_non_keras_tensor
test_specify_state_with_masking
test_TimeDistributed
test_sequential_regression
)
# Windows needs a few more exclusions

Просмотреть файл

@ -742,3 +742,108 @@ def test_udf_in_recurrent_loop():
with pytest.raises(RuntimeError):
m.eval([np.arange(10, dtype=np.float32)])
class SimpleRecurrentNode(UserFunction):
def __init__(self, x, y, name='NewLayer'):
super(SimpleRecurrentNode, self).__init__([x, y], name=name)
self.count = 0
def forward(self, arguments, device=None, as_numpy=True):
return None, arguments[1]
def backward(self, state, root_gradients, input_gradients):
for input in input_gradients:
input_gradients[input] = root_gradients
def infer_outputs(self):
self.count = self.count + 1
outputVar = [C.output_variable(self.inputs[1].shape, self.inputs[1].dtype,
self.inputs[1].dynamic_axes, name='outDummyLayer')]
return outputVar
def serialize(self):
return None
@staticmethod
def deserialize(inputs, name, state):
return SimpleRecurrentNode(inputs, name=name)
def test_recurrance_with_udf_with_layers():
x = C.sequence.input_variable(needs_gradient=True,shape=(3,2))
x0 = np.reshape(np.arange(24.0,dtype=np.float32),(1,4,3,2))
name = "NewLayer"
@C.BlockFunction(name, name)
def udf(x, y):
return C.user_function(SimpleRecurrentNode(x, y))
udf_recurrent = C.layers.Recurrence(udf)(x)
value = udf_recurrent.eval({x:x0})
assert np.array_equal(value, x0)
gradient, result= udf_recurrent.grad({x: x0}, wrt=[x], outputs=[udf_recurrent.output])
g1 = np.full((3,2),4, dtype=np.float32)
g2 = np.full((3,2),3, dtype=np.float32)
g3 = np.full((3,2),2, dtype=np.float32)
g4 = np.full((3,2),1, dtype=np.float32)
grad = [g1,g2,g3,g4]
grad = np.reshape(grad, (1,4,3,2))
assert np.array_equal(gradient, grad)
assert np.array_equal(result, x0)
class SimpleUdf(UserFunction):
def __init__(self, x, name='SimpleUdf'):
super(SimpleUdf, self).__init__([x], name=name)
def forward(self, arguments, device=None, as_numpy=True):
return None, arguments
def backward(self, state, root_gradients, variables=None, as_numpy=True):
return root_gradients
def infer_outputs(self):
outputVar = [C.output_variable(self.inputs[idx].shape, self.inputs[idx].dtype,
self.inputs[idx].dynamic_axes, name='outSimpleUdf') for idx in range(len(self.inputs))]
return outputVar
def serialize(self):
return None
@staticmethod
def deserialize(inputs, name, state):
return SimpleUdf(inputs, name=name)
def test_recurrance_with_udf_without_layers():
name = "SimpleUdf"
def udf(a):
return C.user_function(SimpleUdf(a, name=name))
# input varibale and the data.
x = C.sequence.input_variable(needs_gradient=True,shape=(2,))
x0 = np.reshape(np.arange(16.0, dtype=np.float32),(2,4,2))
print(x0)
# creates a recurrent loop.
p = C.placeholder(shape=(2,))
past= C.sequence.past_value(p)
z = udf(x) * udf(past) + C.Parameter((2,), init=[1,1])
z.replace_placeholders({p:z.outputs[0]})
#C.logging.graph.plot(z, "recurrent.pdf")
out = z.eval({x:x0})
print(out)
expected_out = [np.array([1,1,3,4,13,21,79,148], dtype=np.float32).reshape(4,2),np.array([1,1,11,12,133,157,1863,2356], dtype=np.float32).reshape(4,2)]
assert np.array_equal(out, expected_out)
gradient, result= z.grad({x: x0}, wrt=[x], outputs=[z.output])
print(result)
assert np.array_equal(result, expected_out)
expected_grad = [np.array([0,0,29,41,21,32,13,21], dtype=np.float32).reshape(4,2),np.array([0,0,181,209,165,192,133,157], dtype=np.float32).reshape(4,2)]
print(gradient)
assert np.array_equal(gradient, expected_grad)