Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug when resetting and executing transfer mechanism on different passes #3142

Open
jongkeesbj opened this issue Dec 11, 2024 · 5 comments
Open

Comments

@jongkeesbj
Copy link
Collaborator

There is a difference in Python mode and compiled mode, which I assume reflects a bug on the compiled side or at least an unimplemented feature that does not throw up a warning message:

When a transfer mechanism has reset_stateful_function_when=pnl.AtTrialStart() but is scheduled to only execute on a later pass, for example pass 5, then python mode will appropriately reset the mechanism at pass 0 and execute the mechanism at pass 5.

In compiled mode, however, the reset does not happen on pass 0 but instead on pass 5 when the mechanism executes. A reproducible example is posted below.

import psyneulink as pnl

# Note, my_input is scheduled to only execute on pass 5!
my_input = pnl.TransferMechanism(
    input_shapes=2,
    integrator_mode=True,
    integration_rate=1,
    reset_stateful_function_when=pnl.AtTrialStart()
)

my_lca = pnl.LCAMechanism(
    input_shapes=2,
    termination_threshold=10,
    termination_measure=pnl.TimeScale.TRIAL,
    execute_until_finished=False
)

my_gate = pnl.ProcessingMechanism(input_shapes=2)

comp = pnl.Composition()
comp.add_linear_processing_pathway([my_input, my_lca, my_gate])

comp.scheduler.add_condition(
    my_input, pnl.AtPass(5)
)

comp.scheduler.add_condition(
    my_lca, pnl.Always()
)

comp.scheduler.add_condition(
    my_gate, pnl.WhenFinished(my_lca)
)

print('Python Mode: ')
comp.run([[1, 0], [0, 1]])
print(comp.results)

comp.reset(clear_results=True)

print('Compiled Mode: ')
comp.run([[1, 0], [0, 1]], execution_mode=pnl.ExecutionMode.LLVMRun)
print(comp.results)
@jvesely
Copy link
Collaborator

jvesely commented Feb 1, 2025

Adding some debug output:

