def main():
    device = torch_mlir.mlir_device()
    model = Net()
    tensor = torch.randn((64, 1, 28, 28), requires_grad=True)
    # CHECK: PASS! fwd check
    fwd_path = test.check_ref(model, tensor)

    target = torch.ones((64), dtype=torch.long)
    loss = F.nll_loss

    # CHECK: PASS! back check
    test.check_back(fwd_path, target, loss)

    # CHECK: PASS! fc1_weight_grad check
    test.compare(model.fc1.weight.grad, fwd_path[0].fc1.weight.grad,
                 "fc1_weight_grad")
def main():
    model = Net()
    tensor = torch.randn((64, 1, 28, 28), requires_grad=True)

    # CHECK: PASS! fwd check
    fwd_path = test.check_fwd(model, tensor)

    target = torch.ones((64), dtype=torch.long)
    loss = F.nll_loss

    # CHECK: PASS! back check
    test.check_back(fwd_path, target, loss)

    # CHECK: PASS! weight_grad check
    test.compare(model.conv2.weight.grad, fwd_path[0].conv2.weight.grad,
                 "weight_grad")
    # CHECK: PASS! bias_grad check
    test.compare(model.conv2.bias.grad, fwd_path[0].conv2.bias.grad,
                 "bias_grad")
    # CHECK: PASS! fc1_weight_grad check
    test.compare(model.fc1.weight.grad, fwd_path[0].fc1.weight.grad,
                 "fc1_weight_grad")
t1 = torch.randn((4, 16, 4), device=dev)

t3 = torch.randn((4, 64), device=dev)
t4 = torch.randn((4, 64), device=dev)

t2 = t0 + t1
t5 = t3 + t4

t6 = t5.view((4, 4, 4, 4))
t7 = t2.view((4, 4, 4, 4))

t8 = t6 + t7

t0_cpu = t0.to('cpu')
t1_cpu = t1.to('cpu')

# CHECK: PASS! add_views_0 check
test.compare(t2, t0_cpu + t1_cpu, "add_views_0")

t3_cpu = t3.to('cpu')
t4_cpu = t4.to('cpu')

# CHECK: PASS! add_views_1 check
test.compare(t5, t3_cpu + t4_cpu, "add_views_1")

t6_cpu = t6.to('cpu')
t7_cpu = t7.to('cpu')

# CHECK: PASS! add_views_2 check
test.compare(t8, t6_cpu + t7_cpu, "add_views_2")
x = torch.rand((3, 64, 8, 8), device=dev)
y = x * x
print(y.stride())

dim = [64, 24, 24]
dim = [4, 4, 4]
N = 2
count = dim[0] * dim[1] * dim[2]
sizes = (N, dim[0], dim[1], dim[2])
strides = (1, dim[1] * dim[2], dim[2], 1)
print(count)
t0 = torch.randn((N, count), device=dev)
t0_like = torch.randn((N, count))

t1 = t0.as_strided(sizes, strides)
t1_ref = t0.to('cpu').as_strided(sizes, strides)
t1_like = t0_like.as_strided(sizes, strides)

t1_ref = t1_ref.clone()

# check that the IR has recorded the
# stride properly before invoking JIT
# CHECK: PASS! stride check
test.compare_eq(t1.stride(), t1_like.stride(), "stride")

# CHECK: PASS! as_stride check
test.compare(t1_ref, t1, "as_stride")

# CHECK: PASS! as_stride stride check
test.compare_eq(t1_ref.stride(), t1.to("cpu").stride(), "as_stride stride")
Exemple #5
0
Cout = 4
w = 10
h = 10

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(Cin, Cout, (3,3))

    def forward(self, x):
        x = self.conv1(x)
        output = F.log_softmax(x, dim=1)
        return output

model = Net()
tensor = torch.randn(N, Cin, h, w)

# CHECK: PASS! fwd check
fwd_path = test.check_ref(model, tensor)

loss = torch.nn.NLLLoss()
target = torch.empty(N, 8, 8, dtype=torch.long).random_(0, Cout)

# CHECK: PASS! back check
test.check_back(fwd_path, target, loss)

