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] Variable creation is slow #99

Open
maxeeem opened this issue Mar 13, 2024 · 1 comment
Open

[Bug] Variable creation is slow #99

maxeeem opened this issue Mar 13, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@maxeeem
Copy link
Collaborator

maxeeem commented Mar 13, 2024

Describe the bug

While debugging NAL-6 tests, I found that one of the reasons for increased runtime when variables are present lies in variable initialization. It takes almost 3x longer to create a Variable than to create a Term.

To Reproduce

Steps to reproduce the behavior:

import timeit

def test_variables(self):
    print(timeit.timeit(lambda: Term('a')))
    print(timeit.timeit(lambda: Variable(VarPrefix.Independent, 'x')))    

> 1.020983292
> 2.712368792

Expected behavior

As KanrenReasoner performs frequent conversions back and forth between Narsese and Logic, this leads to suboptimal performance on tasks involving variables. We need to find a way to handle variables more efficiently.

@maxeeem maxeeem added the bug Something isn't working label Mar 13, 2024
@bowen-xu
Copy link
Collaborator

bowen-xu commented Mar 15, 2024

This is because the complex data-structure I adopted, which might need changes.
The index of each variable is recorded in self._vars_independent.
https://github.com/bowen-xu/PyNARS/blob/52ed0fac4de3ced442685ac435c86f38cd0ebc05/pynars/Narsese/_py/Variable.py#L32-L34
https://github.com/bowen-xu/PyNARS/blob/52ed0fac4de3ced442685ac435c86f38cd0ebc05/pynars/Narsese/_py/Statement.py#L37-L39
https://github.com/bowen-xu/PyNARS/blob/52ed0fac4de3ced442685ac435c86f38cd0ebc05/pynars/Narsese/_py/Compound.py#L55-L55

Through those lines/functions, the ids and indices of variables are computed. For example,
image
The id of each variable is repersented as a number. In the example above, <(&&, <#x-->A>, <#x-->B>, <$y-->C>)==><$y-->D>> contains four variables (#x, #x, $y, $y) (though only two distinct variables), and they are internaly stored in a tuple (0, 0, 1, 1). The indices of the variables are [0, 0, 0], [0, 1, 0], [0, 2, 0], and [1, 0]. There are some routines to maintain this kind of structure and records.

Another example:
image

The reason why I use this structure is that when doing unification, for example, the two variable terms are (&&, <$x --> A>, <$y --> B>) and (&&, <$z --> A>, <$w --> B>), their internal representations are equal (i.e., (&&, <$ --> A>, <$ --> B>) (0, 1) [[0, 0], [1, 0]] (here (0, 1) are the ids of the two variables, and [[0, 0], [1, 0]] are their indices/postions).

But when using Kanran, there seems no need to maintain such a data-structure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants