serve icon indicating copy to clipboard operation
serve copied to clipboard

java.lang.ClassNotFoundException: org.pytorch.serve.ModelServer

Open laohur opened this issue 6 years ago • 18 comments

Environment: ubuntu18/20, conda, fresh, gpu, setup:

  • anaconda3
  • sudo apt-get install openjdk-11-jdk
  • conda create --name torchserve torchserve torch-model-archiver pytorch torchtext torchvision cudatoolkit=10.1 -c pytorch -c powerai
  • source activate torchserve
  • git clone https://github.com/pytorch/serve && cd serve && pip install -e .
  • cd serve/model-archiver && pip install -e .
  • pip install -U -e . && cd .. && pip install -U -e . [ " python setup.py build " "python setup.py install" ]
  • cd ~ && git clone https://github.com/pytorch/serve.git
  • mkdir ~/model_store && cd ~/model_store
  • wget https://download.pytorch.org/models/densenet161-8d451a50.pth
  • torch-model-archiver --model-name densenet161 --version 1.0 --model-file ~/serve/examples/image_classifier/densenet_161/model.py --serialized-file ~/model_store/densenet161-8d451a50.pth --extra-files ~/serve/examples/image_classifier/index_to_name.json --handler image_classifier
  • torchserve --start --model-store ~/model_store --models ~/model_store/densenet161.mar

then

ERROR:
File "setup.py", line 85, in run subprocess.check_call('frontend/gradlew -p frontend clean build', shell=True) File "/root/anaconda3/envs/torchserve/lib/python3.8/subprocess.py", line 364, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command 'frontend/gradlew -p frontend clean build' returned non-zero exit status 1.

laohur avatar Apr 25 '20 10:04 laohur

@laohur :

Could you please provide detailed steps of what you tried. From the currently provided details below are my observations and recommendations :

conda create --name torchserve torchserve torch-model-archiver pytorch torchtext torchvision cudatoolkit=10.1 -c pytorch 

This command should create a new conda environment named torchserve with all the above mentioned packages installed. Did you switch to the new environment before starting TorchServe. I am not able to make out whether your switched to the new environment or not from the details provided.

Command: " python setup.py build " "python setup.py install"

To build from source use the following command :

cd <path_to_serve_home>
pip install -e .

Make sure you have jdk11 installed for building from source.

harshbafna avatar Apr 25 '20 11:04 harshbafna

@laohur :

Could you please provide detailed steps of what you tried. From the currently provided details below are my observations and recommendations :

conda create --name torchserve torchserve torch-model-archiver pytorch torchtext torchvision cudatoolkit=10.1 -c pytorch 

This command should create a new conda environment named torchserve with all the above mentioned packages installed. Did you switch to the new environment before starting TorchServe. I am not able to make out whether your switched to the new environment or not from the details provided.

Command: " python setup.py build " "python setup.py install"

To build from source use the following command :

cd <path_to_serve_home>
pip install -e .

Make sure you have jdk11 installed for building from source.

cmds all above, install jdk11 from apt

laohur avatar Apr 25 '20 14:04 laohur

  • anaconda3
  • sudo apt-get install openjdk-11-jdk
  • conda create --name torchserve torchserve torch-model-archiver pytorch torchtext torchvision cudatoolkit=10.1 -c pytorch -c powerai
  • source activate torchserve
  • git clone https://github.com/pytorch/serve && cd serve && pip install -e .

@laohur I am a bit confused here, why are you again installing from source after switching to the torchserve conda environment? The conda create command above will create a new conda environment with torchserve and torch-model-archiver packages pre-installed. You don't need to run pip install commands.

Also the following error is only observed when trying to build from source and the build fails for some reason. However, you are reporting this when you start torchserve.

ERROR: File "setup.py", line 85, in run subprocess.check_call('frontend/gradlew -p frontend clean build', shell=True) File "/root/anaconda3/envs/torchserve/lib/python3.8/subprocess.py", line 364, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command 'frontend/gradlew -p frontend clean build' returned non-zero exit status 1.

