You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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?
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.
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.
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.
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?
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.
@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.
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.
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
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.
Activity
benoitsteiner commentedon Jan 23, 2016
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.
ibab commentedon Feb 3, 2016
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.ccfile?girving commentedon Feb 3, 2016
If we do this, it should probably have a name pattern similar to
reduce_sum,reduce_prod, etc. Maybeaccumulate_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 incore/kernels.leconteur commentedon Feb 3, 2016
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
langmore commentedon Feb 17, 2016
Hello ibab. Have you been able to make any progress on this? Thanks!
ibab commentedon Feb 17, 2016
Hi, I haven't made a lot of progress yet, but I've spent some time understanding the implementation of the
reducekernels.Writing
accumulatekernels should be a bit more work, as there's no corresponding function in Eigen that we can delegate the operation to.girving commentedon Feb 17, 2016
@benoitsteiner: Does eigen have accumulate (scan) capability?
ibab commentedon Mar 12, 2016
I've needed
cumsumandcumprodops today and wrote the following based on @leconteur's code above:This seems to give me relatively decent speed:
It would still be nice to have dedicated (and parallel?) ops for this, though.
Maybe @benoitsteiner can comment on whether adding an
accumulateimplementation to Eigen would make sense. I might have time to contribute that.langmore commentedon Mar 14, 2016
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?
ibab commentedon Mar 14, 2016
I've changed the shape to
(10000,)to isolate the overhead from calling that many nodes a bit better.Results:
So it seems that although there's considerable overhead, it's smaller than the time required to add the length
1000vectors above.benoitsteiner commentedon Mar 14, 2016
@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.
ibab commentedon Mar 14, 2016
Thanks, @benoitsteiner!
I assume that I could structure it in a similar way to
TensorReduction.h, except thataccumulatewould 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.
chemelnucfin commentedon Mar 17, 2016
Does this article help?
http://http.developer.nvidia.com/GPUGems3/gpugems3_ch39.html
7 remaining items
vrv commentedon Jul 18, 2016
Yup, thanks!!
kbrems commentedon Aug 19, 2016
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
ibab commentedon Aug 19, 2016
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.
Update XNNPACK in XLA
Update XNNPACK in XLA
Update XNNPACK in XLA
Update XNNPACK in XLA
Update XNNPACK in XLA
Update XNNPACK in XLA
Update XNNPACK in XLA
Update XNNPACK in XLA
Update XNNPACK in XLA