<0/0/0> Reinitializing: Composition-1/TransferMechanism-1
<0/0/0> Considered: Composition-1/TransferMechanism-1: 0
<0/0/0> Considered: Composition-1/LCAMechanism-1: 0
<0/0/0> Considered: Composition-1/ProcessingMechanism-1: 0
<0/0/0> Considered: Composition-1/TransferMechanism-1: 0
<0/0/0> Considered: Composition-1/LCAMechanism-1: 1
<0/0/0> Considered: Composition-1/ProcessingMechanism-1: 0
<0/0/0> Executing: Composition-1/LCAMechanism-1
<0/0/1> Considered: Composition-1/TransferMechanism-1: 0
<0/0/1> Considered: Composition-1/LCAMechanism-1: 0
<0/0/1> Considered: Composition-1/ProcessingMechanism-1: 0
<0/1/0> Considered: Composition-1/TransferMechanism-1: 0
<0/1/0> Considered: Composition-1/LCAMechanism-1: 0
<0/1/0> Considered: Composition-1/ProcessingMechanism-1: 0
<0/1/0> Considered: Composition-1/TransferMechanism-1: 0
<0/1/0> Considered: Composition-1/LCAMechanism-1: 1
<0/1/0> Considered: Composition-1/ProcessingMechanism-1: 0
<0/1/0> Executing: Composition-1/LCAMechanism-1
<0/1/1> Considered: Composition-1/TransferMechanism-1: 0
<0/1/1> Considered: Composition-1/LCAMechanism-1: 0
<0/1/1> Considered: Composition-1/ProcessingMechanism-1: 0
<0/2/0> Considered: Composition-1/TransferMechanism-1: 0
<0/2/0> Considered: Composition-1/LCAMechanism-1: 0
<0/2/0> Considered: Composition-1/ProcessingMechanism-1: 0
<0/2/0> Considered: Composition-1/TransferMechanism-1: 0
<0/2/0> Considered: Composition-1/LCAMechanism-1: 1
<0/2/0> Considered: Composition-1/ProcessingMechanism-1: 0
<0/2/0> Executing: Composition-1/LCAMechanism-1
<0/2/1> Considered: Composition-1/TransferMechanism-1: 0
<0/2/1> Considered: Composition-1/LCAMechanism-1: 0
<0/2/1> Considered: Composition-1/ProcessingMechanism-1: 0
<0/3/0> Considered: Composition-1/TransferMechanism-1: 0
<0/3/0> Considered: Composition-1/LCAMechanism-1: 0
<0/3/0> Considered: Composition-1/ProcessingMechanism-1: 0
<0/3/0> Considered: Composition-1/TransferMechanism-1: 0
<0/3/0> Considered: Composition-1/LCAMechanism-1: 1
<0/3/0> Considered: Composition-1/ProcessingMechanism-1: 0
<0/3/0> Executing: Composition-1/LCAMechanism-1
<0/3/1> Considered: Composition-1/TransferMechanism-1: 0
<0/3/1> Considered: Composition-1/LCAMechanism-1: 0
<0/3/1> Considered: Composition-1/ProcessingMechanism-1: 0
<0/4/0> Considered: Composition-1/TransferMechanism-1: 0
<0/4/0> Considered: Composition-1/LCAMechanism-1: 0
<0/4/0> Considered: Composition-1/ProcessingMechanism-1: 0
<0/4/0> Considered: Composition-1/TransferMechanism-1: 0
<0/4/0> Considered: Composition-1/LCAMechanism-1: 1
<0/4/0> Considered: Composition-1/ProcessingMechanism-1: 0
<0/4/0> Executing: Composition-1/LCAMechanism-1
<0/4/1> Considered: Composition-1/TransferMechanism-1: 0
<0/4/1> Considered: Composition-1/LCAMechanism-1: 0
<0/4/1> Considered: Composition-1/ProcessingMechanism-1: 0
<0/5/0> Considered: Composition-1/TransferMechanism-1: 1
<0/5/0> Considered: Composition-1/LCAMechanism-1: 0
<0/5/0> Considered: Composition-1/ProcessingMechanism-1: 0
<0/5/0> Executing: Composition-1/TransferMechanism-1
<0/5/1> Considered: Composition-1/TransferMechanism-1: 0
<0/5/1> Considered: Composition-1/LCAMechanism-1: 1
<0/5/1> Considered: Composition-1/ProcessingMechanism-1: 0
<0/5/1> Executing: Composition-1/LCAMechanism-1
<0/5/2> Considered: Composition-1/TransferMechanism-1: 0
<0/5/2> Considered: Composition-1/LCAMechanism-1: 0
<0/5/2> Considered: Composition-1/ProcessingMechanism-1: 0
<0/6/0> Considered: Composition-1/TransferMechanism-1: 0
<0/6/0> Considered: Composition-1/LCAMechanism-1: 0
<0/6/0> Considered: Composition-1/ProcessingMechanism-1: 0
<0/6/0> Considered: Composition-1/TransferMechanism-1: 0
<0/6/0> Considered: Composition-1/LCAMechanism-1: 1
<0/6/0> Considered: Composition-1/ProcessingMechanism-1: 0
<0/6/0> Executing: Composition-1/LCAMechanism-1
<0/6/1> Considered: Composition-1/TransferMechanism-1: 0
<0/6/1> Considered: Composition-1/LCAMechanism-1: 0
<0/6/1> Considered: Composition-1/ProcessingMechanism-1: 0
<0/7/0> Considered: Composition-1/TransferMechanism-1: 0
<0/7/0> Considered: Composition-1/LCAMechanism-1: 0
<0/7/0> Considered: Composition-1/ProcessingMechanism-1: 0
<0/7/0> Considered: Composition-1/TransferMechanism-1: 0
<0/7/0> Considered: Composition-1/LCAMechanism-1: 1
<0/7/0> Considered: Composition-1/ProcessingMechanism-1: 0
<0/7/0> Executing: Composition-1/LCAMechanism-1
<0/7/1> Considered: Composition-1/TransferMechanism-1: 0
<0/7/1> Considered: Composition-1/LCAMechanism-1: 0
<0/7/1> Considered: Composition-1/ProcessingMechanism-1: 0
<0/8/0> Considered: Composition-1/TransferMechanism-1: 0
<0/8/0> Considered: Composition-1/LCAMechanism-1: 0
<0/8/0> Considered: Composition-1/ProcessingMechanism-1: 0
<0/8/0> Considered: Composition-1/TransferMechanism-1: 0
<0/8/0> Considered: Composition-1/LCAMechanism-1: 1
<0/8/0> Considered: Composition-1/ProcessingMechanism-1: 0
<0/8/0> Executing: Composition-1/LCAMechanism-1
<0/8/1> Considered: Composition-1/TransferMechanism-1: 0
<0/8/1> Considered: Composition-1/LCAMechanism-1: 0
<0/8/1> Considered: Composition-1/ProcessingMechanism-1: 0
<0/9/0> Considered: Composition-1/TransferMechanism-1: 0
<0/9/0> Considered: Composition-1/LCAMechanism-1: 0
<0/9/0> Considered: Composition-1/ProcessingMechanism-1: 0
<0/9/0> Considered: Composition-1/TransferMechanism-1: 0
<0/9/0> Considered: Composition-1/LCAMechanism-1: 1
<0/9/0> Considered: Composition-1/ProcessingMechanism-1: 0
<0/9/0> Executing: Composition-1/LCAMechanism-1
<0/9/1> Considered: Composition-1/TransferMechanism-1: 0
<0/9/1> Considered: Composition-1/LCAMechanism-1: 0
<0/9/1> Considered: Composition-1/ProcessingMechanism-1: 1
<0/9/1> Executing: Composition-1/ProcessingMechanism-1
<1/0/0> Reinitializing: Composition-1/TransferMechanism-1
<1/0/0> Considered: Composition-1/TransferMechanism-1: 0
<1/0/0> Considered: Composition-1/LCAMechanism-1: 0
<1/0/0> Considered: Composition-1/ProcessingMechanism-1: 0
<1/0/0> Considered: Composition-1/TransferMechanism-1: 0
<1/0/0> Considered: Composition-1/LCAMechanism-1: 1
<1/0/0> Considered: Composition-1/ProcessingMechanism-1: 0
<1/0/0> Executing: Composition-1/LCAMechanism-1
<1/0/1> Considered: Composition-1/TransferMechanism-1: 0
<1/0/1> Considered: Composition-1/LCAMechanism-1: 0
<1/0/1> Considered: Composition-1/ProcessingMechanism-1: 0
<1/1/0> Considered: Composition-1/TransferMechanism-1: 0
<1/1/0> Considered: Composition-1/LCAMechanism-1: 0
<1/1/0> Considered: Composition-1/ProcessingMechanism-1: 0
<1/1/0> Considered: Composition-1/TransferMechanism-1: 0
<1/1/0> Considered: Composition-1/LCAMechanism-1: 1
<1/1/0> Considered: Composition-1/ProcessingMechanism-1: 0
<1/1/0> Executing: Composition-1/LCAMechanism-1
<1/1/1> Considered: Composition-1/TransferMechanism-1: 0
<1/1/1> Considered: Composition-1/LCAMechanism-1: 0
<1/1/1> Considered: Composition-1/ProcessingMechanism-1: 0
<1/2/0> Considered: Composition-1/TransferMechanism-1: 0
<1/2/0> Considered: Composition-1/LCAMechanism-1: 0
<1/2/0> Considered: Composition-1/ProcessingMechanism-1: 0
<1/2/0> Considered: Composition-1/TransferMechanism-1: 0
<1/2/0> Considered: Composition-1/LCAMechanism-1: 1
<1/2/0> Considered: Composition-1/ProcessingMechanism-1: 0
<1/2/0> Executing: Composition-1/LCAMechanism-1
<1/2/1> Considered: Composition-1/TransferMechanism-1: 0
<1/2/1> Considered: Composition-1/LCAMechanism-1: 0
<1/2/1> Considered: Composition-1/ProcessingMechanism-1: 0
<1/3/0> Considered: Composition-1/TransferMechanism-1: 0
<1/3/0> Considered: Composition-1/LCAMechanism-1: 0
<1/3/0> Considered: Composition-1/ProcessingMechanism-1: 0
<1/3/0> Considered: Composition-1/TransferMechanism-1: 0
<1/3/0> Considered: Composition-1/LCAMechanism-1: 1
<1/3/0> Considered: Composition-1/ProcessingMechanism-1: 0
<1/3/0> Executing: Composition-1/LCAMechanism-1
<1/3/1> Considered: Composition-1/TransferMechanism-1: 0
<1/3/1> Considered: Composition-1/LCAMechanism-1: 0
<1/3/1> Considered: Composition-1/ProcessingMechanism-1: 0
<1/4/0> Considered: Composition-1/TransferMechanism-1: 0
<1/4/0> Considered: Composition-1/LCAMechanism-1: 0
<1/4/0> Considered: Composition-1/ProcessingMechanism-1: 0
<1/4/0> Considered: Composition-1/TransferMechanism-1: 0
<1/4/0> Considered: Composition-1/LCAMechanism-1: 1
<1/4/0> Considered: Composition-1/ProcessingMechanism-1: 0
<1/4/0> Executing: Composition-1/LCAMechanism-1
<1/4/1> Considered: Composition-1/TransferMechanism-1: 0
<1/4/1> Considered: Composition-1/LCAMechanism-1: 0
<1/4/1> Considered: Composition-1/ProcessingMechanism-1: 0
<1/5/0> Considered: Composition-1/TransferMechanism-1: 1
<1/5/0> Considered: Composition-1/LCAMechanism-1: 0
<1/5/0> Considered: Composition-1/ProcessingMechanism-1: 0
<1/5/0> Executing: Composition-1/TransferMechanism-1
<1/5/1> Considered: Composition-1/TransferMechanism-1: 0
<1/5/1> Considered: Composition-1/LCAMechanism-1: 1
<1/5/1> Considered: Composition-1/ProcessingMechanism-1: 0
<1/5/1> Executing: Composition-1/LCAMechanism-1
<1/5/2> Considered: Composition-1/TransferMechanism-1: 0
<1/5/2> Considered: Composition-1/LCAMechanism-1: 0
<1/5/2> Considered: Composition-1/ProcessingMechanism-1: 0
<1/6/0> Considered: Composition-1/TransferMechanism-1: 0
<1/6/0> Considered: Composition-1/LCAMechanism-1: 0
<1/6/0> Considered: Composition-1/ProcessingMechanism-1: 0
<1/6/0> Considered: Composition-1/TransferMechanism-1: 0
<1/6/0> Considered: Composition-1/LCAMechanism-1: 1
<1/6/0> Considered: Composition-1/ProcessingMechanism-1: 0
<1/6/0> Executing: Composition-1/LCAMechanism-1
<1/6/1> Considered: Composition-1/TransferMechanism-1: 0
<1/6/1> Considered: Composition-1/LCAMechanism-1: 0
<1/6/1> Considered: Composition-1/ProcessingMechanism-1: 0
<1/7/0> Considered: Composition-1/TransferMechanism-1: 0
<1/7/0> Considered: Composition-1/LCAMechanism-1: 0
<1/7/0> Considered: Composition-1/ProcessingMechanism-1: 0
<1/7/0> Considered: Composition-1/TransferMechanism-1: 0
<1/7/0> Considered: Composition-1/LCAMechanism-1: 1
<1/7/0> Considered: Composition-1/ProcessingMechanism-1: 0
<1/7/0> Executing: Composition-1/LCAMechanism-1
<1/7/1> Considered: Composition-1/TransferMechanism-1: 0
<1/7/1> Considered: Composition-1/LCAMechanism-1: 0
<1/7/1> Considered: Composition-1/ProcessingMechanism-1: 0
<1/8/0> Considered: Composition-1/TransferMechanism-1: 0
<1/8/0> Considered: Composition-1/LCAMechanism-1: 0
<1/8/0> Considered: Composition-1/ProcessingMechanism-1: 0
<1/8/0> Considered: Composition-1/TransferMechanism-1: 0
<1/8/0> Considered: Composition-1/LCAMechanism-1: 1
<1/8/0> Considered: Composition-1/ProcessingMechanism-1: 0
<1/8/0> Executing: Composition-1/LCAMechanism-1
<1/8/1> Considered: Composition-1/TransferMechanism-1: 0
<1/8/1> Considered: Composition-1/LCAMechanism-1: 0
<1/8/1> Considered: Composition-1/ProcessingMechanism-1: 0
<1/9/0> Considered: Composition-1/TransferMechanism-1: 0
<1/9/0> Considered: Composition-1/LCAMechanism-1: 0
<1/9/0> Considered: Composition-1/ProcessingMechanism-1: 0
<1/9/0> Considered: Composition-1/TransferMechanism-1: 0
<1/9/0> Considered: Composition-1/LCAMechanism-1: 1
<1/9/0> Considered: Composition-1/ProcessingMechanism-1: 0
<1/9/0> Executing: Composition-1/LCAMechanism-1
<1/9/1> Considered: Composition-1/TransferMechanism-1: 0
<1/9/1> Considered: Composition-1/LCAMechanism-1: 0
<1/9/1> Considered: Composition-1/ProcessingMechanism-1: 1
<1/9/1> Executing: Composition-1/ProcessingMechanism-1

distilled to just the executed parts:

<0/0/0> Reinitializing: Composition-1/TransferMechanism-1
<0/0/0> Executing: Composition-1/LCAMechanism-1
<0/1/0> Executing: Composition-1/LCAMechanism-1
<0/2/0> Executing: Composition-1/LCAMechanism-1
<0/3/0> Executing: Composition-1/LCAMechanism-1
<0/4/0> Executing: Composition-1/LCAMechanism-1
<0/5/0> Executing: Composition-1/TransferMechanism-1
<0/5/1> Executing: Composition-1/LCAMechanism-1
<0/6/0> Executing: Composition-1/LCAMechanism-1
<0/7/0> Executing: Composition-1/LCAMechanism-1
<0/8/0> Executing: Composition-1/LCAMechanism-1
<0/9/0> Executing: Composition-1/LCAMechanism-1
<0/9/1> Executing: Composition-1/ProcessingMechanism-1
<1/0/0> Reinitializing: Composition-1/TransferMechanism-1
<1/0/0> Executing: Composition-1/LCAMechanism-1
<1/1/0> Executing: Composition-1/LCAMechanism-1
<1/2/0> Executing: Composition-1/LCAMechanism-1
<1/3/0> Executing: Composition-1/LCAMechanism-1
<1/4/0> Executing: Composition-1/LCAMechanism-1
<1/5/0> Executing: Composition-1/TransferMechanism-1
<1/5/1> Executing: Composition-1/LCAMechanism-1
<1/6/0> Executing: Composition-1/LCAMechanism-1
<1/7/0> Executing: Composition-1/LCAMechanism-1
<1/8/0> Executing: Composition-1/LCAMechanism-1
<1/9/0> Executing: Composition-1/LCAMechanism-1
<1/9/1> Executing: Composition-1/ProcessingMechanism-1

