Thanks to visit codestin.com
Credit goes to github.com

Skip to content

The result of the model does not match the pytorch output #5

@juinshell

Description

@juinshell

hello!
I'm trying to validate your example with pytorch, in which I get some problems.

std::vector<float> ans = {0.0003392257, 0.0014304413, 0.0004299286, 0.0010349639, 0.0020997059,
0.0016049921, 0.0010267848, 0.00042607592, 0.0018747754, 0.0024558322};

Here you give the result of resnet18 with a set of 10 as input, which is consistent with tvm generated workload. However, when I want to use pytorch resnet18 to verify this output, I get a different answer. The code is as follows:

if __name__ == '__main__':
    device = torch.device("cuda")
    model = torch.hub.load('pytorch/vision:v0.10.0', 'resnet18', pretrained=True)
    model.to(device)
    model.eval()

    batch_size = 1
    image_shape = (3, 224, 224)
    data_shape = (batch_size,) + image_shape
    input_data = np.ones(data_shape).astype("float32")
    input_data = input_data * 10
    print(input_data)
    input_data = torch.from_numpy(input_data)
    device = torch.device("cuda")
    input_data = input_data.to(device)

    with torch.no_grad():
        output = model(input_data)
    torch.cuda.synchronize()

    # print(output[0])
    print(output[0][0:10]) // tensor([-5.8784, -1.7790, -0.6576, -4.9093, -3.7112, -1.5704, -7.2738,  2.6429,
                                       // -0.3956, -2.5655], device='cuda:0')

In addition, when I try to use your script tvm_generate_model.py to generate densenet model in NVIDIA CUDA, the output is all zeros by using the following sample:

batch_size = 1
image_shape = (3, 224, 224)
data_shape = (batch_size,) + image_shape
out_shape = (batch_size, num_class)

mod, params = relay.testing.densenet.get_workload(
    densenet_size=169, batch_size=batch_size, image_shape=image_shape
)
opt_level = 3
target = tvm.target.cuda()

with tvm.transform.PassContext(opt_level=opt_level):
    lib = relay.build(mod, target, params=params)
    # graph_json, lib, params = relay.build(mod, target, params=params)  // if I use this style, the code that immediately follows to get the module will report an error, indicating that the module has no function 'default'

graph_json = lib.graph_json
params = lib.get_params()
ctx = tvm.gpu()
module = graph_runtime.GraphModule(lib["default"](ctx))

data = np.ones(data_shape).astype("float32")
data = data * 10
module.set_input("data", data)

module.run()

out = module.get_output(0, tvm.nd.empty(out_shape)).asnumpy()
print(out.flatten()[0:10]) //  [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

I would be very grateful if you could reply!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions