Thoughts about operators and functors
Created by: typhoonzero
I've been writing implementations about manipulating SelectedRows
and Tensor
calculations. Found several places that might be able to improve:
- Operators' "Kernels" are in fact the same as
functors
or can be assembled by several operators or functors. For example, mergeSelectedRows
duplicated rows is a functor used both in adam_op and adagrad_op. And, many operators can also be reused in other operators which are now difficult to do. - Operators and Functors are not “lazy evaluated”. I’ve written some code using Expression templates at here indicating how to do lazy evaluation. If we have Operators/Functors as “Expression templates”, the calculation is done at the last stage and probably using only one for-loop.
How to implement:
- For eigen Operators/Functors, we can return the non evaluated tensor
Operations
inCompute
call of currentKernel
implement. And then evaluate theOperations
only when necessary. Operators need to hold the expression object containing eitherFunctor expression
orEigen Operations
inside it. - For
Functors
that need standalone CUDA kernels, we can use “Expression templates” to implement functor expression. - A
Block
of operators will then be expanded as a large “Functor” containing both Eigen Operations and Functors, it’s type may look like:….Add<Square<SparseAddTo<SparseMul<SelectedRows, SelectedRows>, Tensor>>, Scalar>….