harshbafna avatar Apr 25 '20 16:04 harshbafna

  • anaconda3
  • sudo apt-get install openjdk-11-jdk
  • conda create --name torchserve torchserve torch-model-archiver pytorch torchtext torchvision cudatoolkit=10.1 -c pytorch -c powerai
  • source activate torchserve
  • git clone https://github.com/pytorch/serve && cd serve && pip install -e .

@laohur I am a bit confused here, why are you again installing from source after switching to the torchserve conda environment? The conda create command above will create a new conda environment with torchserve and torch-model-archiver packages pre-installed. You don't need to run pip install commands.

Also the following error is only observed when trying to build from source and the build fails for some reason. However, you are reporting this when you start torchserve.

ERROR: File "setup.py", line 85, in run subprocess.check_call('frontend/gradlew -p frontend clean build', shell=True) File "/root/anaconda3/envs/torchserve/lib/python3.8/subprocess.py", line 364, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command 'frontend/gradlew -p frontend clean build' returned non-zero exit status 1.

emm, your are right. Then how to clean it ?:

python setup.py install --record files.txt cat files.txt | xargs rm -rf

why does install twice wrong? I assume that can be faster.

laohur avatar Apr 26 '20 01:04 laohur

@laohur : I would recommend to create two separate environments for installing from source and installing from conda/pip repo.

Also, as per #272, I assume you were able to successfully install and start torchserve on a fresh environment. Could you please confirm?

harshbafna avatar Apr 27 '20 06:04 harshbafna

I'm facing the same issue while installing. Following are the steps that I followed:

# Install with conda
conda create --name torchserve torchserve torch-model-archiver pytorch torchtext torchvision cudatoolkit=10.1 -c pytorch -c powerai
conda activate torchserve

git clone https://github.com/pytorch/serve.git
cd serve/
mkdir model_store
cd model_store

wget https://download.pytorch.org/models/densenet161-8d451a50.pth
# Model gets downloaded

cd ..
torch-model-archiver --model-name densenet161 --version 1.0 --model-file examples/image_classifier/densenet_161/model.py --serialized-file model_store/densenet161-8d451a50.pth --extra-files examples/image_classifier/index_to_name.json --handler image_classifier

torchserve --start --model-store model_store/ --models densenet161.mar

Error: Could not find or load main class org.pytorch.serve.ModelServer
Caused by: java.lang.ClassNotFoundException: org.pytorch.serve.ModelServer

Edit: Forgot to add torchserve command

ashnair1 avatar Apr 27 '20 18:04 ashnair1

use docker!

laohur avatar Apr 28 '20 02:04 laohur

torch-model-archiver --model-name densenet161 --version 1.0 --model-file examples/image_classifier/densenet_161/model.py --serialized-file model_store/densenet161-8d451a50.pth --extra-files examples/image_classifier/index_to_name.json --handler image_classifier Error: Could not find or load main class org.pytorch.serve.ModelServer Caused by: java.lang.ClassNotFoundException: org.pytorch.serve.ModelServer

@ashnair1 : The torch-model-archiver utility has no Java component. Are you sure you observed the above error while creating the mar file and not while starting torchserve? Looks like you are missing some step in your logs.

harshbafna avatar Apr 28 '20 04:04 harshbafna

@harshbafna Apologies. You're right. This is triggered when I run torchserve. I have edited my comment.

ashnair1 avatar Apr 28 '20 04:04 ashnair1

@ashnair1 : Could you please provide the following environment details :

  • OS
  • JDK version
  • Python version in the torchserve conda environment

Also, did you try to install torchserve from source using pip install command?

harshbafna avatar Apr 28 '20 04:04 harshbafna

OS: Ubuntu 16.04

JDK
openjdk version "11.0.5" 2019-10-15
OpenJDK Runtime Environment (build 11.0.5+10-post-Ubuntu-2ubuntu116.04)
OpenJDK 64-Bit Server VM (build 11.0.5+10-post-Ubuntu-2ubuntu116.04, mixed mode, sharing)

Python : 3.8.2

I did try installing from source and the same problem occurred.