The transfer mechanism is reset at the beginning of each trial. as requested by AtTrialStart

@jvesely
Copy link
Collaborator

jvesely commented Feb 1, 2025

The generated output matches the above:

Python Mode: 
[[[0.52293998 0.40526519]]

 [[0.4336115  0.46026939]]]
Compiled Mode: 
[[[0.52293998 0.40526519]]

 [[0.52196811 0.44454457]]]

The result of the first input is identical, as is the executed sequence.

Executing only one input also produces matching results:

input=[[1, 0]]
Python Mode: 
[[[0.52293998 0.40526519]]]
Compiled Mode: 
[[[0.52293998 0.40526519]]]

and

input=[[0, 1]]
Python Mode: 
[[[0.40526519 0.52293998]]]
Compiled Mode: 
[[[0.40526519 0.52293998]]]

looks like there's something more happening between the trials

@jongkeesbj
Copy link
Collaborator Author

Could it be an input port (on the my_lca mechanism) and/or an output port (on the my_input mechanism) issue, where even before execution the port gets populated with new/resetted values in Python mode but not in LLVMRun mode?

@jvesely
Copy link
Collaborator

jvesely commented Feb 4, 2025

Could it be an input port (on the my_lca mechanism) and/or an output port (on the my_input mechanism) issue, where even before execution the port gets populated with new/resetted values in Python mode but not in LLVMRun mode?

