Conjugate VJP and JVP support (#3386)
This commit is contained in:
@@ -1178,6 +1178,21 @@ std::vector<Shape> Concatenate::output_shapes(
|
||||
return {std::move(shape)};
|
||||
}
|
||||
|
||||
std::vector<array> Conjugate::vjp(
|
||||
const std::vector<array>&,
|
||||
const std::vector<array>& cotangents,
|
||||
const std::vector<int>&,
|
||||
const std::vector<array>&) {
|
||||
return {conjugate(cotangents[0], stream())};
|
||||
}
|
||||
|
||||
std::vector<array> Conjugate::jvp(
|
||||
const std::vector<array>&,
|
||||
const std::vector<array>& tangents,
|
||||
const std::vector<int>&) {
|
||||
return {conjugate(tangents[0], stream())};
|
||||
}
|
||||
|
||||
std::pair<std::vector<array>, std::vector<int>> Conjugate::vmap(
|
||||
const std::vector<array>& inputs,
|
||||
const std::vector<int>& axes) {
|
||||
|
||||
@@ -698,6 +698,7 @@ class Conjugate : public UnaryPrimitive {
|
||||
void eval_gpu(const std::vector<array>& inputs, array& out) override;
|
||||
|
||||
DEFINE_VMAP()
|
||||
DEFINE_GRADS()
|
||||
DEFINE_NAME(Conjugate)
|
||||
DEFINE_DEFAULT_IS_EQUIVALENT()
|
||||
DEFINE_INPUT_OUTPUT_SHAPE()
|
||||
|
||||
@@ -2888,6 +2888,16 @@ class TestOps(mlx_tests.MLXTestCase):
|
||||
out_np = a.conj()
|
||||
self.assertTrue(np.array_equal(np.array(out_mlx), out_np))
|
||||
|
||||
b = np.random.normal(size=shape) + 1j * np.random.normal(size=shape)
|
||||
b = b.astype(np.complex64)
|
||||
|
||||
_, vjps = mx.vjp(mx.conj, [mx.array(a)], [mx.array(b)])
|
||||
self.assertTrue(np.array_equal(np.array(vjps[0]), b.conj()))
|
||||
|
||||
out_mlx, jvps = mx.jvp(mx.conj, [mx.array(a)], [mx.array(b)])
|
||||
self.assertTrue(np.array_equal(np.array(out_mlx[0]), a.conj()))
|
||||
self.assertTrue(np.array_equal(np.array(jvps[0]), b.conj()))
|
||||
|
||||
def test_view(self):
|
||||
# Check scalar
|
||||
out = mx.array(1, mx.int8).view(mx.uint8).item()
|
||||
|
||||
Reference in New Issue
Block a user