class TestLda(unittest.TestCase): def setUp(self): self.lmc = LMC([], [555]) def test_lda(self): self.lmc.lda(0) self.assertEqual(self.lmc.accumulator, 555) self.assertFalse(self.lmc.neg_flag)
class TestBrz(unittest.TestCase): def setUp(self): self.lmc = LMC([], []) def test_0(self): self.lmc.accumulator = 0 self.lmc.brz(5) self.assertEqual(self.lmc.counter, 5) def test_not_0(self): self.lmc.accumulator = 1 self.lmc.brz(5) self.assertEqual(self.lmc.counter, 0)
class TestAdd(unittest.TestCase): def setUp(self): self.lmc = LMC([], [555]) def test_add(self): self.lmc.accumulator = 100 self.lmc.add(0) self.assertEqual(self.lmc.accumulator, 655) def test_add_overflow(self): self.lmc.accumulator = 999 self.lmc.add(0) self.assertEqual(self.lmc.accumulator, 554)
class TestSub(unittest.TestCase): def setUp(self): self.lmc = LMC([], [555]) def test_b_less_than_a(self): self.lmc.accumulator = 999 self.lmc.sub(0) self.assertEqual(self.lmc.accumulator, 444) self.assertFalse(self.lmc.neg_flag) def test_b_greater_than_a(self): self.lmc.accumulator = 100 self.lmc.sub(0) self.assertEqual(self.lmc.accumulator, 545) self.assertTrue(self.lmc.neg_flag)
def in_potential_values(self): lmc = LMC([1, 5, 3], []) lmc.in_out(1) self.assertEqual(lmc.accumulator, 1) lmc.in_out(1) self.assertEqual(lmc.accumulator, 5) lmc.in_out(1) self.assertEqual(lmc.accumulator, 3)
def setup(self, filepath): """Checks the extension and converts the file into mailbox machine code.""" # Adds checker function from file or from batch if self.batch_fp: self.batch_tests, self.potential_values = file_parser.parse_batch_test_file(self.batch_fp) self.checker = self.get_batch_outputs elif self.checker_fp: self.checker = self.get_checker(self.checker_fp) # compiles mailboxes from file self.mailboxes, num_mailboxes = file_parser.get_mailboxes_from_file(filepath) print("Compiled program uses %d/100 mailboxes." % num_mailboxes) self.lmc = LMC(self.potential_values, self.mailboxes, self.max_cycles) # change working directory to filepath so that feedback outputs in right location. os.chdir(ntpath.dirname(filepath) or '.')
class TestInput(unittest.TestCase): def setUp(self): self.lmc = LMC([], []) @patch('lmc.get_input', return_value='5') def test_in(self, _input): self.lmc.in_out(1) self.assertEqual(self.lmc.accumulator, 5) self.assertEqual(self.lmc.inputs, [5]) self.assertFalse(self.lmc.neg_flag) @patch('lmc.get_input', return_value='-1') def test_in_less_than_0(self, _input): self.lmc.in_out(1) self.assertEqual(self.lmc.accumulator, 999) @patch('lmc.get_input', return_value='1000') def test_in_greater_than_999(self, _input): self.lmc.in_out(1) self.assertEqual(self.lmc.accumulator, 0) def in_potential_values(self): lmc = LMC([1, 5, 3], []) lmc.in_out(1) self.assertEqual(lmc.accumulator, 1) lmc.in_out(1) self.assertEqual(lmc.accumulator, 5) lmc.in_out(1) self.assertEqual(lmc.accumulator, 3)
class TestRunCycles(unittest.TestCase): def setUp(self): self.lmc = LMC([55], [901, 902, 105, 902, 000, 100]) def TestExampleProgram(self): inputs, outputs, num_cycles = self.lmc.run_cycles() self.assertEqual(inputs, [55]) self.assertEqual(outputs, [55, 155]) self.assertEqual(num_cycles, 5) self.assertEqual(self.lmc.address_reg, 5) self.assertEqual(self.lmc.counter, 6) self.assertEqual(self.lmc.accumulator, 5) self.assertTrue(self.lmc.halted)
class TestBrp(unittest.TestCase): def setUp(self): self.lmc = LMC([], []) def test_0(self): self.lmc.accumulator = 0 self.lmc.brp(5) self.assertEqual(self.lmc.counter, 5) def test_greater_than_0(self): self.lmc.accumulator = 1 self.lmc.brp(5) self.assertEqual(self.lmc.counter, 5) def test_neg_flag(self): self.lmc.neg_flag = 1 self.lmc.brp(5) self.assertEqual(self.lmc.counter, 0)
class LMCWrapper: def __init__(self, _filepath, _potential_values, max_cycles=50000, **kwargs): self.is_quiet = kwargs.get('quiet', None) self.is_verbose = kwargs.get('verbose', None) self.batch_fp = kwargs.get('batch_fp', None) self.checker_fp = kwargs.get('checker_fp', None) self.has_graph = kwargs.get('graph', None) self.feedback_fp = kwargs.get('feedback', None) self.lmc = None self.checker = None self.batch_tests = None self.filename = ntpath.basename(_filepath) self.inputs_and_cycles = {} self.feedback = "" self.mailboxes = [] self.potential_values = _potential_values self.max_cycles = max_cycles self.total_cycles = 0 self.unexpected_outputs = [] self.setup(_filepath) def setup(self, filepath): """Checks the extension and converts the file into mailbox machine code.""" # Adds checker function from file or from batch if self.batch_fp: self.batch_tests, self.potential_values = file_parser.parse_batch_test_file( self.batch_fp) self.checker = self.get_batch_outputs elif self.checker_fp: self.checker = self.get_checker(self.checker_fp) # compiles mailboxes from file self.mailboxes, num_mailboxes = file_parser.get_mailboxes_from_file( filepath) print("Compiled program uses %d/100 mailboxes." % num_mailboxes) self.lmc = LMC(self.potential_values, self.mailboxes, self.max_cycles) # change working directory to filepath so that feedback outputs in right location. os.chdir(ntpath.dirname(filepath) or '.') @staticmethod def get_checker(checker_filepath): """Gets the checker function at the given file_path.""" os.chdir(os.path.dirname(__file__)) spec = importlib.util.spec_from_file_location("divisors.py", checker_filepath) foo = importlib.util.module_from_spec(spec) spec.loader.exec_module(foo) return foo.checker def get_batch_outputs(self, _inputs): return self.batch_tests.pop(0)[2] def print_mailboxes(self): print(self.mailboxes) def run_batch(self): """ Runs lmc program with given inputs (if any), until all inputs have been used. Checks the outputs to ensure they match those expected (--checker used). Plots a graph of input against cycles taken (--graph used). """ if len(self.potential_values) == 0: self.run_once() # run the program repeatedly for multiple input values while len(self.potential_values) > 0: self.run_program() if self.checker: if len(self.unexpected_outputs) == 0: print("The program passed all tests.") else: print("The program failed %d tests." % len(self.unexpected_outputs)) for inputs, expected_outputs, outputs in self.unexpected_outputs: inputs = ', '.join(str(val) for val in inputs) expected_outputs = ', '.join( str(val) for val in expected_outputs) outputs = ', '.join(str(val) for val in outputs) print("inputs: %s, expected_output: %s, output: %s" % (inputs, expected_outputs, outputs)) self.write_feedback() # print average and worst case number of cycles average_cycles = sum(self.inputs_and_cycles.values()) // len( self.inputs_and_cycles.keys()) worst_cycles = max(self.inputs_and_cycles.values()) print("Average number of cycles: %d" % average_cycles) print("Worst case number of cycles: %d" % worst_cycles) # plot graph if self.has_graph: plot.plot_graph(self.inputs_and_cycles) def run_once(self): """Runs the program once, without resetting.""" self.run_program() self.write_feedback() def run_program(self): """Runs the lmc program.""" if self.batch_tests: self.feedback += f"Attempting to run test {self.batch_tests[0][0]}:\n" self.lmc.max_cycles = self.batch_tests[0][3] inputs, outputs, num_cycles = self.lmc.run_cycles() self.lmc.reset() self.total_cycles += num_cycles self.store_feedback_msg(inputs, outputs, num_cycles) # matches inputs with number of cycles taken for that input if len(inputs) == 1: self.inputs_and_cycles[inputs[0]] = num_cycles elif len(inputs) > 1: self.inputs_and_cycles[tuple(inputs)] = num_cycles def store_feedback_msg(self, inputs, outputs, num_cycles): """Generates a report mentioning inputs, expected outputs (with checker) and actual outputs.""" expect_output_msg = '' if self.checker: expected_outputs = self.checker(inputs) expect_output_msg = f"Expected Outputs(s):\t{', '.join(str(val) for val in expected_outputs)}" if outputs != expected_outputs: self.unexpected_outputs.append( (inputs, expected_outputs, outputs)) msg = ( f"Input(s):\t\t{', '.join(str(val) for val in inputs)}\n" f"{expect_output_msg}\n" f"Actual Output(s):\t{', '.join(str(val) for val in outputs)}\n" f"Program executed in {num_cycles} cycles, cumulative {self.total_cycles}.\n\n" ) self.feedback += msg if not self.is_quiet: print(msg) def write_feedback(self): """Write feedback to a txt file.""" if self.feedback_fp is not None: with open(self.feedback_fp or f"feedback_{self.filename}", 'w') as f: f.write(self.feedback)
def _definePlaceholdersEncoderLikelihood(self, s, Ns, Nc, Nw): ''' Parameters ---------- s : tensor-like (Ns) The frequencies we wish to evaluate the likelihood not used yet Ns : int Number of frequencies Nc : int Number of channels Nw : int Number of windows Returns ------- None: Creates a bunch of object attributes related to computing NLL ''' ######## # Data # ######## # Frequencies are a placeholder in case we decide to do stochastic self.s = tf.placeholder(tf.float32, shape=[Ns], name='s_') #The fourier transformed data self.y_fft = tf.placeholder(tf.complex64, shape=[Ns, Nc, None], name='y_fft') ########### # Kernels # ########### with tf.variable_scope('global'): self.LMCkernels = [ LMC(Nc, self.Q, self.R, learnVar=self.learnVar, init_style=self.init_style, unif_bounds=self.unif_bounds) for i in range(self.L) ] ########### # Encoder # ########### with tf.variable_scope('encoder'): self.S_ = tf.Variable(rand.randn(self.batch_size, self.L).astype(np.float32), name='S_') self.scores = tf.nn.softplus(self.S_, name='scores') ########################### # Evaluate log-likelihood # ########################### #Combine the factor UKU matrices self.UKUL = [self.LMCkernels[l].UKU(self.s) for l in range(self.L)] self.UKUstore = tf.stack(self.UKUL) self.UKUstorep = tf.transpose(self.UKUstore, perm=[2, 3, 1, 0]) # Make scores proper dimension self.scores_c = tf.cast(tf.transpose(self.scores), dtype=tf.complex64) self.scores_c1 = tf.expand_dims(self.scores_c, axis=0) self.scores_c2 = tf.expand_dims(self.scores_c1, axis=0) self.scores_c3 = tf.expand_dims(self.scores_c2, axis=0) self.UKUe = tf.expand_dims(self.UKUstorep, axis=-1) #Multiply scores self.prod_uku = tf.multiply(self.scores_c3, self.UKUe) self.prod_ukuT = tf.transpose(self.prod_uku, perm=[4, 3, 2, 0, 1]) self.UKUscores = tf.reduce_sum(self.prod_ukuT, axis=1) #Add in the noise self.noise = tf.cast(1 / self.eta * tf.eye(self.C), tf.complex64) self.UKUnoise_half = tf.add(self.UKUscores, self.noise) self.UKUnoise = 2 * self.UKUnoise_half #Transform Y into the proper shape self.Yp = tf.transpose(self.y_fft, perm=[2, 0, 1]) self.Yp1 = tf.expand_dims(self.Yp, axis=-1) self.Yc = tf.squeeze(tf.conj(self.Yp)) #Get the quadratic form self.SLV = tf.linalg.solve(self.UKUnoise, self.Yp1) self.SLVs = tf.squeeze(self.SLV) self.Quad = tf.multiply(self.SLVs, self.Yc) self.QL = tf.reduce_sum(self.Quad, axis=-1) #Nw x Ns #This is where we do the proper weighting self.llk = tf.reduce_mean(self.QL) #Get log determinant self.LD = tf.linalg.logdet(self.UKUnoise) #Normalization constant self.const = tf.cast(Nc * np.log(np.pi) * -1, tf.complex64) #Add together for final likelihood self.logDet = tf.cast(tf.reduce_mean(self.LD), tf.complex64) self.LogLikelihood = self.const - self.logDet - self.llk self.eval = tf.real(self.LogLikelihood) ################################# # Final negative log-likelihood # ################################# self.NLL = -1.0 * self.eval # Regularization of scores self.reg_scores = 0.01 * tf.nn.l2_loss(self.scores) #Regularization of factors self.reg_features = tf.nn.l2_loss(tf.real(self.bNorm())) ################################################# # Define the final loss with classification etc # # somewhere else # ################################################# ##################### # Stuff for UKUnorm # ##################### #self.sf = tf.placeholder(tf.float32,shape=[None],name='sf_') s_fine = np.arange(1000) / 1000 * 55 self.sf = tf.constant(s_fine.astype(np.float32)) self.UKUL2_norm = [ self.LMCkernels[l].UKU(self.sf) for l in range(self.L) ] self.UKUstore_norm = tf.stack(self.UKUL2_norm) self.UKUstorep_norm = tf.transpose(self.UKUstore_norm, perm=[2, 3, 1, 0]) self.UKUndiv = tf.reduce_sum(tf.abs(self.UKUstorep_norm), axis=3, keepdims=True) self.UKUnorm = tf.divide(self.UKUstorep_norm, tf.cast(self.UKUndiv, tf.complex64))
def setUp(self): self.lmc = LMC([], [])
def setUp(self): self.lmc = LMC([55], [901, 902, 105, 902, 000, 100])