intel-analytics / analytics-zoo

Distributed Tensorflow, Keras and PyTorch on Apache Spark/Flink & Ray
https://analytics-zoo.readthedocs.io/
Apache License 2.0
16 stars 3 forks source link

TorchNet can't work with Embedding #1004

Open hkvision opened 5 years ago

hkvision commented 5 years ago

Embedding requires LongTensor?

  1. When calling TorchNet.from_pytorch, if input is a shape tuple, then it will throw error since a random float tensor will be created:
    RuntimeError: Expected tensor for argument intel-analytics/analytics-zoo#1 'indices' to have scalar type Long; but got CPUType instead (while checking arguments for embedding)

    I change to input a torch tensor directly and torchnet can be created.

  2. When I call forward, it throws the same error from native as above. Seems the input of embedding is not correctly handled.

Code to repro:

        model = nn.Embedding(20, 5)
        net = TorchNet.from_pytorch(model, torch.LongTensor(1, 5).random_(0, 20))
        input = np.random.randint(0, 20, [4, 5])
        output = net.forward(input)
hkvision commented 5 years ago

Also, seems we currently support torch 1.1.0, while nlp-architect depends on 1.2.0. Not sure whether there will be potential conflicts.

jason-dai commented 5 years ago

It seems that TorchNet currently only accept float as input: https://github.com/intel-analytics/analytics-zoo/blob/master/zoo/src/main/java/com/intel/analytics/zoo/pipeline/api/net/native/com_intel_analytics_zoo_pipeline_api_net_PytorchModel.cpp#L219

hkvision commented 5 years ago

@jason-dai I tried to change all the corresponding data types from float32 to long and we forward an embedding can work. Seems torch embedding can only accept long type as input, please kindly verify @hhbyyh . Also I may want to ask you for suggestions on the best way to handle different data types in TorchNet. @hhbyyh Thanks.

hkvision commented 5 years ago

@jason-dai After I change input type to Long, now TorchNet can load BERT successfully :). [Need to use torch 1.1.0; 1.2.0, required by nlp-architect will encounter unexpected problems]

hkvision commented 5 years ago

As guided by @hhbyyh when creating torch model, can add a cast to long before passing to embedding. I will make a pr for this after fix migrating pytorch bert.