ashnair1 avatar Apr 28 '20 04:04 ashnair1

@ashnair1 :

I could not reproduce the issue following the same steps as yours. Following are the environment details :

(torchserve) ubuntu@ip-172-31-38-229:~/serve$ cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.6 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.6 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial
(torchserve) ubuntu@ip-172-31-38-229:~/serve$ python --version
Python 3.8.2
(torchserve) ubuntu@ip-172-31-38-229:~/serve$ java -version
openjdk version "11.0.5" 2019-10-15
OpenJDK Runtime Environment (build 11.0.5+10-post-Ubuntu-2ubuntu116.04)
OpenJDK 64-Bit Server VM (build 11.0.5+10-post-Ubuntu-2ubuntu116.04, mixed mode, sharing)
(torchserve) ubuntu@ip-172-31-38-229:~/serve$ 

Could you try this on a fresh conda envrironment and confirm?

harshbafna avatar Apr 28 '20 05:04 harshbafna

Reinstalling in a fresh conda environment worked. I'm now able to launch torchserve. But model can't be loaded.

torchserve --start --model-store model_store --models model_store/densenet161.mar

TS Home: /home/ashwin/anaconda3/envs/torchserve/lib/python3.8/site-packages
Current directory: /home/ashwin/Desktop/Projects
Temp directory: /tmp
Number of GPUs: 1
Number of CPUs: 12
Max heap size: 8012 M
Python executable: /home/ashwin/anaconda3/envs/torchserve/bin/python
Config file: logs/config/20200428094816987-shutdown.cfg
Inference address: http://127.0.0.1:8080
Management address: http://127.0.0.1:8081
Model Store: /home/ashwin/Desktop/Projects/model_store
Initial Models: model_store/densenet161.mar
Log dir: /home/ashwin/Desktop/Projects/logs
Metrics dir: /home/ashwin/Desktop/Projects/logs
Netty threads: 0
Netty client threads: 0
Default workers per model: 1
Blacklist Regex: N/A
Maximum Response Size: 6553500
Maximum Request Size: 6553500
2020-04-28 09:49:24,708 [INFO ] main org.pytorch.serve.snapshot.SnapshotManager - Started restoring models from snapshot {
  "name": "20200428094816987-shutdown.cfg",
  "modelCount": 0,
  "created": 1588052896988,
  "models": {}
}
2020-04-28 09:49:24,724 [INFO ] main org.pytorch.serve.snapshot.SnapshotManager - Validating snapshot 20200428094816987-shutdown.cfg
2020-04-28 09:49:24,725 [INFO ] main org.pytorch.serve.snapshot.SnapshotManager - Validated snapshot 20200428094816987-shutdown.cfg
2020-04-28 09:49:24,725 [WARN ] main org.pytorch.serve.snapshot.SnapshotManager - Model snapshot is empty. Starting TorchServe without initial models.
2020-04-28 09:49:24,728 [INFO ] main org.pytorch.serve.ModelServer - Initialize Inference server with: EpollServerSocketChannel.
2020-04-28 09:49:24,811 [INFO ] main org.pytorch.serve.ModelServer - Inference API bind to: http://127.0.0.1:8080
2020-04-28 09:49:24,811 [INFO ] main org.pytorch.serve.ModelServer - Initialize Management server with: EpollServerSocketChannel.
2020-04-28 09:49:24,812 [INFO ] main org.pytorch.serve.ModelServer - Management API bind to: http://127.0.0.1:8081
Model server started.
2020-04-28 09:49:24,906 [INFO ] pool-2-thread-1 TS_METRICS - CPUUtilization.Percent:0.0|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:1588052964
2020-04-28 09:49:24,908 [INFO ] pool-2-thread-1 TS_METRICS - DiskAvailable.Gigabytes:30.20065689086914|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:1588052964
2020-04-28 09:49:24,908 [INFO ] pool-2-thread-1 TS_METRICS - DiskUsage.Gigabytes:533.5580177307129|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:1588052964
2020-04-28 09:49:24,909 [INFO ] pool-2-thread-1 TS_METRICS - DiskUtilization.Percent:94.6|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:1588052964
2020-04-28 09:49:24,909 [INFO ] pool-2-thread-1 TS_METRICS - MemoryAvailable.Megabytes:25761.484375|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:1588052964
2020-04-28 09:49:24,910 [INFO ] pool-2-thread-1 TS_METRICS - MemoryUsed.Megabytes:5423.06640625|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:1588052964
2020-04-28 09:49:24,910 [INFO ] pool-2-thread-1 TS_METRICS - MemoryUtilization.Percent:19.6|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:1588052964
2020-04-28 09:49:45,787 [INFO ] epollEventLoopGroup-3-1 ACCESS_LOG - /127.0.0.1:36950 "POST /predictions/densenet161 HTTP/1.1" 404 9
2020-04-28 09:49:45,788 [INFO ] epollEventLoopGroup-3-1 TS_METRICS - Requests4XX.Count:1|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:null
2020-04-28 09:49:50,575 [INFO ] epollEventLoopGroup-3-2 ACCESS_LOG - /127.0.0.1:36952 "POST /predictions/densenet161 HTTP/1.1" 404 0
2020-04-28 09:49:50,576 [INFO ] epollEventLoopGroup-3-2 TS_METRICS - Requests4XX.Count:1|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:null
2020-04-28 09:50:24,898 [INFO ] pool-2-thread-1 TS_METRICS - CPUUtilization.Percent:0.0|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:1588053024
2020-04-28 09:50:24,898 [INFO ] pool-2-thread-1 TS_METRICS - DiskAvailable.Gigabytes:30.20065689086914|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:1588053024
2020-04-28 09:50:24,898 [INFO ] pool-2-thread-1 TS_METRICS - DiskUsage.Gigabytes:533.5580177307129|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:1588053024
2020-04-28 09:50:24,899 [INFO ] pool-2-thread-1 TS_METRICS - DiskUtilization.Percent:94.6|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:1588053024
2020-04-28 09:50:24,899 [INFO ] pool-2-thread-1 TS_METRICS - MemoryAvailable.Megabytes:25715.10546875|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:1588053024
2020-04-28 09:50:24,899 [INFO ] pool-2-thread-1 TS_METRICS - MemoryUsed.Megabytes:5469.31640625|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:1588053024
2020-04-28 09:50:24,899 [INFO ] pool-2-thread-1 TS_METRICS - MemoryUtilization.Percent:19.8|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:1588053024
2020-04-28 09:50:46,705 [INFO ] epollEventLoopGroup-3-3 ACCESS_LOG - /127.0.0.1:36954 "POST /predictions/densenet161 HTTP/1.1" 404 1
2020-04-28 09:50:46,705 [INFO ] epollEventLoopGroup-3-3 TS_METRICS - Requests4XX.Count:1|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:null
2020-04-28 09:50:47,656 [INFO ] epollEventLoopGroup-3-4 ACCESS_LOG - /127.0.0.1:36956 "POST /predictions/densenet161 HTTP/1.1" 404 0
2020-04-28 09:50:47,656 [INFO ] epollEventLoopGroup-3-4 TS_METRICS - Requests4XX.Count:1|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:null
2020-04-28 09:50:48,680 [INFO ] epollEventLoopGroup-3-5 ACCESS_LOG - /127.0.0.1:36958 "POST /predictions/densenet161 HTTP/1.1" 404 0
2020-04-28 09:50:48,680 [INFO ] epollEventLoopGroup-3-5 TS_METRICS - Requests4XX.Count:1|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:null
2020-04-28 09:51:24,897 [INFO ] pool-2-thread-1 TS_METRICS - CPUUtilization.Percent:0.0|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:1588053084
2020-04-28 09:51:24,897 [INFO ] pool-2-thread-1 TS_METRICS - DiskAvailable.Gigabytes:30.20065689086914|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:1588053084
2020-04-28 09:51:24,898 [INFO ] pool-2-thread-1 TS_METRICS - DiskUsage.Gigabytes:533.5580177307129|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:1588053084
2020-04-28 09:51:24,898 [INFO ] pool-2-thread-1 TS_METRICS - DiskUtilization.Percent:94.6|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:1588053084
2020-04-28 09:51:24,898 [INFO ] pool-2-thread-1 TS_METRICS - MemoryAvailable.Megabytes:25666.24609375|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:1588053084
2020-04-28 09:51:24,898 [INFO ] pool-2-thread-1 TS_METRICS - MemoryUsed.Megabytes:5518.17578125|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:1588053084
2020-04-28 09:51:24,898 [INFO ] pool-2-thread-1 TS_METRICS - MemoryUtilization.Percent:19.9|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:1588053084
2020-04-28 09:51:30,687 [INFO ] epollEventLoopGroup-3-6 ACCESS_LOG - /127.0.0.1:36960 "POST /predictions/densenet161 HTTP/1.1" 404 0
2020-04-28 09:51:30,688 [INFO ] epollEventLoopGroup-3-6 TS_METRICS - Requests4XX.Count:1|#Level:Host|#hostname:ashwin-OMEN-by-HP-Laptop-15-dc1xxx,timestamp:null


