def backward(ctx, grad_output): in_shape, = ctx.saved_tensors return MetalBuffer(in_shape, grad_output) # METAL=1 python3 test/test_ops.py TestOps.test_relu if __name__ == "__main__": b1 = MetalBuffer(10, np.ones(10)) b2 = MetalBuffer(10, np.ones(10)) out = MetalBuffer(10, None) mtl_buffer = cmd_buffer() add_shader.encodeToCommandBuffer_primaryTexture_secondaryTexture_destinationTexture_( mtl_buffer, b1.texture, b2.texture, out.texture) mtl_buffer.commit() print(b1.toCPU()) print(b2.toCPU()) print(out.toCPU()) from tinygrad.tensor import Tensor, Device r1 = Tensor([-2, -1, 0, 2, 4], device=Device.METAL) r2 = r1.relu() r3 = r2.sum() r3.backward() print(r1.cpu()) print(r2.cpu()) print(r3.cpu())
#!/usr/bin/env python3 import numpy as np from tinygrad.tensor import Tensor a = Tensor([-2, -1, 0, 1, 2]).ane() print(a.cpu()) b = a.relu() print(b.cpu())