def test_nested_composition_execution(benchmark, executions, mode): benchmark.group = "Nested Composition execution multirun {}".format( executions) # mechanisms A = ProcessingMechanism(name="A", function=AdaptiveIntegrator(rate=0.1)) B = ProcessingMechanism(name="B", function=Logistic) inner_comp = Composition(name="inner_comp") inner_comp.add_linear_processing_pathway([A, B]) inner_comp._analyze_graph() sched = Scheduler(composition=inner_comp) outer_comp = Composition(name="outer_comp") outer_comp.add_node(inner_comp) outer_comp._analyze_graph() sched = Scheduler(composition=outer_comp) # The input dict should assign inputs origin nodes (inner_comp in this case) var = {inner_comp: [[1.0]]} expected = [[0.52497918747894]] if executions > 1: var = [var for _ in range(executions)] if mode == 'Python': f = lambda x: [ outer_comp.execute(x[i], execution_id=i) for i in range(executions) ] res = f(var) if executions > 1 else outer_comp.execute(var) benchmark(f if executions > 1 else outer_comp.execute, var) elif mode == 'LLVM': e = pnlvm.execution.CompExecution(outer_comp, [None for _ in range(executions)]) e.execute(var) res = e.extract_node_output(outer_comp.output_CIM) benchmark(e.execute, var) elif mode == 'PTX': e = pnlvm.execution.CompExecution(outer_comp, [None for _ in range(executions)]) e.cuda_execute(var) res = e.extract_node_output(outer_comp.output_CIM) benchmark(e.cuda_execute, var) assert np.allclose(res, [expected for _ in range(executions)]) assert len(res) == executions
def test_sequence_of_DDM_mechs_in_Composition_Pathway(): myMechanism = DDM( function=DriftDiffusionAnalytical( drift_rate=(1.0), threshold=(10.0), starting_point=0.0, ), name='My_DDM', ) myMechanism_2 = DDM( function=DriftDiffusionAnalytical( drift_rate=2.0, threshold=20.0), name='My_DDM_2' ) myMechanism_3 = DDM( function=DriftDiffusionAnalytical( drift_rate=3.0, threshold=30.0 ), name='My_DDM_3', ) z = Composition( # default_variable=[[30], [10]], pathways=[[ myMechanism, (IDENTITY_MATRIX), myMechanism_2, (FULL_CONNECTIVITY_MATRIX), myMechanism_3 ]], ) result = z.execute(inputs={myMechanism:[40]}) expected_output = [ (myMechanism.input_ports[0].parameters.value.get(z), np.array([40.])), (myMechanism.output_ports[0].parameters.value.get(z), np.array([10.])), (myMechanism_2.input_ports[0].parameters.value.get(z), np.array([10.])), (myMechanism_2.output_ports[0].parameters.value.get(z), np.array([20.])), (myMechanism_3.input_ports[0].parameters.value.get(z), np.array([20.])), (myMechanism_3.output_ports[0].parameters.value.get(z), np.array([30.])), (result[0], np.array([30.])), ] for i in range(len(expected_output)): val, expected = expected_output[i] # setting absolute tolerance to be in accordance with reference_output precision # if you do not specify, assert_allcose will use a relative tolerance of 1e-07, # which WILL FAIL unless you gather higher precision values to use as reference np.testing.assert_allclose(val, expected, atol=1e-08, err_msg='Failed on expected_output[{0}]'.format(i))
def test_previous_value_stored(self): G = LCAMechanism(integrator_mode=True, leak=1.0, noise=0.0, time_step_size=0.02, function=Linear(slope=2.0), self_excitation=1.0, competition=-1.0, initial_value=np.array([[1.0]])) C = Composition(pathways=[G]) G.output_port.value = [0.0] # - - - - - LCAMechanism integrator functions - - - - - # X = previous_value + (rate * previous_value + variable) * self.time_step_size + noise # f(X) = 2.0*X + 0 # - - - - - starting values - - - - - # variable = G.output_port.value + stimulus = 0.0 + 1.0 = 1.0 # previous_value = initial_value = 1.0 # single_run = S.execute([[1.0]]) # np.testing.assert_allclose(single_run, np.array([[2.0]])) np.testing.assert_allclose(C.execute(inputs={G: [[1.0]]}), np.array([[2.0]])) # X = 1.0 + (-1.0 + 1.0)*0.02 + 0.0 # X = 1.0 + 0.0 + 0.0 = 1.0 <--- previous value 1.0 # f(X) = 2.0*1.0 <--- return 2.0, recurrent projection 2.0 np.testing.assert_allclose(C.execute(inputs={G: [[1.0]]}), np.array([[2.08]])) # X = 1.0 + (-1.0 + 3.0)*0.02 + 0.0 # X = 1.0 + 0.04 = 1.04 <--- previous value 1.04 # f(X) = 2.0*1.04 <--- return 2.08 np.testing.assert_allclose(C.execute(inputs={G: [[1.0]]}), np.array([[2.1616]]))