harbor run -t openthoughts/tasktrove-mix-h8-adversarial-tests-v2-mix-h8-adversarial-0012harbor download openthoughts/tasktrove-mix-h8-adversarial-tests-v2-mix-h8-adversarial-0012solution file
"""
The source code is based on:
NeurASP: Embracing Neural Networks into Answer Set Programming
Zhun Yang, Adam Ishay, Joohyung Lee. Proceedings of the Twenty-Ninth International Joint Conference on Artificial Intelligence
Main track. Pages 1755-1762.
https://github.com/azreasoners/NeurASP
"""
import re
import sys
import time
import clingo
import torch
import torch.nn as nn
import numpy as np
import time
sys.path.append('../')
sys.path.append('../../')
sys.path.append('../../SLASH/')
from operator import itemgetter
from mvpp import MVPP
from sklearn.metrics import confusion_matrix
from tqdm import tqdm
from joblib import Parallel, delayed
import scipy
from itertools import count
def pad_3d_tensor(target, framework, bs, ruleNum, max_classes):
'''
Turns a 3d list with unequal length into a padded tensor or 3D numpy array
@param target: nested list
@param framework: type of the framework (either numpy or pytorch) of the output
@param bs: len of the first dimension of the list
@param ruleNum: max len of the second dimension of the list
@param max_classes: max len of the third dimension of the list
:return:returns a tensor or 3D numpy array
'''
if framework == 'numpy':
padded = torch.tensor([np.hstack((np.asarray(row, dtype=np.float32), [0] * (max_classes - len(row))) ) for batch in target for row in batch]).type(torch.FloatTensor).view(bs, ruleNum, max_classes)
if framework == 'torch':
padded = torch.stack([torch.hstack((row, torch.tensor([0] * (max_classes - len(row)), device="cuda" ) ) ) for batch in target for row in batch ]).view(ruleNum, bs, max_classes)
return padded
def replace_plus_minus_occurences(pi_prime):
"""
For a given query/program replace all occurences of the +- Notation with the ASP readable counterpart eg. digit(0, +i1, -0) -> digit(0, 1, i1, 0)
@param pi_prime: input query/program as string
:return:returns the ASP readable counterpart of the given query/program and a dict of all used operators per npp
"""
pat_pm = r'(\s*[a-z][a-zA-Z0-9_]*)\((\s*[A-Z]*[a-zA-Z0-9_]*\s*),\s*\+([A-Z]*[a-zA-Z0-9_]*\s*),\s*\-([A-Z]*[a-zA-Z0-9_]*\s*)\)' #1 +- p(c|x) posterior
pat_mp = r'(\s*[a-z][a-zA-Z0-9_]*)\((\s*[A-Z]*[a-zA-Z0-9_]*\s*),\s*\-([A-Z]*[a-zA-Z0-9_]*\s*),\s*\+([A-Z]*[a-zA-Z0-9_]*\s*)\)' #2 -+ p(x|c) likelihood
pat_mm = r'(\s*[a-z][a-zA-Z0-9_]*)\((\s*[A-Z]*[a-zA-Z0-9_]*\s*),\s*\-([A-Z]*[a-zA-Z0-9_]*\s*),\s*\-([A-Z]*[a-zA-Z0-9_]*\s*)\)' #3 -- p(x,c) joint
pat_pp = r'(\s*[a-z][a-zA-Z0-9_]*)\((\s*[A-Z]*[a-zA-Z0-9_]*\s*),\s*\+([A-Z]*[a-zA-Z0-9_]*\s*),\s*\+([A-Z]*[a-zA-Z0-9_]*\s*)\)' #3 -- p(c) prior
#pattern for vqa relations with two vars
pat_pm2 = r'([a-z][a-zA-Z0-9_]*)\((\s*[A-Z]*[a-zA-Z0-9_]*\s*),\s*\+\(([a-zA-Z0-9_ ]*,[a-zA-Z0-9_ ]*)\),\s*\-([A-Z]*[a-zA-Z0-9_]*\s*)\)'
#track which operator(+-,-+,--) was used for which npp
npp_operators= {}
for match in re.findall(pat_pm, pi_prime):
if match[0] not in npp_operators:
npp_operators[match[0]] = {}
npp_operators[match[0]][1] = True
for match in re.findall(pat_mp, pi_prime):
if match[0] not in npp_operators:
npp_operators[match[0]] = {}
npp_operators[match[0]][2] = True
for match in re.findall(pat_mm, pi_prime):
if match[0] not in npp_operators:
npp_operators[match[0]] = {}
npp_operators[match[0]][3] = True
for match in re.findall(pat_pp, pi_prime):
if match[0] not in npp_operators:
npp_operators[match[0]] = {}
npp_operators[match[0]][4] = True
for match in re.findall(pat_pm2, pi_prime):
if match[0] not in npp_operators:
npp_operators[match[0]] = {}
npp_operators[match[0]][1] = True
#replace found matches with asp compatible form for npp occuring in rules
#example: digit(0,+A,-N1) -> digit(0,1,A,N1)
pi_prime = re.sub( pat_pm, r'\1(\2,1,\3,\4)', pi_prime)
pi_prime = re.sub( pat_mp, r'\1(\2,2,\3,\4)', pi_prime)
pi_prime = re.sub( pat_mm, r'\1(\2,3,\3,\4)', pi_prime)
pi_prime = re.sub( pat_pp, r'\1(\2,4,\3,\4)', pi_prime)
pi_prime = re.sub( pat_pm2, r'\1(\2,1,\3,\4)', pi_prime)
return pi_prime, npp_operators
def compute_models_splitwise(big_M_split, query_batch_split, dmvpp, method, k, same_threshold, obj_filter, vqa_params):
"""
Computes the potential solutions and P(Q) for a split of a batch.
@param big_M_split:
@param query_batch_split:
@param dmvpp:
@param method:
@param k:
@same_threshold:
@obj_filter:
@vqa_params:
:return: returns the potential solutions, the atom indices for the gradient computation and the probability P(Q)
"""
query_batch_split = query_batch_split.tolist()
#create a list to store the stable models into
model_batch_list_split = []
models_idx_list = []
#iterate over all queries
for bidx, query in enumerate(query_batch_split):
#produce gradients for every data entry
dmvpp.M = big_M_split[:,bidx,:]
dmvpp.normalize_M()
#if len(dmvpp.parameters[ruleIdx]) == 1:
# dmvpp.parameters[ruleIdx] = [dmvpp.parameters[ruleIdx][0][0],1-dmvpp.parameters[ruleIdx][0][0]]
query, _ = replace_plus_minus_occurences(query)
#exact (default): uses all/k-first stable models for WMC
if method == 'exact':
models = dmvpp.find_all_SM_under_query(query)
#uses only one most probable stable model for WMC
elif method == 'most_prob':
models = dmvpp.find_k_most_probable_SM_under_query_noWC(query, k=1)
#uses the k most probable stables models for WMC
elif method == 'top_k':
models = dmvpp.find_k_most_probable_SM_under_query_noWC(query,k=k)
#gradients = dmvpp.mvppLearn(models)
#reduces the npp grounding to the most probable instantiations
elif method == "same":
if obj_filter is not None: #in the vqa case we want to filter out objects which are not in the image
models = dmvpp.find_SM_vqa_with_same(query, k=k, threshold=same_threshold, obj_filter=obj_filter[bidx], vqa_params= vqa_params)
else:
models = dmvpp.find_SM_with_same(query, k=k, threshold=same_threshold)
#reduces the npp grounding to the most probable instantiations and then uses the top k stable models for WMC
elif method == "same_top_k":
models = dmvpp.find_k_most_probable_SM_with_same(query,k=k)
else:
print('Error: the method \'%s\' should be either \'exact\', \'most_prob\',\'top_k\',\'same_top_k\' or \'same\'', method)
#old NeurASP code - Can also be reenabled in SLASH
# #samples atoms from the npp until k stable models are found
# elif method == 'sampling':
# models = dmvpp.sample_query(query, num=k)
# elif method == 'network_prediction':
# models = dmvpp.find_k_most_probable_SM_under_query_noWC(query, k=1)
# check = SLASH.satisfy(models[0], mvpp['program_asp'] + query)
# if check:
# continue
# elif method == 'penalty':
# models = dmvpp.find_all_SM_under_query()
# models_noSM = [model for model in models if not SLASH.satisfy(model, mvpp['program_asp'] + query)]
models_idx = dmvpp.collect_atom_idx(models)
models_idx_list.append(models_idx)
model_batch_list_split.append(models)
return model_batch_list_split, models_idx_list
def compute_vqa_splitwise(big_M_split, query_batch_split, dmvpp, obj_filter_split, k, pred_vector_size, vqa_params):
"""
Computes the gradients, stable models and P(Q) for a split of a batch.
@param networkOutput_split:
@param query_batch_split:
@param mvpp:
@param n:
@param normalProbs:
@param dmvpp:
@param method:
@param opt:
@param k:
:return:returns the gradients, the stable models and the probability P(Q)
"""
query_batch_split = query_batch_split.tolist()
#create a list to store the target predictions into
pred_batch_list_split = []
#iterate over all queries
for bidx, query in enumerate(query_batch_split):
dmvpp.M = big_M_split[:,bidx,:]
dmvpp.normalize_M()
#if len(dmvpp.parameters[ruleIdx]) == 1:
# dmvpp.parameters[ruleIdx] = [dmvpp.parameters[ruleIdx][0][0],1-dmvpp.parameters[ruleIdx][0][0]]
#query, _ = replace_plus_minus_occurences(query)
#find all targets with same
models = dmvpp.find_SM_vqa_with_same(query, k=k, obj_filter= obj_filter_split[bidx], threshold={"relation":1, "name":1, "attr":1}, train=False, vqa_params=vqa_params)
#compute probabilites of targets in the model
# first retrieve the object ids of targets in the model
pred_vec = np.zeros(pred_vector_size)
#if we have a model
if len(models) > 0:
targets = {}
models = [model for model in models if model != []] #remove empty models
#go through all models and check for targets
for model in models:
if len(model)> 1:
model_prob = 0
model_has_npp_atom = False
for atom in model:
#we found the target atom and save the id of the object
if re.match(r'target', atom):
#add target id
target_id = int(re.match(r'target\(([0-9]*)\)',atom)[1])
else:
#if the non target atom is a ground npp atom get its probability and add it
if atom in dmvpp.ga_map:
rule_idx, atom_idx = dmvpp.ga_map[atom]
model_prob += dmvpp.M[rule_idx, atom_idx].log()
model_has_npp_atom = True
# only add model prob for real probability values < 1
if model_has_npp_atom:
#if this is the first model with a target of that id
if target_id not in targets:
targets[target_id] = []
targets[target_id].append(model_prob)
#get the maximum value for each target object
for target_id in targets:
pred_vec[target_id] = torch.tensor(targets[target_id]).max().exp()
#add prediction vector entry for this query to the batch list containing all queries
pred_batch_list_split.append(pred_vec)
return pred_batch_list_split
class SLASH(object):
def __init__(self, dprogram, networkMapping, optimizers, gpu=True):
"""
@param dprogram: a string for a NeurASP program
@param networkMapping: a dictionary maps network names to neural networks modules
@param optimizers: a dictionary maps network names to their optimizers
@param gpu: a Boolean denoting whether the user wants to use GPU for training and testing
"""
# the neural models should be run on a GPU for better performance. The gradient computation is vectorized but in some cases it makes sense
# to run them on the cpu. For example in the case of the MNIST addition we create a lot of small tensors and putting them all on the gpu is significant overhead
# while the computation itself is comparable for small tensors.
# As a rule of thumb you can say that with more date or more npps it makes more sense to use the gpu for fast gradient computation.
self.device = torch.device('cuda' if torch.cuda.is_available() and gpu else 'cpu')
self.grad_comp_device = torch.device('cuda' if torch.cuda.is_available() and gpu else 'cpu')
self.dprogram = dprogram
self.const = {} # the mapping from c to v for rule #const c=v.
self.n = {} # the mapping from network name to an integer n denoting the domain size; n would be 1 or N (>=3); note that n=2 in theory is implemented as n=1
self.max_n = 2 # integer denoting biggest domain of all npps
self.e = {} # the mapping from network name to an integer e
self.domain = {} # the mapping from network name to the domain of the predicate in that network atom
self.normalProbs = None # record the probabilities from normal prob rules
self.networkOutputs = {}
self.networkGradients = {}
self.networkTypes = {}
if gpu==True:
self.networkMapping = {key : nn.DataParallel(networkMapping[key].to(self.device)) for key in networkMapping}
else:
self.networkMapping = networkMapping
self.optimizers = optimizers
# self.mvpp is a dictionary consisting of 4 keys:
# 1. 'program': a string denoting an MVPP program where the probabilistic rules generated from network are followed by other rules;
# 2. 'networkProb': a list of lists of tuples, each tuple is of the form (model, i ,term, j)
# 3. 'atom': a list of list of atoms, where each list of atoms is corresponding to a prob. rule
# 4. 'networkPrRuleNum': an integer denoting the number of probabilistic rules generated from network
self.mvpp = {'networkProb': [], 'atom': [], 'networkPrRuleNum': 0,'networkPrRuleNumWithoutBinary': 0, 'binary_rule_belongings':{}, 'program': '','networkProbSinglePred':{}}
self.mvpp['program'], self.mvpp['program_pr'], self.mvpp['program_asp'], self.npp_operators = self.parse(query='')
self.stableModels = [] # a list of stable models, where each stable model is a list
self.prob_q = [] # a list of probabilites for each query in the batch
def constReplacement(self, t):
""" Return a string obtained from t by replacing all c with v if '#const c=v.' is present
@param t: a string, which is a term representing an input to a neural network
"""
t = t.split(',')
t = [self.const[i.strip()] if i.strip() in self.const else i.strip() for i in t]
return ','.join(t)
def networkAtom2MVPPrules(self, networkAtom, npp_operators):
"""
@param networkAtom: a string of a neural atom
@param countIdx: a Boolean value denoting whether we count the index for the value of m(t, i)[j]
"""
# STEP 1: obtain all information
regex = '^(npp)\((.+)\((.+)\),\((.+)\)\)$'
out = re.search(regex, networkAtom)
network_type = out.group(1)
m = out.group(2)
e, inf_type, t = out.group(3).split(',', 2) # in case t is of the form t1,...,tk, we only split on the second comma
domain = out.group(4).split(',')
inf_type =int(inf_type)
#TODO how do we get the network type?
self.networkTypes[m] = network_type
t = self.constReplacement(t)
# check the value of e
e = e.strip()
e = int(self.constReplacement(e))
n = len(domain)
if n == 2:
n = 1
self.n[m] = n
if self.max_n <= n:
self.max_n = n
self.e[m] = e
self.domain[m] = domain
if m not in self.networkOutputs:
self.networkOutputs[m] = {}
for o in npp_operators[m]:
if o not in self.networkOutputs[m]:
self.networkOutputs[m][o] = {}
self.networkOutputs[m][o][t]= None
# STEP 2: generate MVPP rules
mvppRules = []
# we have different translations when n = 2 (i.e., n = 1 in implementation) or when n > 2
if n == 1:
for i in range(e):
rule = '@0.0 {}({}, {}, {}, {}); @0.0 {}({},{}, {}, {}).'.format(m, i, inf_type, t, domain[0], m, i, inf_type, t, domain[1])
prob = [tuple((m, i, inf_type, t, 0))]
atoms = ['{}({}, {}, {}, {})'.format(m, i,inf_type, t, domain[0]), '{}({},{},{}, {})'.format(m, i, inf_type, t, domain[1])]
mvppRules.append(rule)
self.mvpp['networkProb'].append(prob)
self.mvpp['atom'].append(atoms)
self.mvpp['networkPrRuleNum'] += 1
self.mvpp['networkPrRuleNumWithoutBinary'] += 1
self.mvpp['networkProbSinglePred'][m] = True
elif n > 2:
if m == "attr": #TODO special case hack to map a model to multiple npp's as in the attributes in vqa
self.mvpp['binary_rule_belongings'][self.mvpp['networkPrRuleNum']] = (m,t)
for i in range(e):
rule = ''
prob = []
atoms = []
for j in range(n):
atom = '{}({},{}, {}, {})'.format(m, i, inf_type, t, domain[j])
rule += '@0.0 {}({},{}, {}, {}); '.format(m, i,inf_type, t, domain[j])
prob.append(tuple((m, i, inf_type, t, j)))
atoms.append(atom)
mvppRules.append(rule[:-2]+'.')
self.mvpp['networkProb'].append(prob)
self.mvpp['atom'].append(atoms)
self.mvpp['networkPrRuleNum'] += 1
self.mvpp['networkPrRuleNumWithoutBinary'] += 1
else:
print('Error: the number of element in the domain %s is less than 2' % domain)
return mvppRules
def parse(self, query=''):
dprogram = self.dprogram + query
# 1. Obtain all const definitions c for each rule #const c=v.
regex = '#const\s+(.+)=(.+).'
out = re.search(regex, dprogram)
if out:
self.const[out.group(1).strip()] = out.group(2).strip()
# 2. Generate prob. rules for grounded network atoms
clingo_control = clingo.Control(["--warn=none"])
# 2.1 remove weak constraints and comments
program = re.sub(r'\n:~ .+\.[ \t]*\[.+\]', '\n', dprogram)
program = re.sub(r'\n%[^\n]*', '\n', program)
# 2.2 replace [] with ()
program = program.replace('[', '(').replace(']', ')')
# 2.3 use MVPP package to parse prob. rules and obtain ASP counter-part
mvpp = MVPP(program)
#if mvpp.parameters and not self.normalProbs:
# self.normalProbs = mvpp.parameters
pi_prime = mvpp.pi_prime
#2.4 parse +-Notation and add a const to flag the operation in the npp call
pi_prime = pi_prime.replace(' ','').replace('#const','#const ')
#replace all occurences of the +- calls
pi_prime, npp_operators = replace_plus_minus_occurences(pi_prime)
#extend npps definitions with the operators found
#example: npp(digit(1,X),(0,1,2,3,4,5,6,7,8,9)):-img(X). with a +- and -- call in the program
# -> npp(digit(1,3,X),(0,1,2,3,4,5,6,7,8,9)):-img(X). npp(digit(1,1,X),(0,1,2,3,4,5,6,7,8,9)):-img(X).
#pat_npp = r'(npp\()([a-z]*[a-zA-Z0-9_]*)(\([0-9]*,)([A-Z]*[a-zA-Z0-9_]*\),\((?:[a-z0-9_]*,)*[a-z0-9_]*\)\))(:-[a-z][a-zA-Z0-9_]*\([A-Z][a-zA-Z0-9_]*\))?.'
#can match fact with arity > 1
pat_npp = r'(npp\()([a-z]*[a-zA-Z0-9_]*)(\([0-9]*,)([A-Z]*)([A-Z]*[a-zA-Z0-9_]*\),\((?:[a-z0-9_]*,)*[a-z0-9_]*\)\))(:-[a-z][a-zA-Z0-9_]*\([A-Z][a-zA-Z0-9_,]*\))?.'
def npp_sub(match):
"""
filters program for npp occurances
"""
npp_extended =""
for o in npp_operators[match.group(2)]:
if match.group(6) is None:
body = ""
var_replacement = match.group(4)
elif re.match(r':-[a-z]*\([0-9a-zA-Z_]*,[0-9a-zA-Z_]*\)', match.group(6)):
body = match.group(6)
var_replacement = re.sub(r'(:-)([a-zA-Z0-9]*)\(([a-z0-9A-Z]*,[a-z0-9A-Z]*)\)', r"\3" ,body)
else:
body = match.group(6)
var_replacement = match.group(4)
npp_extended = '{}{}{}{}{}{}{}{}.\n'.format(match.group(1), match.group(2),match.group(3),o,",", var_replacement,match.group(5), body)+ npp_extended
return npp_extended
pi_prime = re.sub(pat_npp, npp_sub, pi_prime)
# 2.5 use clingo to generate all grounded network atoms and turn them into prob. rules
clingo_control.add("base", [], pi_prime)
clingo_control.ground([("base", [])])
#symbolic mappings map the constants to functions in the ASP program
symbols = [atom.symbol for atom in clingo_control.symbolic_atoms]
#iterate over all NPP atoms and extract information for the MVPP program
mvppRules = [self.networkAtom2MVPPrules(str(atom),npp_operators) for atom in symbols if (atom.name == 'npp')]
mvppRules = [rule for rules in mvppRules for rule in rules]
# 3. obtain the ASP part in the original NeurASP program after +- replacements
lines = [line.strip() for line in pi_prime.split('\n') if line and not re.match("^\s*npp\(", line)]
return '\n'.join(mvppRules + lines), '\n'.join(mvppRules), '\n'.join(lines), npp_operators
@staticmethod
def satisfy(model, asp):
"""
Return True if model satisfies the asp program; False otherwise
@param model: a stable model in the form of a list of atoms, where each atom is a string
@param asp: an ASP program (constraints) in the form of a string
"""
asp_with_facts = asp + '\n'
for atom in model:
asp_with_facts += atom + '.\n'
clingo_control = clingo.Control(['--warn=none'])
clingo_control.add('base', [], asp_with_facts)
clingo_control.ground([('base', [])])
result = clingo_control.solve()
if str(result) == 'SAT':
return True
return False
def infer(self, dataDic, query='', mvpp='', dataIdx=None):
"""
@param dataDic: a dictionary that maps terms to tensors/np-arrays
@param query: a string which is a set of constraints denoting a query
@param mvpp: an MVPP program used in inference
"""
mvppRules = ''
facts = ''
# Step 1: get the output of each neural network
for m in self.networkOutputs:
self.networkMapping[m].eval()
for o in self.networkOutputs[m]:
for t in self.networkOutputs[m][o]:
if dataIdx is not None:
dataTensor = dataDic[t][dataIdx]
else:
dataTensor = dataDic[t]
self.networkOutputs[m][o][t] = self.networkMapping[m](dataTensor).view(-1).tolist()
for ruleIdx in range(self.mvpp['networkPrRuleNum']):
probs = [self.networkOutputs[m][inf_type][t][i*self.n[m]+j] for (m, i, inf_type, t, j) in self.mvpp['networkProb'][ruleIdx]]
#probs = [self.networkOutputs[m][inf_type][t][0][i*self.n[m]+j] for (m, i, inf_type, t, j) in self.mvpp['networkProb'][ruleIdx]]
if len(probs) == 1:
mvppRules += '@{:.15f} {}; @{:.15f} {}.\n'.format(probs[0], self.mvpp['atom'][ruleIdx][0], 1 - probs[0], self.mvpp['atom'][ruleIdx][1])
else:
tmp = ''
for atomIdx, prob in enumerate(probs):
tmp += '@{:.15f} {}; '.format(prob, self.mvpp['atom'][ruleIdx][atomIdx])
mvppRules += tmp[:-2] + '.\n'
# Step 3: find an optimal SM under query
dmvpp = MVPP(facts + mvppRules + mvpp)
return dmvpp.
Complete the code above, taking into account the cross-file context provided. Your solution must work correctly with the related files in the project.
Place your complete solution in /app/solution.py