void realValTokenToByteRepsTest() { /* * 'a'(0x61) << 4 | 0 == 0x0610 */ RealValueToken<TokenPolicy<12, 4> > rvt('a'); std::vector<char> bitReps = rvt.toByteReplesentation(); CPPUNIT_ASSERT(bitReps.size() == 2); CPPUNIT_ASSERT(bitReps[0] == 0x06); CPPUNIT_ASSERT(bitReps[1] == 0x10); /* * 'a'(0x61) << 2 | 0 == 0x0184 */ RealValueToken<TokenPolicy<14, 2> > rvt2('a'); bitReps = rvt2.toByteReplesentation(); CPPUNIT_ASSERT(bitReps.size() == 2); CPPUNIT_ASSERT(bitReps[0] == 0x01); CPPUNIT_ASSERT(std::char_traits<char>::to_int_type(bitReps[1]) == 0x84); }
bool VarCollector::VisitBinaryOperator(BinaryOperator *e) { CompilerInstance &CI = FullDirectives->GetCI(e->getLocStart()); // If it's not an assignment, no contamination can occur if (!e->isAssignmentOp()) { return true; } // If we're not changing a pointer, no contamination can occur if (!e->getLHS()->getType()->isPointerType()) { return true; } assert(!e->getLHS()->getType()->isArrayType()); // Get Dominant LHS DeclRef and its expr DeclRefExpr * LDominantRef = NULL; Expr * LDominantExpr = NULL; VarTraverser lvt(e->getLHS(), LDominantRef, LDominantExpr, CI); lvt.TraverseStmt(e->getLHS()); // Shouldn't get this!! // But if there isn't a dominant variable being written to just return // Where on earth is it writing to!? // TODO: Convert to actual error! if (!LDominantRef) { llvm::errs() << "Warning: Found a pointer being modified that we have no " << "idea where came from. Can't determine if private or not!\n"; return true; } // Get Dominant RHS DeclRef and its expr DeclRefExpr * RDominantRef = NULL; Expr * RDominantExpr = NULL; VarTraverser rvt(e->getRHS(), RDominantRef, RDominantExpr, CI); rvt.TraverseStmt(e->getRHS()); // If there isn't a dominant variable now being pointed to, just return if (!RDominantRef) { llvm::errs() << "No dominant right ref\n"; return true; } VarDecl * VDLHS = dyn_cast<VarDecl>(LDominantRef->getDecl()); VarDecl * VDRHS = dyn_cast<VarDecl>(RDominantRef->getDecl()); const Type * T = LDominantExpr->getType().getTypePtr(); string LT = GetType(LDominantExpr); string RT = GetType(RDominantExpr); maybeContaminated(VDLHS, LT, VDRHS, RT, T, e->getLocStart()); return true; }
void VarCollector::HandleArrayInitList(VarDecl *VDLHS, string TLHS, InitListExpr * Inits, SourceLocation StmtLoc) { CompilerInstance &CI = FullDirectives->GetCI(Inits->getLocStart()); for (unsigned i = 0; i < Inits->getNumInits(); i++) { Expr * Current = Inits->getInit(i); InitListExpr * InitList = dyn_cast<InitListExpr>(Current); if (InitList) { HandleArrayInitList(VDLHS, TLHS, InitList, StmtLoc); } else { // Get Dominant RHS DeclRef and its expr DeclRefExpr * RDominantRef = NULL; Expr * RDominantExpr = NULL; VarTraverser rvt(Current, RDominantRef, RDominantExpr, CI); rvt.TraverseStmt(Current); // If there isn't a dominant variable now being pointed to, just return if (!RDominantRef) { continue; } // DEBUG: // Print out the dominant LHS and RHS /* llvm::errs() << "Found init of a pointer:\n"; llvm::errs() << "RHS Ref " << RDominantRef->getDecl()->getName() << " -> " << GetStmtString(RDominantExpr) << " (" << RDominantExpr->getType().getAsString() << ")\n";*/ VarDecl * VDRHS = dyn_cast<VarDecl>(RDominantRef->getDecl()); string TRHS = GetType(RDominantExpr); const Type * T = RDominantExpr->getType()->getUnqualifiedDesugaredType(); maybeContaminated(VDLHS, TLHS, VDRHS, TRHS, T, StmtLoc); } } }
void VarCollector::HandlePointerInit(VarDecl *VDLHS, string TLHS, SourceLocation StmtLoc) { CompilerInstance &CI = FullDirectives->GetCI(StmtLoc); // Get Dominant RHS DeclRef and its expr DeclRefExpr * RDominantRef = NULL; Expr * RDominantExpr = NULL; VarTraverser rvt(VDLHS->getInit(), RDominantRef, RDominantExpr, CI); rvt.TraverseStmt(VDLHS->getInit()); // If there isn't a dominant variable now being pointed to, just return if (!RDominantRef) { return; } // DEBUG: // Print out the dominant LHS and RHS /*llvm::errs() << "Found init of a pointer:\n"; llvm::errs() << "RHS Ref " << RDominantRef->getDecl()->getName() << " -> " << GetStmtString(RDominantExpr) << " (" << RDominantExpr->getType().getAsString() << ")\n";*/ VarDecl * VDRHS = dyn_cast<VarDecl>(RDominantRef->getDecl()); string TRHS = GetType(RDominantExpr); const Type * T = RDominantExpr->getType()->getUnqualifiedDesugaredType(); maybeContaminated(VDLHS, TLHS, VDRHS, TRHS, T, StmtLoc); }
void referenceValTokenToByteRepsTest() { /* * 1 << 4 | 2 == 16 + 2 = 18 == 0x0012 */ ReferenceValueToken<TokenPolicy<12, 4> > rvt(1, 2); std::vector<char> bitReps = rvt.toByteReplesentation(); CPPUNIT_ASSERT(bitReps.size() == 2); CPPUNIT_ASSERT(bitReps[0] == 0x00); CPPUNIT_ASSERT(bitReps[1] == 0x12); /* * 1 << 2 | 2 == 4 + 2 == 6 == 0x0006 */ ReferenceValueToken<TokenPolicy<14, 2> > rvt2(1, 2); bitReps = rvt2.toByteReplesentation(); CPPUNIT_ASSERT(bitReps.size() == 2); CPPUNIT_ASSERT(bitReps[0] == 0x00); CPPUNIT_ASSERT(bitReps[1] == 0x06); }
/* * Permute each categorical variable and predict */ AnyType rf_cat_imp_score::run(AnyType &args) { if (args[0].isNull() || args[7].isNull()) { return Null(); } Tree dt = args[0].getAs<ByteString>(); MutableNativeIntegerVector cat_features; NativeColumnVector con_features; try { if (args[1].isNull()){ // no cat features return Null(); } else { MutableNativeIntegerVector xx_cat = args[1].getAs<MutableNativeIntegerVector>(); cat_features.rebind(xx_cat.memoryHandle(), xx_cat.size()); } if (args[2].isNull()){ con_features.rebind(this->allocateArray<double>(0)); } else { NativeColumnVector xx_con = args[2].getAs<NativeColumnVector>(); con_features.rebind(xx_con.memoryHandle(), xx_con.size()); } } catch (const ArrayWithNullException &e) { // not expect to reach here // if max_surr = 0, nulls are filtered // otherwise, mapped to -1 or NaN return Null(); } MappedIntegerVector cat_n_levels = args[3].getAs<MappedIntegerVector>(); int n_permutations = args[4].getAs<int>(); double y = args[5].getAs<double>(); bool is_classification = args[6].getAs<bool>(); MappedMatrix distributions = args[7].getAs<MappedMatrix>(); // returning MutableNativeColumnVector permuted_predictions( this->allocateArray<double>(cat_n_levels.size())); // permute each and predict NativeRandomNumberGenerator generator; for (int p = 0; p < n_permutations; p ++) { for (Index i = 0; i < cat_n_levels.size(); i ++) { int orig_i = cat_features(i); discrete_distribution<> ddist(distributions.col(i).data(), distributions.col(i).data() + cat_n_levels(i) + 1); variate_generator<NativeRandomNumberGenerator, discrete_distribution<> > rvt(generator, ddist); cat_features(i) = rvt() - 1; // calling NativeIntegerVector for a const cast // see EigenIntegration_impl.hpp in ports for details double prediction = dt.predict_response( NativeIntegerVector(cat_features.memoryHandle()), con_features); double score = 0.; if (is_classification) { score = y - prediction < 1e-3 ? 1. : 0.; } else { score = - (y - prediction) * (y - prediction); } permuted_predictions(i) += score; cat_features(i) = orig_i; } } permuted_predictions /= n_permutations; return permuted_predictions; }
/* * Permute each continuous variable and predict */ AnyType rf_con_imp_score::run(AnyType &args) { if (args[0].isNull() || args[7].isNull()) { return Null(); } Tree dt = args[0].getAs<ByteString>(); NativeIntegerVector cat_features; MutableNativeColumnVector con_features; try { if (args[1].isNull()){ // no cat features cat_features.rebind(this->allocateArray<int>(0)); } else { NativeIntegerVector xx_cat = args[1].getAs<NativeIntegerVector>(); cat_features.rebind(xx_cat.memoryHandle(), xx_cat.size()); } if (args[2].isNull()){ //no con features return Null(); } else { MutableNativeColumnVector xx_con = args[2].getAs<MutableNativeColumnVector>(); con_features.rebind(xx_con.memoryHandle(), xx_con.size()); } } catch (const ArrayWithNullException &e) { // not expect to reach here // if max_surr = 0, nulls are filtered // otherwise, mapped to -1 or NaN return Null(); } // con_splits size = num_con_features x num_bins // When num_con_features = 0, the input will be an empty string that is read // as a ByteString ConSplitsResult<RootContainer> splits_results = args[3].getAs<ByteString>(); int n_permutations = args[4].getAs<int>(); double y = args[5].getAs<double>(); bool is_classification = args[6].getAs<bool>(); MappedMatrix distributions = args[7].getAs<MappedMatrix>(); // returning MutableNativeColumnVector permuted_predictions( this->allocateArray<double>(con_features.size())); // permute each and predict NativeRandomNumberGenerator generator; for (int p = 0; p < n_permutations; p ++) { for (Index i = 0; i < con_features.size(); i ++) { double orig_i = con_features(i); discrete_distribution<> ddist(distributions.col(i).data(), distributions.col(i).data() + distributions.rows()); variate_generator<NativeRandomNumberGenerator, discrete_distribution<> > rvt(generator, ddist); int outcome = rvt(); if (outcome == 0) { con_features(i) = std::numeric_limits<double>::quiet_NaN(); } else if (outcome == static_cast<int>(distributions.rows()) - 1) { // bin value that is larger than the last separator (last value in con_splits) con_features(i) = splits_results.con_splits(i, outcome-2) + 1.; } else { con_features(i) = splits_results.con_splits(i, outcome-1); } // calling NativeColumnVector for a const cast // see EigenIntegration_impl.hpp in ports for details double prediction = dt.predict_response( cat_features, NativeColumnVector(con_features.memoryHandle())); double score = 0.; if (is_classification) { score = y - prediction < 1e-3 ? 1. : 0.; } else { score = - (y - prediction) * (y - prediction); } permuted_predictions(i) += score; con_features(i) = orig_i; } } permuted_predictions /= n_permutations; return permuted_predictions; }