Edit: So I moved model_store inside serve and ran

torchserve --start --model-store serve/model_store --models serve/model_store/densenet161.mar

which gives the expected response:

curl -X POST http://127.0.0.1:8080/predictions/densenet161 -T kitten.jpg

[
  {
    "tiger_cat": 0.46933630108833313
  },
  {
    "tabby": 0.46338731050491333
  },
  {
    "Egyptian_cat": 0.06456129252910614
  },
  {
    "lynx": 0.0012828161707147956
  },
  {
    "plastic_bag": 0.00023322951165027916
  }

ashnair1 avatar Apr 28 '20 05:04 ashnair1

In my opinion, it is not clear what the root cause is so re-opening.

mycpuorg avatar May 08 '20 18:05 mycpuorg

@mycpuorg

I recreated this issue by:

  1. installing from source pip install -e . in env "a"
  2. creating env "b" and doing pip install torchserve
  3. run torchserve ... and get this error...

I fixed it by:

  1. pip uninstall torchserve
  2. pip install torchserve or even pip install . or pip install -U .

I recreated the error again by installing with the -e option. Talked to @harshbafna about it and it is because the -e option will uninstall your current working torchserve (and get rid of the jar?), then doesn't run the gradle process which creates the required jar file for the server.

I think this problem needs to be captured in a developer guide. For simple use cases where people aren't changing the source, this won't come up. It's also why it doesn't show up in regular testing as a regular user, or if you're using a clean instance or docker setup every time. #313 will get around introducing this issue in the first place, but since people might be used to using -e, another PR can address how developers should set things up and iterate their dev work without breaking all of their environments.

aaronmarkham avatar May 08 '20 20:05 aaronmarkham

@mycpuorg

I recreated this issue by:

  1. installing from source pip install -e . in env "a"
  2. creating env "b" and doing pip install torchserve
  3. run torchserve ... and get this error...

I fixed it by:

  1. pip uninstall torchserve
  2. pip install torchserve or even pip install . or pip install -U .

I recreated the error again by installing with the -e option. Talked to @harshbafna about it and it is because the -e option will uninstall your current working torchserve (and get rid of the jar?), then doesn't run the gradle process which creates the required jar file for the server.

I think this problem needs to be captured in a developer guide. For simple use cases where people aren't changing the source, this won't come up. It's also why it doesn't show up in regular testing as a regular user, or if you're using a clean instance or docker setup every time. #313 will get around introducing this issue in the first place, but since people might be used to using -e, another PR can address how developers should set things up and iterate their dev work without breaking all of their environments.

Root cause for this behavior : https://github.com/conda/conda/issues/5861

harshbafna avatar May 10 '20 16:05 harshbafna

Thanks @harshbafna can you please look into PR #320 ? and comment what else is needed there?

mycpuorg avatar May 10 '20 17:05 mycpuorg

Related PRs #320 #322 #420 #349 have been merged hence closing this.

codinnvrends avatar Jun 10 '20 03:06 codinnvrends