# CHECK: PASS! weight_grad check
test.compare(model.conv1.weight.grad, fwd_path[0].conv1.weight.grad, "weight_grad")
# CHECK: PASS! bias_grad check
test.compare(model.conv1.bias.grad, fwd_path[0].conv1.bias.grad, "bias_grad")
# -*- Python -*-
# This file is licensed under a pytorch-style license
# See frontends/pytorch/LICENSE for license information.

import torch
import npcomp.frontends.pytorch as torch_mlir
import npcomp.frontends.pytorch.test as test

# RUN: python %s | FileCheck %s

dev = torch_mlir.mlir_device()
t0 = torch.randn((4,4), device=dev)
t1 = torch.randn((4,4), device=dev)

t2 = t0 * t1
#
# Check the result tensor against the CPU
#
t0_cpu = t0.to('cpu')
t1_cpu = t1.to('cpu')
t2_cpu = t2.to('cpu')

print (t0_cpu, " *\n", t1_cpu, " =\n", t2_cpu)

# CHECK: PASS! mul2 check
test.compare(t2, t0_cpu * t1_cpu, "mul2")
Exemple #7
0
# -*- Python -*-
# This file is licensed under a pytorch-style license
# See frontends/pytorch/LICENSE for license information.

import torch
import npcomp.frontends.pytorch as torch_mlir
import npcomp.frontends.pytorch.test as test

# RUN: %PYTHON %s | FileCheck %s

dev = torch_mlir.mlir_device()
t0 = torch.randn((4,4), device=dev)
t1 = torch.randn((4,4), device=dev)

t2 = t0 + t1

#
# Check the result tensor against the CPU
#
t0_cpu = t0.to('cpu')
t1_cpu = t1.to('cpu')
t2_cpu = t2.to('cpu')

print (t0_cpu, " +\n", t1_cpu, " =\n", t2_cpu)

# CHECK: PASS! add2 check
test.compare(t2, t0_cpu + t1_cpu, "add2")
Exemple #8
0
# -*- Python -*-
# This file is licensed under a pytorch-style license
# See frontends/pytorch/LICENSE for license information.

import torch
import npcomp.frontends.pytorch as torch_mlir
import npcomp.frontends.pytorch.test as test

# RUN: python %s | FileCheck %s

dev = torch_mlir.mlir_device()

tensor = torch.randn(2,3).to(dev)
result = tensor.t()

ref_result = tensor.to('cpu').t()
# CHECK: PASS! transpose check
test.compare(ref_result, result, "transpose")
Exemple #9
0
# -*- Python -*-
# This file is licensed under a pytorch-style license
# See frontends/pytorch/LICENSE for license information.

import torch
import npcomp.frontends.pytorch as torch_mlir
import npcomp.frontends.pytorch.test as test

# RUN: %PYTHON %s | FileCheck %s

dev = torch_mlir.mlir_device()
t0 = torch.randn((1, 2, 3, 4), device=dev)
t1 = torch.randn((1, 2, 3, 4), device=dev)
t2 = torch.randn((1, 2, 3, 4), device=dev)

t3 = t0 + t1 + t2

#
# Check the result tensor against the CPU
#
t0_cpu = t0.to('cpu')
t1_cpu = t1.to('cpu')
t2_cpu = t2.to('cpu')
t3_cpu = t3.to('cpu')

print(t0_cpu, " +\n", t1_cpu, " +\n", t2_cpu, " =\n", t3_cpu)

# CHECK: PASS!
test.compare(t3, t0_cpu + t1_cpu + t2_cpu, "add3")
Exemple #10
0
# See frontends/pytorch/LICENSE for license information.

import torch
import npcomp.frontends.pytorch as torch_mlir
import npcomp.frontends.pytorch.test as test

# RUN: %PYTHON %s | FileCheck %s

dev = torch_mlir.mlir_device()

t0 = torch.randn((3,13), device=dev)
t1 = torch.randn((13,5), device=dev)
print(t0.to('cpu'), t1.to('cpu'))
print(torch.mm(t0.to('cpu'), t1.to('cpu')))

t2 = torch.mm(t0, t1)

#
# Check the result tensor against the CPU
#
t0_cpu = t0.to('cpu')
t1_cpu = t1.to('cpu')
t2_cpu = t2.to('cpu')

print (t0_cpu, " *\n", t1_cpu, " =\n", t2_cpu)

ref_tensor = torch.mm(t0_cpu, t1_cpu)
# CHECK: PASS! mm check
test.compare(t2, ref_tensor, "mm")