neumannjan / nn-structural-graph-vectorizer-compiler

1 stars 0 forks source link

XOR Example not working #1

Closed LukasZahradnik closed 2 weeks ago

LukasZahradnik commented 2 weeks ago

Hi, I tried to use vectorizer compiler by integrating it into PyNeuraLogic and got some errors on simple templates. For example this one:

from neuralogic.core import Settings
from neuralogic.optim import SGD

from neuralogic.core import Relation, Template, Transformation
from neuralogic.dataset import Dataset, Sample

dataset = Dataset()
template = Template()

template.add_rule((Relation.xor[1, 8] <= Relation.xy[8, 2]) | [Transformation.TANH])  # Add template rule
template.add_rule(Relation.xor / 0 | [Transformation.TANH])

dataset.add_samples(
    [  # Add 4 examples
        Sample(Relation.xor[0], Relation.xy[[0, 0]]),
        Sample(Relation.xor[1], Relation.xy[[0, 1]]),
        Sample(Relation.xor[1], Relation.xy[[1, 0]]),
        Sample(Relation.xor[0], Relation.xy[[1, 1]]),
    ]
)

settings = Settings(optimizer=SGD(lr=0.1), epochs=300)

model = template.build(settings)
ptrch = model.vectorize(dataset)

x = ptrch()
Traceback (most recent call last):
  File "/home/lukas/Workspace/PyNeuraLogic/venv/lib/python3.11/site-packages/compute_graph_vectorize/sources/minimal_api/internal/neuralogic.py", line 185, in visit
    neuron_rule_name = get_rule_or_fact_main_name(parse_rule_or_fact(str(neuron.name)))
                                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lukas/Workspace/PyNeuraLogic/venv/lib/python3.11/site-packages/compute_graph_vectorize/facts/parser.py", line 209, in parse_rule_or_fact
    _fact = _parse_fact(tokens_iter)
            ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lukas/Workspace/PyNeuraLogic/venv/lib/python3.11/site-packages/compute_graph_vectorize/facts/parser.py", line 137, in _parse_fact
    raise ParserError()
compute_graph_vectorize.facts.parser.ParserError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/lukas/Workspace/PyNeuraLogic/xor_test.py", line 27, in <module>
    ptrch = model.vectorize(dataset)
            ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lukas/Workspace/PyNeuraLogic/neuralogic/nn/base.py", line 52, in vectorize
    network = from_neuralogic(built_dataset.samples, settings)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lukas/Workspace/PyNeuraLogic/venv/lib/python3.11/site-packages/compute_graph_vectorize/sources/builders.py", line 32, in from_neuralogic
    minimal = MinimalAPINeuralogicNetwork(samples=samples, settings=settings)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lukas/Workspace/PyNeuraLogic/venv/lib/python3.11/site-packages/compute_graph_vectorize/sources/minimal_api/neuralogic.py", line 69, in __init__
    self._java_neurons_per_layer, self._layers = compute_neuralogic_neurons_per_layer(samples)
                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lukas/Workspace/PyNeuraLogic/venv/lib/python3.11/site-packages/compute_graph_vectorize/sources/minimal_api/internal/neuralogic.py", line 202, in compute_neuralogic_neurons_per_layer
    visit(sample.java_sample.query.neuron if isinstance(sample, NeuralSample) else sample)
  File "/home/lukas/Workspace/PyNeuraLogic/venv/lib/python3.11/site-packages/compute_graph_vectorize/sources/minimal_api/internal/neuralogic.py", line 193, in visit
    visit(inp)
  File "/home/lukas/Workspace/PyNeuraLogic/venv/lib/python3.11/site-packages/compute_graph_vectorize/sources/minimal_api/internal/neuralogic.py", line 187, in visit
    raise ValueError(f"Failed to parse rule name from '{str(neuron.name)}'") from e
ValueError: Failed to parse rule name from '{1, 8} xor :- {8, 2} xy. [transformation=tanh]'
neumannjan commented 2 weeks ago

Fixed the parser and added a test case in 899b233. The problem was the right-hand side fact not having parentheses, which is a test case that I missed.

However, there's another bug further down the pipeline, for which the fix will not be as easy. I will have to do that some other time.

neumannjan commented 2 weeks ago

Did a quick fix in 45d6d6a. Should probably make a test case for it.

neumannjan commented 2 weeks ago

Also: don't forget to always use neuralogic settings with chain_pruning=False, iso_value_compression=False! The compiler has its own versions of these optimizations. If you use the Java ones, it might break the vectorizer. @LukasZahradnik

neumannjan commented 2 weeks ago

The addition of the following lines https://github.com/neumannjan/nn-structural-graph-vectorizer-compiler/blob/45d6d6a2713a1ee7453fef1f3783de5c23b82f7d/src/compute_graph_vectorize/vectorize/pipeline/separate_input_refs.py#L188-L190 is the core of the fix.

neumannjan commented 1 week ago

@LukasZahradnik Another note: The optimizer= + epochs= parameters in NeuraLogic Settings() are ignored, as you need to write the training pipeline for ptrch in PyTorch directly yourself (as you would in PyTorch normally). ptrch is untrained.

LukasZahradnik commented 1 week ago

I only wanted to try out inference without any training, so settings are fine. The PyNeuraLogic integration handles settings for the user soiso compression and pruning is turned off. Settings in the code above are irrelevant for vectorizer