intel-analytics / ipex-llm

Accelerate local LLM inference and finetuning (LLaMA, Mistral, ChatGLM, Qwen, Baichuan, Mixtral, Gemma, Phi, MiniCPM, etc.) on Intel XPU (e.g., local PC with iGPU and NPU, discrete GPU such as Arc, Flex and Max); seamlessly integrate with llama.cpp, Ollama, HuggingFace, LangChain, LlamaIndex, GraphRAG, DeepSpeed, vLLM, FastChat, Axolotl, etc.
Apache License 2.0
6.63k stars 1.26k forks source link

Method createKerasLSTM does not exist ! #3024

Closed m-developer96 closed 4 years ago

m-developer96 commented 4 years ago

Hi everyone! I created a model with below code script. I tried it several times and it worked well. But suddenly it crashed and didn't run and I couldn't find the reason. The error is this :

Py4JError: An error occurred while calling o30.createKerasLSTM. Trace:
py4j.Py4JException: Method createKerasLSTM([class java.lang.Integer, class java.lang.Integer, class java.lang.String, class java.lang.Boolean, class java.lang.Boolean, null, null, null, null]) does not exist
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:318)
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:326)
    at py4j.Gateway.invoke(Gateway.java:274)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.lang.Thread.run(Thread.java:748)

Please help me with this error. How can I fix this? Thanks :)

from bigdl.util.common import *
from bigdl.nn.layer import *
from bigdl.nn.criterion import *
from bigdl.optim.optimizer import *
from bigdl.util.common import *
from bigdl.util.common import Sample
from bigdl.nn.keras.layer import Dense
from bigdl.nn.keras.layer import *
import bigdl.version
init_engine()
model_type = 'lstm'
p = 0.2
def build_model(class_num):
    model = Sequential()
    if model_type.lower() == "cnn":
        model.add(Reshape([embedding_dim, 1, sequence_len]))
        model.add(SpatialConvolution(embedding_dim, 128, 5, 1))
        model.add(ReLU())
        model.add(SpatialMaxPooling(5, 1, 5, 1))
        model.add(SpatialConvolution(128, 128, 5, 1))
        model.add(ReLU())
        model.add(SpatialMaxPooling(5, 1, 5, 1))
        model.add(Reshape([128]))
    elif model_type.lower() == "lstm":
        model.add(Recurrent()
                  .add(LSTM(embedding_dim, 100, p)))
        model.add(Select(2, -1))
    elif model_type.lower() == "gru":
        model.add(Recurrent()
                  .add(GRU(embedding_dim, 128)))
        model.add(Select(2, -1))
    else:
        raise ValueError('model can only be cnn, lstm, or gru')
    model.add(Flatten())
    model.add(Dense(class_num, name="fc1"))
    model.add(LeakyReLU(alpha=0.3, input_shape=None, name='leaky-relu:0.3'))
    model.add(Dropout(0.5))
    model.add(SoftMax())
    return model
model = build_model(2)
hkvision commented 4 years ago

Hi @m-developer96

The code you refer to should use bigdl.nn.layer.LSTM instead of bigdl.nn.keras.layer.LSTM. You are using mixed imports and thus the former is overridden by the latter. Please make sure you use only one set of API.

In other words, do not use the following:

from bigdl.nn.layer import *
from bigdl.nn.keras.layer import *

Only choose one of them. You can't mix bigd.l.nn.keras.layer.Dense with bigdl.nn.layers. These code segments should be working for bigdl.nn.layers:

model = Sequential()
    if model_type.lower() == "cnn":
        model.add(Reshape([embedding_dim, 1, sequence_len]))
        model.add(SpatialConvolution(embedding_dim, 128, 5, 1))
        model.add(ReLU())
        model.add(SpatialMaxPooling(5, 1, 5, 1))
        model.add(SpatialConvolution(128, 128, 5, 1))
        model.add(ReLU())
        model.add(SpatialMaxPooling(5, 1, 5, 1))
        model.add(Reshape([128]))
    elif model_type.lower() == "lstm":
        model.add(Recurrent()
                  .add(LSTM(embedding_dim, 100, p)))
        model.add(Select(2, -1))
    elif model_type.lower() == "gru":
        model.add(Recurrent()
                  .add(GRU(embedding_dim, 128)))
        model.add(Select(2, -1))
    else:
        raise ValueError('model can only be cnn, lstm, or gru')

If you want to add layers, please use all the layers under bigdl.nn.layers instead of bigdl.nn.keras.layers.

m-developer96 commented 4 years ago

@hkvision Thank you for your attention. I used your solution and imported below packages:

from bigdl.util.common import *
from bigdl.nn.layer import *
from bigdl.nn.criterion import *
from bigdl.optim.optimizer import *
from bigdl.nn.keras.layer import Flatten

But now I faced with this new error:

An error occurred while calling o177.add.
: com.intel.analytics.bigdl.nn.abstractnn.InvalidLayer: Do not mix Sequential[c40b4b27]{
  [input -> (1) -> (2) -> output]
  (1): Recurrent[9b07cab0]ArrayBuffer(LSTM(300, 100, 0.2))
  (2): nn.Select
}(isKerasStyle=false) with Layer
                           (isKerasStyle=true):
         Flatten[2888551e]
    at com.intel.analytics.bigdl.nn.abstractnn.InferShape$class.excludeInvalidLayers(InferShape.scala:98)
    at com.intel.analytics.bigdl.nn.abstractnn.AbstractModule.excludeInvalidLayers(AbstractModule.scala:59)
    at com.intel.analytics.bigdl.nn.abstractnn.InferShape$class.validateInput(InferShape.scala:108)
    at com.intel.analytics.bigdl.nn.abstractnn.AbstractModule.validateInput(AbstractModule.scala:59)
    at com.intel.analytics.bigdl.nn.DynamicContainer.add(DynamicContainer.scala:44)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:282)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.lang.Thread.run(Thread.java:748)

Is there another alternative method forFlatten() in bigdl.nn.layer package? How can I solve this?

hkvision commented 4 years ago

Hi, @m-developer96

As I mentioned before, you can't mix the layers. To be more specific, you can't combine bigdl.nn.keras.layer.Flatten with bigdl.nn.layer.Sequential. So you need to replace Flatten. We don't have a Flatten layer under bigdl.nn.layer but you can use Reshape as an alternative, you can refer to here for the documentation: https://bigdl-project.github.io/0.10.0/#APIGuide/Layers/Simple-Layers/#reshape

hkvision commented 4 years ago

Or you can change to all layers in Keras style, which you are recommended to use Analytics Zoo instead since we have already done something for you and the API is much easier. You can take a look at here: https://github.com/intel-analytics/analytics-zoo/blob/master/pyzoo/zoo/models/textclassification/text_classifier.py#L82

m-developer96 commented 4 years ago

Hi, @hkvision

Thanks, I understood. What method can I use as an alternative of Dense() ?

I tested Linear() but I got different outputs for each of them!

model = Sequential()
model.add(Dense(5, activation=None,  input_shape=(3, )))
input = np.arange(1, 4, 1)
output = model.forward(input)
output = [-3.140941    0.21892631  0.20151067  3.1806703  -1.6549964 ]

module = Linear(3, 5)
input = np.arange(1, 4, 1)
output = module.forward(input)
output = [-0.4214403   0.8508979   1.4701201   1.8007498  -0.27794093]
hkvision commented 4 years ago

Hi, @hkvision

Thanks, I understood. What method can I use as an alternative of Dense() ?

I tested Linear() but I got different outputs for each of them!

model = Sequential()
model.add(Dense(5, activation=None,  input_shape=(3, )))
input = np.arange(1, 4, 1)
output = model.forward(input)
output = [-3.140941    0.21892631  0.20151067  3.1806703  -1.6549964 ]

module = Linear(3, 5)
input = np.arange(1, 4, 1)
output = module.forward(input)
output = [-0.4214403   0.8508979   1.4701201   1.8007498  -0.27794093]

You get different results because the models have different initialized weights. Probably you can set the same weights to get the same result, such as the following:

module.set_weights(model.get_weights())

You can have a try and tell me if you have further problems.

m-developer96 commented 4 years ago

@hkvision Oh yes ... I'm sorry for asking that question. I totally didn't notice to weights. Thank you so much for your help and attention :)

hkvision commented 4 years ago

You are very welcome! @m-developer96

hkvision commented 4 years ago

I'll first close this issue for now. @m-developer96 Feel free to tell us if you have other questions in the future.