forked from llvm/torch-mlir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eager_mode.py
34 lines (26 loc) · 909 Bytes
/
eager_mode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
# Also available under a BSD-style license. See LICENSE.
import torch
from torch_mlir.eager_mode.torch_mlir_tensor import TorchMLIRTensor
torch_a = torch.randn(5, requires_grad=True)
torch_b = torch.randn(5, requires_grad=True)
torch_c = torch_a + torch_b
torch_d = torch_a * torch_b
torch_e = torch_c / torch_d
torch_loss = torch_e.sum()
print("PyTorch loss: ", torch_loss)
torch_loss.backward()
print("PyTorch grad a: ", torch_a.grad)
print("PyTorch grad b: ", torch_b.grad)
a = TorchMLIRTensor(torch_a)
b = TorchMLIRTensor(torch_b)
c = a + b
d = a * b
e = c / d
loss = e.sum()
print("Torch-MLIR loss: ", loss)
loss.backward()
print("Torch-MLIR grad a: ", a.grad)
print("Torch-MLIR grad b: ", b.grad)