Skip to content

Cumulative sum (cumsum) op needed #813

Closed

Description

@langmore

Activity

  1. benoitsteiner commented on Jan 23, 2016

    @benoitsteiner
    Contributor

    You can achieve what you're looking for by padding your input with zeros and then convolving the padded tensor with ones. For example, if you have a vector of N elements, you can start by padding it with N-1 elements on the left, and the launch a 1D convolution with a kernel of size N.

    Of course that won't be very efficient, but that can get you going for the time being.

  2. ibab commented on Feb 3, 2016

    @ibab
    Contributor

    I'd like to learn how to write ops by implementing this.
    I'll work based on the adding_an_op section in the docs.

    Can someone give me a few pointers on where this should end up in the tensorflow source tree?
    The op itself would probably be registered in core/ops/math_ops.cc.
    Should I put the kernel into a new core/kernels/cumsum_op.cc file?

  3. girving commented on Feb 3, 2016

    @girving
    Contributor

    If we do this, it should probably have a name pattern similar to reduce_sum, reduce_prod, etc. Maybe accumulate_sum, accumulate_prod (somewhat following numpy)? @dave-andersen: Do you have opinions here?

    Also, I'm not sure if it should be inclusive or exclusive.

    Once we figure that out: yes, the op definitions would go in core/ops/math_ops.cc, and the kernel files would have a similar pattern to the reduction ops in core/kernels.

  4. leconteur commented on Feb 3, 2016

    @leconteur

    I did the following in a personal project. It is not very efficient but it's probably a little better than the convolution.

    https://gist.github.com/leconteur/82f968f483ddd2234a03

  5. langmore commented on Feb 17, 2016

    @langmore
    ContributorAuthor

    Hello ibab. Have you been able to make any progress on this? Thanks!

  6. ibab commented on Feb 17, 2016

    @ibab
    Contributor

    Hi, I haven't made a lot of progress yet, but I've spent some time understanding the implementation of the reduce kernels.
    Writing accumulate kernels should be a bit more work, as there's no corresponding function in Eigen that we can delegate the operation to.

  7. girving commented on Feb 17, 2016

    @girving
    Contributor

    @benoitsteiner: Does eigen have accumulate (scan) capability?

  8. ibab commented on Mar 12, 2016

    @ibab
    Contributor

    I've needed cumsum and cumprod ops today and wrote the following based on @leconteur's code above:

    import tensorflow as tf
    
    def cumsum(xs):
        values = tf.unpack(xs)
        out = []
        prev = tf.zeros_like(values[0])
        for val in values:
            s = prev + val
            out.append(s)
            prev = s
        result = tf.pack(out)
        return result

    This seems to give me relatively decent speed:

    s = tf.Session()
    X = np.random.uniform(0, 1, size=(10000, 1000))
    v = tf.Variable(X)
    s.run(tf.initialize_all_variables())
    y = cumsum(v)
    
     [2]: %timeit s.run(y)
    10 loops, best of 3: 110 ms per loop
     [3]: %timeit np.cumsum(X, axis=0)
    10 loops, best of 3: 80.3 ms per loop

    It would still be nice to have dedicated (and parallel?) ops for this, though.
    Maybe @benoitsteiner can comment on whether adding an accumulate implementation to Eigen would make sense. I might have time to contribute that.

  9. langmore commented on Mar 14, 2016

    @langmore
    ContributorAuthor

    I don't know the tensorflow internals, but it seems to me that this would be very slow. Your for loop adds 10,000 nodes to the graph, and the cumsum evaluation requires evaluation of 10,000 separate nodes, rather than one node that calls into an optimized loop. Each node adds two length 1000 vectors, and this is fast, but I don't imagine iterating through 10,000 nodes is fast. I must be misunderstanding, how?

  10. ibab commented on Mar 14, 2016

    @ibab
    Contributor

    I've changed the shape to (10000,) to isolate the overhead from calling that many nodes a bit better.
    Results:

    [2]: %timeit s.run(y)
    10 loops, best of 3: 36.6 ms per loop
     [3]: %timeit np.cumsum(X, axis=0)
    The slowest run took 8.94 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000 loops, best of 3: 28.2 µs per loop
    

    So it seems that although there's considerable overhead, it's smaller than the time required to add the length 1000 vectors above.

  11. benoitsteiner commented on Mar 14, 2016

    @benoitsteiner
    Contributor

    @ibab @girving Eigen currently doesn't provide the functionality needed to efficiently implement an accumulate op. You're more than welcome to contribute the code to Eigen, that would be a very useful extension.

  12. ibab commented on Mar 14, 2016

    @ibab
    Contributor

    Thanks, @benoitsteiner!
    I assume that I could structure it in a similar way to TensorReduction.h, except that accumulate would preserve the original shape of the tensor.
    This might turn out to be too difficult for me, but I'll give it a try over the next few weeks.

  13. 7 remaining items

  14. vrv commented on Jul 18, 2016

    @vrv

    Yup, thanks!!

  15. kbrems commented on Aug 19, 2016

    @kbrems
    Contributor

    As a potential alternative, we have implemented a python-only framework for adding custom operators to tensorflow, the Operator Vectorization Library:
    https://github.com/opveclib/opveclib. Multi-threaded C++ and CUDA code is automatically generated from the python operators, which can be used in a Tensorflow application with only a binary install of tensorflow. We have implemented cumsum and cumprod operators as an example and benchmarks against the new Tensorflow/Eigen scan operators on the original example in this issue are favorable - test code at: https://github.com/opveclib/opveclib/blob/master/opveclib/examples/test_accumulate.py

    input: X = np.random.uniform(0, 1, size=(10000,1000))
    Machine: 48 CPU cores
    GPU - GeForce GTX TITAN
    DEBUG:opveclib:Best numpy time (ms): 148.728299141
    DEBUG:opveclib:Best ovl cpp time (ms): 41.827047
    DEBUG:opveclib:Best ovl cuda time (ms): 4.738103
    DEBUG:opveclib:Best tf + ovl time (ms) on /cpu:0 :62.8859996796
    DEBUG:opveclib:Best tf cumsum time (ms) on /cpu:0 :141.862869263
    DEBUG:opveclib:Best tf + ovl time (ms) on /gpu:0 :4.96506690979
    DEBUG:opveclib:Best tf cumsum time (ms) on /gpu:0 :9.82308387756

  16. ibab commented on Aug 19, 2016

    @ibab
    Contributor

    opveclib is very cool!
    Note that both the CPU and GPU implementation that I've added to Eigen are extremely naive.
    I've been planning to add optimized implementations, but haven't managed to get around to it yet.
    It would be interesting to test the performance again with the optimized implementations.

  17. added
    type:feature
    and removed on Feb 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stat:contribution welcometype:feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions