Open SamPruden opened 4 years ago
I've taken a quick run at implementing a custom JAX transformation for this. It seems promising so far! This was built with no real planning or testing, and the documentation doesn't really exist for this so I just hacked something together that works. It's probably a very poor implementation and doubtless has bugs and unaccounted for edge cases galore. It seems to be working on the basic things that I've tested it on, but I really only did it to explore the transformation capabilities and to see how hard it is.
This only moves a simple sum reduction up. That means it isn't actually useful for me yet as I need it to be able to move a whole MSE reduction up. That will require significantly more manipulation and I may or may not bother taking a run at that.
If anybody with familiarity with the internal API would like to comment on whether I'm approaching this in the right way, that would be appreciated! I'm wondering if there are some utilities that make this type of transformation easier? Perhaps I should be creating new jaxprs from scratch instead of patching existing ones?
Major things still on my TODO list:
subjaxprs
I may get these things done, or I may never look at this code again.
I don't know how well this would work as an inbuilt automatic transformation or whether JAX would be the correct level to do this on. If it's a good optimisation then I would think it should happen in XLA. There could still be a good reason why this transformation isn't done, and I just haven't found it yet.
def push_up_reductions(fun):
from functools import wraps
from jax import core
from jax import lax
# from jax.util import safe_map
def scan_predicate(eqn):
# TODO: Skip if reduction already in place
# Necessary for doing multiple iterations
if eqn.primitive != lax.scan_p: return False
if eqn.outvars[1] == core.dropvar: return False
# Excluded because I'm too lazy to think about them for now
if eqn.params['reverse']: return False
if eqn.params['unroll'] != 1: return False
return True
def get_dependant_reductions(eqns, var):
# TODO: Handle transparent operations like scalar multiply which reductions can be moved through
dependants = [eqn for eqn in eqns if var in eqn.invars]
return dependants if all(dep.primitive == lax.reduce_sum_p for dep in dependants) else None
def patch_scan_body(closed_jaxpr, axes):
jaxpr = closed_jaxpr.jaxpr
old_outvar = jaxpr.outvars[1]
out_aval = closed_jaxpr.out_avals[1]
newvar = core.gensym([jaxpr])
sumshape = [out_aval.shape[i] for i in range(out_aval.ndim) if not i in axes]
sumvar = newvar(jax.ShapedArray(sumshape, out_aval.dtype))
outvar = newvar(out_aval)
# Add reduction
jaxpr.eqns.append(core.JaxprEqn([old_outvar], [sumvar], lax.reduce_sum_p, {'axes': axes}, None))
# Broadcast to original ndim so that outer reductions can be left as is
dims = tuple(i for i in range(out_aval.ndim) if i not in axes)
shape = tuple(1 if i in axes else out_aval.shape[i] for i in range(out_aval.ndim))
jaxpr.eqns.append(core.JaxprEqn(
[sumvar], [outvar], lax.broadcast_in_dim_p,
{'broadcast_dimensions': dims, 'shape': shape}, None)
)
def patch_eqns(jaxpr):
scans = filter(scan_predicate, jaxpr.eqns)
for scan in scans:
dependants = get_dependant_reductions(jaxpr.eqns, scan.outvars[1])
if dependants == None: continue
# If there are multiple dependant reductions, we can move up the intersection of their axes
movable_shape = set.intersection(*(set(dep.params['axes']) for dep in dependants))
movable_shape.discard(0)
movable_shape = tuple(movable_shape)
if movable_shape == (): continue
# Patch the existing scan body
inner_axes = tuple(a - 1 for a in movable_shape)
patch_scan_body(scan.params['jaxpr'], inner_axes)
def wrapped(*args, **kwargs):
closed_jaxpr = jax.make_jaxpr(fun)(*args, **kwargs)
patch_eqns(closed_jaxpr.jaxpr)
return core.eval_jaxpr(closed_jaxpr.jaxpr, closed_jaxpr.literals, *args)[0]
return wraps(fun)(wrapped)
One reason this hasn't been implemented in XLA might be that there's no native "scan" HLO, so this transformation would be a fairly involved kind of loop-invariant code motion rather than the pattern match that it is for jaxprs. @blakehechtman to maybe comment more?
One reason this hasn't been implemented in XLA might be that there's no native "scan" HLO, so this transformation would be a fairly involved kind of loop-invariant code motion rather than the pattern match that it is for jaxprs.
Ah that's interesting. Would it make sense to introduce a transformation like this into JAX then? I don't know whether it's considered appropriate for JAX to do these types of optimisations itself, or whether the philosophy is that jit
just passes everything transparently to XLA. The floating point differences that it introduces may be an issue too; would it need to be toggleable or even off by default because of that?
Of course, if XLA could detect it that would benefit other frameworks too, so that seems the most appropriate place if it's practical to do.
This is great stuff! Thanks for providing so much detail, and for digging in so readily.
Would it make sense to introduce a transformation like this into JAX then?
So far the philosophy is indeed that JAX doesn't do rewrites spanning multiple primitives. That doesn't always have to be the case, but it helps us keep things simple so it seems worth sticking with as long as we can.
The floating point issues might also make this optimization not always desirable, though I'm not sure. It can be hard to find compiler optimizations that are uniformly profitable. We've seen other similar examples.
But there may be another possibility here: going meta a bit, maybe this is an example of a domain- or problem-specific compiler optimization, and so the right answer is to make optimizations like these as easy as possible for users to add themselves. That way expert users can get exactly the optimizations they want, and we don't need them to be profitable across all reasonable applications (like they might have to be to include in XLA or even JAX core). That could include, for example, some basic pattern-matching-rewrite machinery.
There may be a tradeoff there: we need to be defensive about internal APIs so that we can maintain the ability to revise JAX internals. If we lose the ability to revise things because so many people depend on internal details, JAX will stagnate. But we may be able to find a balance: for example, if these APIs were only used by researchers, there might be a lower expectation of API stability and long-term compatibility. (We've already found that intrepid researchers and other experts end up building cool things with JAX internal APIs, and so mainly this is about expectations management, so that people stay happy even if we have to revise these JAX APIs and revise their code.)
Anyway, just wanted to share those high-level thoughts. Having a user-malleable compiler was one of the original motivating visions behind JAX, and this same thought has come up again recently as well.
does this make even more sense? scan(lambda (carry, sum), _: ((jnp.sin(carry), sum+jnp.sin(carry).mean()) / 10)),(x, 0), None, length=10)[0][1]
excuse my potentially terrible python
Thanks for the detailed reply Matt! I've been sleeping on itl here's where my thoughts are today.
So far the philosophy is indeed that JAX doesn't do rewrites spanning multiple primitives. That doesn't always have to be the case, but it helps us keep things simple so it seems worth sticking with as long as we can.
I can see why! Doing this comprehensively has quite a lot of tricky rules, and I'm not at all looking forward to the prospect of having to write tests for it.
The floating point issues might also make this optimization not always desirable, though I'm not sure. It can be hard to find compiler optimizations that are uniformly profitable. We've seen other similar examples.
Yeah I'm not sure what the policy is here. As I understand it, you do have some parts of fastmath on my default, so I presume it's not an instant deal breaker?
But there may be another possibility here: going meta a bit, maybe this is an example of a domain- or problem-specific compiler optimization, and so the right answer is to make optimizations like these as easy as possible for users to add themselves. That way expert users can get exactly the optimizations they want, and we don't need them to be profitable across all reasonable applications (like they might have to be to include in XLA or even JAX core). That could include, for example, some basic pattern-matching-rewrite machinery.
That type of rewrite machinery would be very welcome. I admit that I'd hoped I might find something like that when I looked into doing this.
I'm not sure leaving this to each user makes sense in this case. The problem is that this is not a good use of my time. If I just wanted to get this problem solved, I should just refactor and live with the two parallel implementations of the model. It would probably take about 20 minutes. I'd have to live with slightly uglier code and the risk of bugs where the two versions get out of sync, but that would be more practical than modifying the compiler. The peace of mind of knowing that it works rather than worrying you may have a bug in your compiler mod is worth a lot on its own. If we left this elegant solution up to the user, I don't think they'd do it.
It's also something that significant number of users may benefit from, but only a few would think to do. It may be doing them a disservice to not give this to them out of the box.
An obvious user of this would be Trax. If I get it working, I may run some benchmarks with their RNN layers and models. If it improves things significantly on their components, they would probably want to implement it. One option would be for them to do it in their project, another would be for JAX to provide it to them.
If this were an official feature you wouldn't have to worry about the internal API problem because you would just maintain this. Would it make sense to provide this as a transformation that can be dropped in as easily as grad
, but that never gets automatically applied? That might be a good balance between ease of use and not changing the default behaviour.
does this make even more sense?
scan(lambda (carry, sum), _: ((jnp.sin(carry), sum+jnp.sin(carry).mean()) / 10)),(x, 0), None, length=10)[0][1]
I woke up thinking a similar thing this morning. I can performance test this as a special case, but my instinct is that it's not going to be worth doing. It's not actually as powerful as the sum_reduce
version because it only deals with summation over the first axis, whereas reduction can go over many. I would hope that XLA would make these about the same anyway.
I'll give a slightly more comprehensive overview of the type of transformation I'm aiming at.
x1 = jnp.ones((10, 5, 8))
x2 = jnp.ones((30, 15, 12))
y = jnp.ones((30, 15, 12))
jax.make_jaxpr(
lambda x1, x2, y: jnp.square(y - lax.scan(lambda c, xs: (jnp.sin(c), jnp.cos(xs)), x1, x2)[1]).mean()
)(x1, x2, y)
Note that I've used 3D inputs, so it can't just be an addition in the carry.
{ lambda ; a b c.
let _ d = scan[ jaxpr={ lambda ; a b.
let c = sin a
d = cos b
in (c, d) }
length=30
linear=(False, False)
num_carry=1
num_consts=0
reverse=False
unroll=1 ] a b
e = sub c d
f = integer_pow[ y=2 ] e
g = reduce_sum[ axes=(0, 1, 2) ] f
h = div g 5400.0
in (h,) }
{ lambda ; a b c.
let _ d = scan[ jaxpr={ lambda ; a b c.
let d = sin a
e = cos b
f = sub c e
g = integer_pow[ y=2 ] f
h = reduce_sum[ axes=(0, 1) ] g
in (d, h) }
length=30
linear=(False, False, False)
num_carry=1
num_consts=0
reverse=False
unroll=1 ] a b c
e = reduce_sum[ axes=(0,) ] d
f = div e 5400.0
in (f,) }
I haven't tested that, but I think that it's correct...
If I'm understanding correctly, is this the same optimization that KeOps performs? http://www.kernel-operations.io/keops/_auto_benchmarks/index.html
They have a lot of examples of potential users of this kind of optimization.
Scanning over reduction can be significantly faster and more memory efficient than reducing over the stacked output of scan. That's hard to articulate so I'll give a simple example.
I've found this to be a very powerful optimisation, but it can be awkward to work with. In my case, the stacked output of the scan is the output of my model, but in the training loop I do an MSE reduction over it to get the loss. Training speed can be significantly improved by pushing the loss reduction up into the scan, but it requires two separate implementations of the model: one for inference, and one for training.
Note that there are two levels of performance improvement available here. One is that moving the reduction into the loop speeds up execution by a factor of ~2. The other is that the output of the scan may well be the memory bottleneck because it's a length * batch_size allocation, and by moving the reduction inside we reduce it to simply a length allocation. In my case this allowed me to increase my batch size by a factor of half of the scan length, which was 16!
It would be great if this optimisation could be done automatically on a single implementation of the model. I don't know where the right place to do that is; perhaps this is an issue better filed with XLA. JAX already does some similar transformations - would it make sense for JAX to do this?
This is a fastmath style optimisation and may well accumulate floating point error.
I'm considering prototyping a custom JAX interpreter to do this for my narrow use case, although I'm not sure that will be a good use of my time compared to just living with the two parallel implementations.
Benchmark code
CPU results
GPU results