Yes, I think it's an issue with output port values after reset. Python appears to update the output ports after Mechanism reset, which is not done in compiled mode.
It looks like the first trial gets "lucky" and the output port values after reset would match those that are set during model construction so the compiled mode produces the correct result.
However, after the second reset the compiled mode does not update the output port values until <1/5/0> so the first 5 executions of the LCA would consume incorrect inputs.

inserting some tracking to Python reset function confirms this:

$  python 3142.py 
Python Mode: 
RESET INTEGRATOR START: TransferMechanism-0 AdaptiveIntegrator Function-1 [[0. 0.]] [array([0., 0.])]
RESET INTEGRATOR: TransferMechanism-0 AdaptiveIntegrator Function-1 [[0. 0.]] [array([0., 0.])]
RESET INTEGRATOR START: TransferMechanism-0 AdaptiveIntegrator Function-1 [[1. 0.]] [array([1., 0.])]
RESET INTEGRATOR: TransferMechanism-0 AdaptiveIntegrator Function-1 [[0. 0.]] [array([0., 0.])]
...

The above is printing the mechanism and output port values before and after the call to reset.
The first reset zeros both, but the new values match the old ones.
The second reset also zeros the values, but it overwrites the old values of [[1, 0]]. This is not happening in compiled version.

@jdcpni
Copy link
Collaborator

jdcpni commented Feb 4, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants