Coder Social home page Coder Social logo

Comments (2)

Jutho avatar Jutho commented on June 24, 2024

There is some speed advantage with using @tensor, but it is not that substantial. It arises specifically from the fact that @tensor only works if the contraction is exactly known at compile time, and the different steps can therefore be hardcoded. That's what the macro does, it does some code rewriting at compile time.

If the contraction requires dynamic information, which is not known at compile time, you cannot use @tensor for that.

from tensoroperations.jl.

sents avatar sents commented on June 24, 2024

If you really need the speed and you don't have a lot of different contractions you can call the macro from a generated function. The generated function can dynamically dispatch by handing it a Val type.

Generated functions have to compile a function for every call of different type they get. This means that each contraction with a different signature would have to be recompiled. So using this approach with rapidly changing contraction orders will introduce a lot of compilation overhead.

Here is an example, handing the operators the indices (as Tuples of Ints):

using TensorOperations
@generated function contract_operator!(S,
                                       A,
                                       B,
                                       op,
                                       order_A::Val{K}) where {K}
    leftside = Expr(:call, :*,
                    :(A[$(K.a...)]),
                    :(B[$(K.b...)]),
                    :(op[$(K.op...)]))
    return :(@tensor S[:] = $leftside)
end
S = rand(5,5,5,2,5,5,5,2)
a = rand(5,5,5,5,2)
b = rand(5,5,5,5,2)
op = rand(2,2,2,2)
contraction_order = Val((a = (1,-1,-2,-3,2),
                     b = (-5,-6,1,-7,3),
                     op = (2,3,-4,-8)))
contract_operator!(S,a,b,op, contraction_order);

from tensoroperations.jl.

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.