/// printAllValuesForNode -- prints all values for a given node, without a newline /// (meant to be a helper) static void printAllValuesForNode(llvm::raw_ostream &O, NodeValue &NV) { // We only consider other values that are in the graph // containing the specified node (by design) // Look for values that have an equivalent NH DSNodeHandle &NH = NV.getNodeH(); const DSGraph::ScalarMapTy &SM = NV.getGraph()->getScalarMap(); bool first = true; for (DSGraph::ScalarMapTy::const_iterator I = SM.begin(), E = SM.end(); I != E; ++I ) if (NH == I->second) { //Found one! const Value *V = I->first; //Print them out, separated by commas if (!first) O << ","; first = false; // Print out name, if it has one. // FIXME: Get "%0, "%1", naming like the .ll has? if (V->hasName()) O << V->getName(); else O << "<tmp>"; } //FIXME: Search globals in this graph too (not just scalarMap)? }
void NodeUse::setOperand(NodeValue &other) { if (other && site_->getNode()) { assert(site_->getType() == other.getType() && "Setting operand to a node with a different type"); } site_->setOperand(other.getNode(), other.getResNo()); }
//---------------------------------------------------------------------------------------------- void GraphScene::NewNode() { if(m_pChoosePlanStepDlg->exec() == QDialog::Accepted) { unsigned planStepId = m_pChoosePlanStepDlg->SelectedPlanStepId(); NodeValue pNodeModel = NULL; if(BELONG(GoalType, planStepId)) pNodeModel = g_GoalFactory.GetGoal((GoalType)planStepId, false); else if(BELONG(ActionType, planStepId)) pNodeModel = g_ActionFactory.GetAction((ActionType)planStepId, false); else throw NotImplementedException(XcptHere); _ASSERTE(planStepId != NULL); NodeID nodeId = m_pGraph->AddNode(pNodeModel, pNodeModel->Id()); GraphNodeView *pNodeView = new GraphNodeView(pNodeModel, nodeId, m_pPlanContext, m_pNodeMenu, nullptr); pNodeView->setPos(m_lastCtxtMenuScreenPos.x(), m_lastCtxtMenuScreenPos.y()); m_nodeIdToNodeViewMap[nodeId] = pNodeView; addItem(pNodeView); } }
/// printNodes -- print the node specified by NV /// /// Format: /// "flags:{value(s)}:{type(s)}" /// /// Additionally, the user can specify to print just one piece static void printNode(llvm::raw_ostream &O, NodeValue &NV) { assert( ((!OnlyPrintFlags && !OnlyPrintValues)|| (!OnlyPrintFlags && !OnlyPrintTypes) || (!OnlyPrintValues && !OnlyPrintTypes)) && "Only one \"Only\" option allowed!"); if (OnlyPrintFlags) { printFlags(O,NV.getNode()); } else if (OnlyPrintValues) { printAllValuesForNode(O, NV); } else if (OnlyPrintTypes) { printTypesForNode(O, NV); } else { //Print all of them printFlags(O,NV.getNode()); O << ":{"; printAllValuesForNode(O, NV); O << "}:{"; printTypesForNode(O, NV); O << "}"; } O << "\n"; }
bool UnownedNodeValueMap::count(NodeValue from) { for (auto &E : entries_) { auto &F = E.first; if (F.first == from.getNode() && F.second == from.getResNo()) { return true; } } return false; }
static void verifyConvolution(NodeValue src, NodeValue dest, NodeValue filter, NodeValue bias, size_t kernel, size_t stride, size_t pad, size_t group) { assert(src.getElementType() == dest.getElementType() && "Invalid Type"); assert(src.getElementType() == filter.getElementType() && "Invalid Type"); assert(src.getElementType() == bias.getElementType() && "Invalid Type"); ShapeNHWC idim(src.getType()->dims()); ShapeNHWC odim(dest.getType()->dims()); assert(idim.w >= kernel && idim.h >= kernel && "buffer too small for selected stride"); assert(idim.c % group == 0 && "channels number must be divisible by groups"); auto outSz = calculateConvOutputDims(idim.h, idim.w, kernel, stride, pad); (void)outSz; assert(odim.n == idim.n && odim.h == outSz.first && odim.w == outSz.second && odim.c % group == 0 && "Invalid output dimensions"); auto filterDims = {odim.c, kernel, kernel, idim.c / group}; assert(filter.getType()->dims().equals(filterDims) && "Invalid filter dims"); (void)filterDims; auto biasDims = {odim.c}; assert(bias.getType()->dims().equals(biasDims) && "Invalid bias dims"); (void)biasDims; }
IlwisTypes OperationNodeScript::typesUsed(int index, const NodeValue& vright, SymbolTable &symbols) const { if ( index >= vright.size()) return itUNKNOWN; IlwisTypes tp1 = symbols.ilwisType(vright[index], vright.id(index)); IlwisTypes tp2 = symbols.ilwisType(_value[index], _value.id(index)); if ( tp1 == itUNKNOWN || tp2 == itUNKNOWN) return itUNKNOWN; return tp1 | tp2; }
static void verifyPool(NodeValue src, NodeValue dest, size_t kernel, size_t stride, size_t pad) { ShapeNHWC idim = ShapeNHWC(src.getType()->dims()); ShapeNHWC odim = ShapeNHWC(dest.getType()->dims()); (void)odim; assert(idim.w >= kernel && idim.h >= kernel && "buffer too small for selected stride"); auto outSz = calculateConvOutputDims(idim.h, idim.w, kernel, stride, pad); ShapeNHWC exp(idim.n, outSz.first, outSz.second, idim.c); (void)exp; assert(exp == odim && "Unexpected output dimensions"); }
NodeValue UnownedNodeValueMap::get(NodeValue from) { for (auto &E : entries_) { auto &F = E.first; auto &T = E.second; if (F.first == from.getNode() && F.second == from.getResNo()) { return NodeValue(T.first, T.second); } } llvm_unreachable("Invalid node"); return NodeValue(nullptr, 0); }
static void verifyBatchNormalization(NodeValue src, NodeValue dest, NodeValue bias, NodeValue scale, NodeValue mean, NodeValue var, size_t channel) { checkSameType(dest, src); // Figure out how many channels are in the tensor. size_t channels = src.dims()[channel]; auto exp = {channels}; (void)exp; assert(bias.getType()->dims().equals(exp) && "Invalid bias dim"); assert(scale.getType()->dims().equals(exp) && "Invalid scale dim"); assert(mean.getType()->dims().equals(exp) && "Invalid mean dim"); assert(var.getType()->dims().equals(exp) && "Invalid var dim"); }
// printTypesForNode --prints all the types for the given NodeValue, without a newline // (meant to be called as a helper) static void printTypesForNode(llvm::raw_ostream &O, NodeValue &NV) { DSNode *N = NV.getNode(); if (N->isNodeCompletelyFolded()) { O << "Folded"; } // Go through all the types, and just dump them. // FIXME: Lifted from Printer.cpp, probably should be shared bool firstType = true; if (N->type_begin() != N->type_end()) for (DSNode::TyMapTy::const_iterator ii = N->type_begin(), ee = N->type_end(); ii != ee; ++ii) { if (!firstType) O << "::"; firstType = false; O << ii->first << ":"; if (ii->second) { bool first = true; for (svset<Type*>::const_iterator ni = ii->second->begin(), ne = ii->second->end(); ni != ne; ++ni) { if (!first) O << "|"; Type * t = *ni; t->print (O); first = false; } } else O << "VOID"; } else O << "VOID"; if (N->isArrayNode()) O << "Array"; }
QString TermNode::getName(const NodeValue& var) const { QString name = var.toString(); if (name != sUNDEF) return name; // QString typeName = var.typeName(); // if ( typeName == "Ilwis::IRasterCoverage") { // Ilwis::IRasterCoverage raster = var.value<Ilwis::IRasterCoverage>(); // name = raster->name(); // } // if ( typeName == "Coordinate") { // name = var.id(); // } // if ( typeName == "Voxel") { // name = var.id(); // } return var.id(); }
static void verifyFullyConnected(NodeValue src, NodeValue weights, NodeValue bias, NodeValue dest) { assert(src.dims()[0] == dest.dims()[0] && flattenCdr(src.dims()).second == weights.dims()[0] && "Mismatch on expected source dimensions"); assert(bias.dims()[0] == weights.dims()[1] && weights.dims()[1] == dest.dims()[1] && "Inconsistent bias/weights/dest sizes."); }
bool OperationNodeScript::handleTableCases(int index, const NodeValue& vright, const QString &operation, const QString& relation, SymbolTable &symbols, ExecutionContext *ctx) { if ( index >= vright.size()) return false; QString expr; if ( SymbolTable::isNumerical(vright[index]) && SymbolTable::isDataLink(_value[index])){ expr = QString("%1(%2,%3,%4,%5)").arg(operation).arg(_value.toString(index)). arg(additionalInfo(ctx,_value.toString(index))). arg(vright.toDouble(index)).arg(relation); } else if (SymbolTable::isNumerical(_value[index]) && SymbolTable::isDataLink(vright[index])){ expr = QString("%1(%2,%3,%4, %5)").arg(operation).arg(vright.toString(index)). arg(additionalInfo(ctx,vright.toString(index))). arg(_value.toDouble(index)).arg(relation); } else if (SymbolTable::isDataLink(_value[index]) && SymbolTable::isDataLink(vright[index])) { expr = QString("%1(%2,%3,%4,%5)").arg(operation).arg(vright.toString(index)).arg(_value.toString(index)).arg(relation); } else if (SymbolTable::isDataLink(vright[index]) && SymbolTable::isNumerical(_value[index])) { expr = QString("%1(%2,%3,%4,%5)").arg(operation).arg(vright.toString(index)).arg(_value.toDouble(index)).arg(relation); } bool ok = Ilwis::commandhandler()->execute(expr, ctx,symbols); if ( !ok || ctx->_results.size() != 1) return false; _value = {ctx->_results[0], NodeValue::ctID}; return true; }
void NodeValue::replaceAllUsesOfWith(NodeValue v) { if (v.getNode()) { assert(getType() == v.getType() && "Replacing value with the wrong type"); } auto &users = node_->getUsers(); llvm::SmallVector<NodeUse, 4> usersVec(users.begin(), users.end()); for (auto &U : usersVec) { NodeValue *site = U.get(); assert(site->getNode() == node_ && "Invalid user"); if (site->getResNo() == getResNo()) { site->setOperand(v.getNode(), v.getResNo()); } } }
static void checkType(NodeValue A, ElemKind expectedType) { assert(A.getElementType() == expectedType && "Invalid type"); }
bool AssignmentNode::evaluate(SymbolTable& symbols, int scope, ExecutionContext *ctx) { if ( _expression.isNull()) return false; try{ bool ok = _expression->evaluate(symbols, scope, ctx); if ( ok) { // we save the additional info as we might need it but for the rest clear // the results as the result of the assignment node is a newly filled ctx auto additionalInfo = ctx->_additionalInfo; ctx->clear(true); NodeValue val = _expression->value(); for(int i = 0; i < val.size(); ++i) { Symbol sym = symbols.getSymbol(val.id(i),SymbolTable::gaREMOVEIFANON); IlwisTypes tp = sym.isValid() ? sym._type : itUNKNOWN; QString result = _outParms->id(i); if ( hasType(tp, itILWISOBJECT | itCOLUMN)) { if ( hasType(tp, itRASTER)) { ok &= copyObject<RasterCoverage>(sym, result,symbols); } else if (hasType(tp, itFEATURE)) ok &= copyObject<FeatureCoverage>(sym, result,symbols); else if (hasType(tp, itCOORDSYSTEM)) ok &= copyObject<CoordinateSystem>(sym, result,symbols); else if ( hasType(tp, itDOMAIN)){ ok &= copyObject<Domain>(sym, result,symbols); } else if ( hasType(tp, itGEOREF)){ ok &= copyObject<GeoReference>(sym, result,symbols); } else if (hasType(tp, itTABLE | itCOLUMN)){ ok &= copyObject<Table>(sym, result,symbols,true); QSharedPointer<Selector> selector = _outParms->selector(result); if (!selector.isNull()){ QString varName = selector->variable(); ITable source = sym._var.value<ITable>(); QString oldColName = additionalInfo[source->name()].toString(); QVariant newT= symbols.getValue(result); ITable newTable = newT.value<ITable>(); ColumnDefinition& coldef = newTable->columndefinitionRef(oldColName); if ( coldef.isValid()){ coldef.name(varName); } } } if(!ok) { throw ErrorObject(QString(TR(ERR_OPERATION_FAILID1).arg("assignment"))); } QSharedPointer<ASTNode> specifier = _outParms->specifier(_outParms->id(i)); if ( !specifier.isNull()) { if ( specifier->noOfChilderen()!= 1) return ERROR2(ERR_NO_OBJECT_TYPE_FOR_2, "Output object", "expression"); store2Format(specifier, sym, result); } ctx->_results.push_back(result); } else { sym = symbols.getSymbol(result,SymbolTable::gaREMOVEIFANON); tp = sym.isValid() ? sym._type : itUNKNOWN; if ( tp == itUNKNOWN) { tp = Domain::ilwType(val); } } //ctx->addOutput(symbols,_expression->value(),result, tp, Resource()); } } return ok; } catch(const ErrorObject&){ } return false; }
void UnownedNodeValueMap::insert(NodeValue from, NodeValue to) { entries_.push_front( {{from.getNode(), from.getResNo()}, {to.getNode(), to.getResNo()}}); }
void NodeManager::reclaimZombies() { // FIXME multithreading Assert(!d_attrManager->inGarbageCollection()); Debug("gc") << "reclaiming " << d_zombies.size() << " zombie(s)!\n"; // during reclamation, reclaimZombies() is never supposed to be called Assert(! d_inReclaimZombies, "NodeManager::reclaimZombies() not re-entrant!"); // whether exit is normal or exceptional, the Reclaim dtor is called // and ensures that d_inReclaimZombies is set back to false. ScopedBool r(d_inReclaimZombies); // We copy the set away and clear the NodeManager's set of zombies. // This is because reclaimZombie() decrements the RC of the // NodeValue's children, which may (recursively) reclaim them. // // Let's say we're reclaiming zombie NodeValue "A" and its child "B" // then becomes a zombie (NodeManager::markForDeletion(B) is called). // // One way to handle B's zombification would be simply to put it // into d_zombies. This is what we do. However, if we were to // concurrently process d_zombies in the loop below, such addition // may be invisible to us (B is leaked) or even invalidate our // iterator, causing a crash. So we need to copy the set away. vector<NodeValue*> zombies; zombies.reserve(d_zombies.size()); remove_copy_if(d_zombies.begin(), d_zombies.end(), back_inserter(zombies), NodeValueReferenceCountNonZero()); d_zombies.clear(); #ifdef _LIBCPP_VERSION NodeValue* last = NULL; #endif for(vector<NodeValue*>::iterator i = zombies.begin(); i != zombies.end(); ++i) { NodeValue* nv = *i; #ifdef _LIBCPP_VERSION // Work around an apparent bug in libc++'s hash_set<> which can // (very occasionally) have an element repeated. if(nv == last) { continue; } last = nv; #endif // collect ONLY IF still zero if(nv->d_rc == 0) { if(Debug.isOn("gc")) { Debug("gc") << "deleting node value " << nv << " [" << nv->d_id << "]: "; nv->printAst(Debug("gc")); Debug("gc") << endl; } // remove from the pool kind::MetaKind mk = nv->getMetaKind(); if(mk != kind::metakind::VARIABLE) { poolRemove(nv); } // whether exit is normal or exceptional, the NVReclaim dtor is // called and ensures that d_nodeUnderDeletion is set back to // NULL. NVReclaim rc(d_nodeUnderDeletion); d_nodeUnderDeletion = nv; // remove attributes { // notify listeners of deleted node TNode n; n.d_nv = nv; nv->d_rc = 1; // so that TNode doesn't assert-fail for(vector<NodeManagerListener*>::iterator i = d_listeners.begin(); i != d_listeners.end(); ++i) { (*i)->nmNotifyDeleteNode(n); } // this would mean that one of the listeners stowed away // a reference to this node! Assert(nv->d_rc == 1); } nv->d_rc = 0; d_attrManager->deleteAllAttributes(nv); // decr ref counts of children nv->decrRefCounts(); if(mk == kind::metakind::CONSTANT) { // Destroy (call the destructor for) the C++ type representing // the constant in this NodeValue. This is needed for // e.g. CVC4::Rational, since it has a gmp internal // representation that mallocs memory and should be cleaned // up. (This won't delete a pointer value if used as a // constant, but then, you should probably use a smart-pointer // type for a constant payload.) kind::metakind::deleteNodeValueConstant(nv); } free(nv); } } }/* NodeManager::reclaimZombies() */
static void verifyCrossEntropyLoss(NodeValue P, NodeValue CE, NodeValue labels) { assert(P.getElementType() == CE->getElementType()); assert(P.dims()[0] == labels.dims()[0] && "Invalid shape"); }
static void verifySoftMax(NodeValue src, NodeValue dest) { checkSameType(src, dest); assert(src.dims() == dest.dims() && "Invalid shape"); }
/// Check that the type of the first operand matches the type of the second /// operand. static void checkSameType(NodeValue A, NodeValue B) { assert(A.getType() == B.getType() && "Invalid type"); }
/// Check that the shape of the first operand matches the shape of the second /// operand. static void checkSameShape(NodeValue A, NodeValue B) { assert(A.dims() == B.dims() && "Invalid shape"); }