Giter Club home page Giter Club logo

Comments (10)

Daniel63656 avatar Daniel63656 commented on August 15, 2024 1

yes, this helps a lot. The calls to accepts() now only takes about 2,6% of time in my case instead of the 50% it used to be.

from lark.

MegaIng avatar MegaIng commented on August 15, 2024

choices() returns the terminals that have a transition in the state machine from the current state. But following that transition might lead to errors further down the line, see #646

Whether choices() is enough for you depends on what you are trying to do.

from lark.

Daniel63656 avatar Daniel63656 commented on August 15, 2024

I am trying to get all terminal at the step that conform to the grammar. According to the issue, I would need to use accepts() then I guess but the copying overhead is way too much.
I use this in a transformer at each prediction and using this approach doubles the inference time, meaning the parsing takes as much time as running the model (!).
Maybe there is another solution to verify the tokens?

from lark.

MegaIng avatar MegaIng commented on August 15, 2024

I don't think there is a fundamentally different approach that avoids copying the parser state.

But it might be possible to avoid deep-copying the value_stack, which I suspect is the majority of the cost of copying, since everything else should be quite cheap to copy.

from lark.

Daniel63656 avatar Daniel63656 commented on August 15, 2024

Is there no way to "peek" and check if a token would be accepted without advancing the state (thus making copying of any kind obsolete)?

from lark.

erezsh avatar erezsh commented on August 15, 2024

I agree that the deepcopy is the most likely candidate / suspect. I think the deepcopy is there because the callbacks (i.e. inline transformer) might change the objects they are given. But actually in this use-case we don't care about the values. We can even tell the parser not to compute anything in the value_stack, which will cause it to run faster, and avoid the risk of side-effects from the callbacks.

Another optimization for this particular scenario: If the action is a SHIFT (which is quite probable), unwinding the stack only requires a pop, and no copy is needed.

from lark.

Daniel63656 avatar Daniel63656 commented on August 15, 2024

@erezsh yes I can confirm that. Replacing accepts() with choices() reduced the percentage of compute time in my method taken by lark from 50% to almost 0% in my case. But of course this also defeats the purpose if it also returns invalid Tokens.

from lark.

erezsh avatar erezsh commented on August 15, 2024

Sorry, my message was to @MegaIng .

Anyway, I think we can make accepts() pretty fast if we do those things.

from lark.

erezsh avatar erezsh commented on August 15, 2024

@Daniel63656 I created a PR that might solve your performance problems.

Can you give it a try and let me know if it helped?

#1419

from lark.

erezsh avatar erezsh commented on August 15, 2024

There could be more optimizations done, but sounds like it's good enough right now, so I'm closing the issue.

from lark.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.