chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 12:47:05 +08:00
commit 4f3b7da785
7394 changed files with 2005594 additions and 0 deletions
@@ -0,0 +1,131 @@
/* ******************************************************************************
*
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/
//
// Created by raver119 on 20.10.2017.
//
#include <graph/GraphExecutioner.h>
#include <graph/execution/LogicConditional.h>
#include <graph/execution/LogicReturn.h>
namespace sd {
namespace graph {
Status LogicConditional::processNode(Graph *graph, Node *node) {
auto __variableSpace = graph->getVariableSpace();
auto size = node->input()->size();
// propagating inputs (optional)
for (size_t e = 0; e < size - 3; e++) {
std::pair<int, int> pair(node->id(), e);
if (!__variableSpace->hasVariable(pair)) {
__variableSpace->putVariable(pair, new Variable(nullptr, nullptr, node->id(), e));
}
auto va = node->input()->at(e);
auto inputVar = __variableSpace->getVariable(va);
auto innerVar = __variableSpace->getVariable(pair);
if (innerVar->hasNDArray()) {
// TODO: ???
} else {
// FIXME: in some cases it's possible to have no NDArray
if (inputVar->hasNDArray()) innerVar->setNDArray(new NDArray(inputVar->getNDArray()->dup(inputVar->getNDArray()->ordering())));
}
}
int scopeConditionIndex = node->input()->at(size - 3).first;
int scopeFalseIndex = node->input()->at(size - 2).first;
int scopeTrueIndex = node->input()->at(size - 1).first;
auto scopeCondition = graph->scopeById(scopeConditionIndex);
int lastNode = 0;
for (auto v : *scopeCondition->nodes()) {
GraphExecutioner::executeFlatNode(graph, v, __variableSpace);
lastNode = v->id();
}
auto result = __variableSpace->getVariable(lastNode)->getNDArray();
bool isReturn = false;
// now we're executing one of the scopes, depending on condition evaluation
if (result->e<int>(0) == 0) {
auto scopeFalse = graph->scopeById(scopeFalseIndex);
lastNode = 0;
int nodes = scopeFalse->nodes()->size();
for (int e = 0; e < nodes - 1; e++) {
auto v = scopeFalse->nodes()->at(e);
GraphExecutioner::executeFlatNode(graph, v, __variableSpace);
lastNode = v->id();
}
// last node is either return or just last op
auto *node2 = scopeFalse->nodes()->at(nodes - 1);
if (node2->opType() == ::graph::OpType_LOGIC && node2->opNum() == 40) {
isReturn = true;
LogicReturn::processNode(graph, node2);
} else {
GraphExecutioner::executeFlatNode(graph, node2, __variableSpace);
lastNode = node2->id();
}
} else {
auto scopeTrue = graph->scopeById(scopeTrueIndex);
lastNode = 0;
int nodes = scopeTrue->nodes()->size();
for (int e = 0; e < nodes - 1; e++) {
auto v = scopeTrue->nodes()->at(e);
GraphExecutioner::executeFlatNode(graph, v, __variableSpace);
lastNode = v->id();
}
// last node is either return or just last op
auto node2 = scopeTrue->nodes()->at(nodes - 1);
if (node2->opType() == ::graph::OpType_LOGIC && node2->opNum() == 40) {
isReturn = true;
LogicReturn::processNode(graph, node2);
} else {
GraphExecutioner::executeFlatNode(graph, node2, __variableSpace);
lastNode = node->id();
}
}
// now fetch and transfer variables to Conditional node
// but only if return wasn't called at the end of scope
if (!isReturn) {
for (int e = 0; e < DataTypeUtils::max<int>(); e++) {
std::pair<int, int> pair(lastNode, e);
std::pair<int, int> pairNew(node->id(), e);
if (__variableSpace->hasVariable(pair)) {
auto array = __variableSpace->getVariable(pair)->getNDArray();
auto newVar = new Variable(array);
newVar->setId(lastNode, e);
newVar->markRemovable(false);
__variableSpace->putVariable(pairNew, newVar);
} else
break;
}
}
return Status::OK;
}
} // namespace graph
} // namespace sd
@@ -0,0 +1,74 @@
/* ******************************************************************************
*
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/
//
// @author raver119@gmail.com
//
#include <graph/execution/LogicEnter.h>
namespace sd {
namespace graph {
Status LogicEnter::processNode(Graph *graph, Node *node) {
// this op replicates input variable into the frame. basically happens once for single loop.
// sure, if there's inner loop within outer loop, it'll be called once for outer loop and multiple times for inner
// loop
auto __variableSpace = graph->getVariableSpace();
auto __flowPath = __variableSpace->flowPath();
// basically, first non-null variable is our target
for (size_t e = 0; e < node->input()->size(); e++) {
auto inputAddr = node->input()->at(e);
if (__variableSpace->hasVariable(inputAddr)) {
auto var = __variableSpace->getVariable(inputAddr);
if (var->hasNDArray()) {
Variable *lvar = nullptr;
if (__variableSpace->hasVariable(node->id(), 0))
lvar = __variableSpace->getVariable(node->id(), 0);
else
lvar = new Variable(nullptr, node->getName()->c_str(), node->id(), 0);
auto array = var->getNDArray();
lvar->setNDArray(array);
lvar->markReadOnly(true);
break;
} else if (var->hasNDArrayList()) {
Variable *lvar = nullptr;
if (__variableSpace->hasVariable(node->id(), 0))
lvar = __variableSpace->getVariable(node->id(), 0);
else
lvar = new Variable(nullptr, node->getName()->c_str(), node->id(), 0);
auto list = var->getNDArrayList();
lvar->setNDArrayList(list);
lvar->markReadOnly(true);
break;
} else {
// FIXME: can we really have third case here?
continue;
}
}
}
return Status::OK;
}
} // namespace graph
} // namespace sd
@@ -0,0 +1,71 @@
/* ******************************************************************************
*
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/
//
// Created by raver119 on 20.10.2017.
//
#include <graph/execution/LogicConditional.h>
#include <graph/execution/LogicEnter.h>
#include <graph/execution/LogicExecutor.h>
#include <graph/execution/LogicExit.h>
#include <graph/execution/LogicExpose.h>
#include <graph/execution/LogicLoopCond.h>
#include <graph/execution/LogicMerge.h>
#include <graph/execution/LogicNextIteration.h>
#include <graph/execution/LogicReturn.h>
#include <graph/execution/LogicScope.h>
#include <graph/execution/LogicSwitch.h>
#include <graph/execution/LogicWhile.h>
namespace sd {
namespace graph {
Status LogicExecutor::processNode(Graph *graph, Node *node) {
switch (node->opNum()) {
case logic::While:
return LogicWhile::processNode(graph, node);
case logic::Scope:
return LogicScope::processNode(graph, node);
case logic::Conditional:
return LogicConditional::processNode(graph, node);
case logic::Switch:
return LogicSwitch::processNode(graph, node);
case logic::Return:
return LogicReturn::processNode(graph, node);
case logic::Expose:
return LogicExpose::processNode(graph, node);
case logic::Merge:
return LogicMerge::processNode(graph, node);
case logic::LoopCond:
return LogicLoopCond::processNode(graph, node);
case logic::NextIteration:
return LogicNextIeration::processNode(graph, node);
case logic::Exit:
return LogicExit::processNode(graph, node);
case logic::Enter:
return LogicEnter::processNode(graph, node);
}
if (node->getName() == nullptr) {
sd_printf("Unknown LogicOp used at node [%i]: [%i]\n", node->id(), node->opNum());
} else {
sd_printf("Unknown LogicOp used at node [%i:<%s>]: [%i]\n", node->id(), node->getName()->c_str(), node->opNum());
}
return Status::BAD_INPUT;
}
} // namespace graph
} // namespace sd
@@ -0,0 +1,47 @@
/* ******************************************************************************
*
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/
//
// @author raver119@gmail.com
//
#include <graph/execution/LogicExit.h>
namespace sd {
namespace graph {
Status LogicExit::processNode(Graph *graph, Node *node) {
// this op is basically no-op
// we just know it exists
auto __variableSpace = graph->getVariableSpace();
auto __flowPath = __variableSpace->flowPath();
Context ctx(node->getContextPrototype(), __variableSpace);
auto input = ctx.variable(0)->getNDArray();
std::pair<int, int> pair0(node->id(), 0);
if (!__variableSpace->hasVariable(pair0))
__variableSpace->putVariable(pair0, new Variable(nullptr, nullptr, node->id(), 0));
__variableSpace->getVariable(pair0)->setNDArray(input);
__variableSpace->getVariable(pair0)->markRemovable(false);
return Status::OK;
}
} // namespace graph
} // namespace sd
@@ -0,0 +1,31 @@
/* ******************************************************************************
*
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/
//
// Created by raver119 on 12.11.2017.
//
#include <graph/execution/LogicExpose.h>
namespace sd {
namespace graph {
Status LogicExpose::processNode(Graph *graph, Node *node) {
// do we really want this?
return Status::OK;
}
} // namespace graph
} // namespace sd
@@ -0,0 +1,54 @@
/* ******************************************************************************
*
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/
//
// @author raver119@gmail.com
//
#include <graph/execution/LogicLoopCond.h>
namespace sd {
namespace graph {
Status LogicLoopCond::processNode(Graph *graph, Node *node) {
auto __variableSpace = graph->getVariableSpace();
auto __flowPath = __variableSpace->flowPath();
Context ctx(node->getContextPrototype(), __variableSpace);
auto input = ctx.variable(0)->getNDArray();
std::pair<int, int> pair0(node->id(), 0);
if (!__variableSpace->hasVariable(pair0))
__variableSpace->putVariable(pair0, new Variable(nullptr, nullptr, node->id(), 0));
__variableSpace->getVariable(pair0)->setNDArray(input);
__variableSpace->getVariable(pair0)->markRemovable(false);
// pass further
if (input->e<int>(0) > 0) {
// if condition is TRUE body will be invoked some time soon
// __flowPath->markFrameActive(node->getFrameId(), true);
//__flowPath->i
} else {
// body won't be activated
// __flowPath->markFrameActive(node->getFrameId(), false);
}
return Status::OK;
}
} // namespace graph
} // namespace sd
@@ -0,0 +1,122 @@
/* ******************************************************************************
*
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/
//
// Created by raver119 on 30.01.18.
//
#include <graph/execution/LogicMerge.h>
namespace sd {
namespace graph {
Status LogicMerge::processNode(Graph *graph, Node *node) {
// at merge node only one of inputs exist if that's just switch and other node isn't LogicNextItration
auto __variableSpace = graph->getVariableSpace();
auto __flowPath = __variableSpace->flowPath();
// merge MUST have 2 inputs
auto inputAddr0 = node->input()->at(0);
auto inputAddr1 = node->input()->at(1);
bool isWhile = false;
// now we want to check if second input is NextIteration
if (graph->hasNode(inputAddr1.first)) {
auto secondNode = graph->nodeById(inputAddr1.first);
// checking for NextIteration
if (secondNode->opType() == ::graph::OpType_LOGIC && secondNode->opNum() == 80L) {
isWhile = true;
// notifying NextIteration node for rewind index
secondNode->setRewindLayer(node->getLayer());
secondNode->setRewindNode(node->id());
}
}
// FIXME: we don't need this check. Just last input should survive, IF it exists
if (isWhile) {
if (node->getFrameId() >= 0) __flowPath->markFrameActive(node->getFrameId(), true);
bool hasVar = __variableSpace->hasVariable(inputAddr1);
if (hasVar && __flowPath->wasExecuted(inputAddr1.first)) {
sd_debug("Node_%i: propagating second input\n", node->id());
auto var = __variableSpace->getVariable(inputAddr1);
Variable *lvar = nullptr;
if (__variableSpace->hasVariable(node->id(), 0))
lvar = __variableSpace->getVariable(node->id(), 0);
else
lvar = new Variable(nullptr, node->getName()->c_str(), node->id(), 0);
auto array = var->getNDArray();
lvar->setNDArray(array);
lvar->markReadOnly(true);
__flowPath->markExecuted(inputAddr1.first, false);
} else {
sd_debug("Node_%i: propagating first input\n", node->id());
auto var = __variableSpace->getVariable(inputAddr0);
Variable *lvar = nullptr;
if (__variableSpace->hasVariable(node->id(), 0))
lvar = __variableSpace->getVariable(node->id(), 0);
else
lvar = new Variable(nullptr, node->getName()->c_str(), node->id(), 0);
if (lvar->hasNDArray())
delete lvar->getNDArray();
auto array = var->getNDArray();
lvar->setNDArray(array);
lvar->markReadOnly(true);
}
} else {
// basically, first non-null variable is our target
for (size_t e = 0; e < node->input()->size(); e++) {
auto inputAddr = node->input()->at(e);
if (__variableSpace->hasVariable(inputAddr)) {
auto var = __variableSpace->getVariable(inputAddr);
if (!var->hasNDArray() || !__flowPath->isNodeActive(inputAddr.first)) continue;
Variable *lvar = nullptr;
if (__variableSpace->hasVariable(node->id(), 0))
lvar = __variableSpace->getVariable(node->id(), 0);
else {
lvar = new Variable(nullptr, node->getName()->c_str(), node->id(), 0);
}
if (lvar->hasNDArray()) delete lvar->getNDArray();
auto array = var->getNDArray();
lvar->setNDArray(array);
lvar->markReadOnly(true);
lvar->markExternal(false);
break;
}
}
}
return Status::OK;
}
} // namespace graph
} // namespace sd
@@ -0,0 +1,50 @@
/* ******************************************************************************
*
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/
//
// @author raver119@gmail.com
//
#include <graph/execution/LogicNextIteration.h>
namespace sd {
namespace graph {
Status LogicNextIeration::processNode(Graph *graph, Node *node) {
auto __variableSpace = graph->getVariableSpace();
auto __flowPath = __variableSpace->flowPath();
auto inputAddr = node->input()->at(0);
auto var = __variableSpace->getVariable(inputAddr);
Variable *lvar = nullptr;
if (__variableSpace->hasVariable(node->id(), 0))
lvar = __variableSpace->getVariable(node->id(), 0);
else
lvar = new Variable(nullptr, node->getName()->c_str(), node->id(), 0);
if (lvar->hasNDArray())
delete lvar->getNDArray();
auto array = var->getNDArray();
lvar->setNDArray(array);
lvar->markReadOnly(true);
return Status::OK;
}
} // namespace graph
} // namespace sd
@@ -0,0 +1,58 @@
/* ******************************************************************************
*
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/
//
// Created by raver119 on 28.10.2017.
//
#include "graph/execution/LogicReturn.h"
#include <helpers/EnumUtils.h>
namespace sd {
namespace graph {
Status LogicReturn::processNode(Graph *graph, Node *node) {
auto __variableSpace = graph->getVariableSpace();
for (size_t e = 0; e < node->input()->size(); e++) {
auto inputAddr = node->input()->at(e);
auto outputAddr = node->output()->at(e);
// FIXME!!
outputAddr.second = e;
if (Environment::getInstance().isDebugAndVerbose())
sd_debug("Return input: <%i, %i>; Return output: <%i, %i>\n", inputAddr.first, inputAddr.second, outputAddr.first,
outputAddr.second);
auto varIn = __variableSpace->getVariable(inputAddr);
auto varOut = __variableSpace->getVariable(outputAddr);
sd_debug("Returning varType: [%s]\n", EnumUtils::_VariableTypeToString(varIn->variableType()));
// FIXME: this is obviously wrong, we should keep depth track for backprop here
varOut->getNDArray()->assign(varIn->getNDArray());
if (Environment::getInstance().isDebugAndVerbose())
sd_debug("In after: [%f]; Out after: [%f]\n", varIn->getNDArray()->meanNumber().e<float>(0),
varOut->getNDArray()->meanNumber().e<float>(0));
}
return Status::OK;
}
} // namespace graph
} // namespace sd
@@ -0,0 +1,32 @@
/* ******************************************************************************
*
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/
//
// Created by raver119 on 20.10.2017.
//
#include <graph/execution/LogicScope.h>
namespace sd {
namespace graph {
Status LogicScope::processNode(Graph *graph, Node *node) {
// this op is basically no-op
// we just know it exists
return Status::OK;
}
} // namespace graph
} // namespace sd
@@ -0,0 +1,105 @@
/* ******************************************************************************
*
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/
//
// Created by raver119 on 21.10.17.
//
#include <graph/GraphExecutioner.h>
#include <graph/execution/LogicSwitch.h>
namespace sd {
namespace graph {
Status LogicSwitch::processNode(Graph* graph, Node* node) {
auto __variableSpace = graph->getVariableSpace();
auto __flowPath = __variableSpace->flowPath();
Context ctx(node->getContextPrototype(), __variableSpace);
// this can be either our format, or compatible format.
if (graph->hasScope(node->input()->at(0).first)) {
sd_debug("Node_%i: Scoped mode.\n", node->id());
// first input is OpScope, so it's ours
int scopeConditionIndex = node->input()->at(0).first;
auto input = ctx.variable(1);
auto scopeCondition = graph->scopeById(scopeConditionIndex);
int lastNode = 0;
for (auto v : *scopeCondition->nodes()) {
GraphExecutioner::executeFlatNode(graph, v, __variableSpace);
lastNode = v->id();
}
// now we should take result of the OpScope run, and evaluate it
auto result = __variableSpace->getVariable(lastNode)->getNDArray();
std::pair<int, int> pair0(node->id(), 0);
std::pair<int, int> pair1(node->id(), 1);
if (!__variableSpace->hasVariable(pair0))
__variableSpace->putVariable(pair0, new Variable(nullptr, nullptr, node->id(), 0));
if (!__variableSpace->hasVariable(pair1))
__variableSpace->putVariable(pair1, new Variable(nullptr, nullptr, node->id(), 1));
if (!result->e<bool>(0)) {
__flowPath->markBranch(node->id(), 0);
__variableSpace->getVariable(pair0)->setNDArray(input->getNDArray());
__variableSpace->getVariable(pair0)->markRemovable(false);
} else {
__flowPath->markBranch(node->id(), 1);
__variableSpace->getVariable(pair1)->setNDArray(input->getNDArray());
__variableSpace->getVariable(pair1)->markRemovable(false);
}
} else {
// first input is NOT a OpScope, so it's compatible format
sd_debug("Node_%i: Compatible mode.\n", node->id());
auto input = ctx.variable(0)->getNDArray();
auto boolean = ctx.variable(1)->getNDArray();
std::pair<int, int> pair0(node->id(), 0);
std::pair<int, int> pair1(node->id(), 1);
if (!__variableSpace->hasVariable(pair0))
__variableSpace->putVariable(pair0, new Variable(nullptr, nullptr, node->id(), 0));
if (!__variableSpace->hasVariable(pair1))
__variableSpace->putVariable(pair1, new Variable(nullptr, nullptr, node->id(), 1));
if (!boolean->e<bool>(0)) {
// false
sd_debug("Node_%i: FALSE branch active\n", node->id());
__flowPath->markBranch(node->id(), 0);
__variableSpace->getVariable(pair0)->setNDArray(input);
__variableSpace->getVariable(pair0)->markRemovable(false);
} else {
// true
sd_debug("Node_%i: TRUE branch active\n", node->id());
__flowPath->markBranch(node->id(), 1);
__variableSpace->getVariable(pair1)->setNDArray(input);
__variableSpace->getVariable(pair1)->markRemovable(false);
}
}
return Status::OK;
};
} // namespace graph
} // namespace sd
@@ -0,0 +1,137 @@
/* ******************************************************************************
*
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/
//
// Created by raver119 on 20.10.2017.
//
#include <graph/GraphExecutioner.h>
#include <graph/execution/LogicExecutor.h>
#include <graph/execution/LogicReturn.h>
#include <graph/execution/LogicWhile.h>
namespace sd {
namespace graph {
Status LogicWhile::processNode(Graph* graph, Node* node) {
auto __variableSpace = graph->getVariableSpace();
sd_debug("Starting on WHILE loop: [%i]\n", node->id());
// total number of inputs. 2 last inputs are scopes
int inputs = node->input()->size();
if (inputs < 3) {
sd_printf("While [%i]: loop should have at least 1 external variable announced\n", node->id());
return Status::BAD_INPUT;
}
for (int e = 0; e < inputs - 2; e++) {
std::pair<int, int> pair(node->id(), e);
if (!__variableSpace->hasVariable(pair)) {
__variableSpace->putVariable(pair, new Variable(nullptr, nullptr, node->id(), e));
}
auto va = node->input()->at(e);
auto inputVar = __variableSpace->getVariable(va);
auto innerVar = __variableSpace->getVariable(pair);
if (innerVar->hasNDArray()) {
// TODO: ???
} else {
// FIXME: in some cases it's possible to have no NDArray
if (inputVar->hasNDArray()) innerVar->setNDArray(new NDArray(inputVar->getNDArray()->dup(inputVar->getNDArray()->ordering())));
}
}
int scopeConditionIndex = node->input()->at(inputs - 2).first;
int scopeBodyIndex = node->input()->at(inputs - 1).first;
sd_debug("While [%i]: got [%i] inputs\n", node->id(), node->input()->size());
// we're running condition nodes now
auto scope = graph->scopeById(scopeConditionIndex);
int breaker = 0;
while (true && breaker < 10000000) {
int lastNode = 0;
// we're running condition scope first
sd_debug("While [%i]: got [%i] ops in condition scope [%i]\n", node->id(), scope->nodes()->size(),
scopeConditionIndex);
for (Node* v : *scope->nodes()) {
// v->getBlock()->updateVariables();
if (v->opType() == ::graph::OpType_LOGIC) {
sd_debug("Falling back to logic\n", "");
LogicExecutor::processNode(graph, v);
} else {
sd_debug("Op [<%s>]\n", v->getName()->c_str());
Status status = GraphExecutioner::executeFlatNode(graph, v, __variableSpace);
if (status != Status::OK) return status;
}
lastNode = v->id();
}
if (!__variableSpace->hasVariable(lastNode)) {
sd_printf("While [%i]: got no results out of conditional loop\n", node->id());
return Status::KERNEL_FAILURE;
}
// now we should take result of the OpScope run, and evaluate it
auto result = __variableSpace->getVariable(lastNode)->getNDArray();
// if result evaluates to 0.0 - condition returned FALSE
if (result->e<int>(0) == 0)
break;
else {
auto scopeBody = graph->scopeById(scopeBodyIndex);
size_t e = 0;
sd_debug("While [%i] got [%i] ops in body scope [%i]\n", node->id(), scopeBody->nodes()->size(), scopeBodyIndex);
for (; e < scopeBody->nodes()->size() - 1; e++) {
Node* v = scopeBody->nodes()->at(e);
if (v->opType() == ::graph::OpType_LOGIC) {
sd_debug("Falling back to logic\n", "");
LogicExecutor::processNode(graph, v);
} else {
sd_debug("Op [<%s>]\n", v->getName()->c_str());
// v->getBlock()->updateVariables();
Status status = GraphExecutioner::executeFlatNode(graph, v, __variableSpace);
if (status != Status::OK) return status;
}
}
// now execute return statement
Node* ret = scopeBody->nodes()->at(e);
LogicReturn::processNode(graph, ret);
}
breaker++;
}
// if we've hit breaker limit - we should notify about that
if (breaker >= 10000000) {
sd_printf("While condition seems to be never ending, aborting...\n", breaker);
return Status::KERNEL_FAILURE;
}
return Status::OK;
}
} // namespace graph
} // namespace sd