Not sure if this is an issue or I'm just holding it wrong :-) I've got an alternative approach that seems to work with gradients by building up my output up by catting slices into my tensors, which does seem to work for the grads.
I have two tensors (token_embeddings and image_features), I want to interleave them in some way. It's for a llava like model, so my input tokens are like
[
[this, is, an, <image>],
[two, images, <image>, <image>],
]
where <image> is a sentinel token. What I've got to so far is making my output a flattened tensor [batch * max_seq, dim] my token/image tensors [num_tokens, dim] and [num_images, dim] and forming indices to assign to, ending up looking like this:
output = mx.zeros(batch * max_seq, dim)
output[token_ix] = self.embeddings(token_ids)
output[image_ix] = image_features
It doesn't seem like I can get gradients for this though:
def fn(x):
output = mx.zeros([4])
output[mx.array([0, 1])] = x
return output.sum()
x = mx.array([2,3])
print(fn(x))
grad_fn = mx.grad(fn)
print(grad_fn(x))
gives me
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[310], line 10
7 print(fn(x))
9 grad_fn = mx.grad(fn)
---> 10 print(grad_fn(x))
ValueError: Primitive's vjp not implemented.
Like I said, it seems like there's an alternative in catting things since:
def fn(x, y):
val = mx.concatenate([x[:1, :], y[:, :], x[1:, :]])
return val.sum()
x = mx.array([[2,3], [4, 5]])
y = mx.array([[2,3], [4, 5]])
print(fn(x, y))
grad_fn = mx.grad(fn)
print(grad_fn(x, y))
seems to work.
Not sure if this is an issue or I'm just holding it wrong :-) I've got an alternative approach that seems to work with gradients by building up my output up by catting slices into my tensors, which does seem to work for the grads.
I have two tensors (token_embeddings and image_features), I want to interleave them in some way. It's for a llava like model, so my input tokens are like
where
<image>is a sentinel token. What I've got to so far is making my output a flattened tensor[batch * max_seq, dim]my token/image tensors[num_tokens, dim]and[num_images, dim]and forming indices to assign to, ending up looking like this:It doesn't seem like I can get gradients for this though:
gives me
Like I said, it seems like there's an alternative in catting things since:
seems to work.