Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AOTInductor BERT CPP example #2931

Merged
merged 48 commits into from
Feb 27, 2024
Merged

AOTInductor BERT CPP example #2931

merged 48 commits into from
Feb 27, 2024

Conversation

lxning
Copy link
Collaborator

@lxning lxning commented Feb 7, 2024

Description

Please read our CONTRIBUTING.md prior to creating your first pull request.

Please include a summary of the feature or issue being fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

Fixes #(issue)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

Feature/Issue validation/testing

Please describe the Unit or Integration tests that you ran to verify your changes and relevant result summary. Provide instructions so it can be reproduced.
Please also list any relevant details for your test configuration.

torchserve_cpp build is complete. To run unit test:   ./_build/test/torchserve_cpp_test
Running main() from /home/ubuntu/serve/cpp/_build/_deps/googletest-src/googletest/src/gtest_main.cc
[==========] Running 48 tests from 11 test suites.
[----------] Global test environment set-up.
[----------] 1 test from BackendIntegTest
[ RUN      ] BackendIntegTest.TestOTFProtocolAndHandler
I0218 02:29:56.854155 177874 log_metric.cc:92] [METRICS]HandlerTime.Milliseconds:59.775908|#ModelName:mnist_scripted_v2,Level:Model|#hostname:ip-172-31-35-108,1708223396,reqi
I0218 02:29:56.854215 177874 log_metric.cc:92] [METRICS]PredictionTime.Milliseconds:59.775908|#ModelName:mnist_scripted_v2,Level:Model|#hostname:ip-172-31-35-108,1708223396,reqi
[       OK ] BackendIntegTest.TestOTFProtocolAndHandler (84 ms)
[----------] 1 test from BackendIntegTest (84 ms total)

[----------] 8 tests from OTFMessageTest
[ RUN      ] OTFMessageTest.TestRetieveCmd
[       OK ] OTFMessageTest.TestRetieveCmd (0 ms)
[ RUN      ] OTFMessageTest.TestEncodeLoadModelResponse
[       OK ] OTFMessageTest.TestEncodeLoadModelResponse (0 ms)
[ RUN      ] OTFMessageTest.TestUTF8EncodeLoadModelResponse
[       OK ] OTFMessageTest.TestUTF8EncodeLoadModelResponse (0 ms)
[ RUN      ] OTFMessageTest.TestRetrieveMsgLoadGpu
[       OK ] OTFMessageTest.TestRetrieveMsgLoadGpu (0 ms)
[ RUN      ] OTFMessageTest.TestRetrieveMsgLoadNoGpu
[       OK ] OTFMessageTest.TestRetrieveMsgLoadNoGpu (0 ms)
[ RUN      ] OTFMessageTest.TestEncodeSuccessInferenceResponse
[       OK ] OTFMessageTest.TestEncodeSuccessInferenceResponse (0 ms)
[ RUN      ] OTFMessageTest.TestEncodeFailureInferenceResponse
E0218 02:29:56.856161 177874 otf_message_test.cc:157] result_size: 120
[       OK ] OTFMessageTest.TestEncodeFailureInferenceResponse (0 ms)
[ RUN      ] OTFMessageTest.TestRetrieveInferenceMsg
[       OK ] OTFMessageTest.TestRetrieveInferenceMsg (0 ms)
[----------] 8 tests from OTFMessageTest (0 ms total)

[----------] 10 tests from ModelPredictTest
[ RUN      ] ModelPredictTest.TestLoadPredictBabyLlamaHandler
Total number of tokens generated: 332
Achieved tok per sec: 194.266
Generated String:  Hello my name is
The little girl, who was three years old, was playing in the garden. She saw a big, red tomato and wanted to pick it. She reached out her hand and grabbed it.
Suddenly, a voice said, "Hey! That's my tomato!"
The little girl looked up and saw a big, angry bird. She was scared and started to cry.
The bird said, "That tomato is mine! I'm going to eat it!"
The little girl was very scared and ran away. She was so sad that she didn't get to eat the tomato.
The bird flew away and the little girl was left alone in the garden. She was very sad and cried all the way home.
<s>

Generated String:  Hello my name is
The little girl, who was three years old, was playing in the garden. She saw a big, red tomato and wanted to pick it. She reached out her hand and grabbed it.
Suddenly, a voice said, "Hey! That's my tomato!"
The little girl looked up and saw a big, angry bird. She was scared and started to cry.
The bird said, "That tomato is mine! I'm going to eat it!"
The little girl was very scared and ran away. She was so sad that she didn't get to eat the tomato.
The bird flew away and the little girl was left alone in the garden. She was very sad and cried all the way home.
<s>

I0218 02:29:58.587792 177874 log_metric.cc:92] [METRICS]HandlerTime.Milliseconds:1726.664271|#ModelName:babyllama,Level:Model|#hostname:ip-172-31-35-108,1708223398,llm_ts_0,llm_ts_1
I0218 02:29:58.587825 177874 log_metric.cc:92] [METRICS]PredictionTime.Milliseconds:1726.664271|#ModelName:babyllama,Level:Model|#hostname:ip-172-31-35-108,1708223398,llm_ts_0,llm_ts_1
[       OK ] ModelPredictTest.TestLoadPredictBabyLlamaHandler (1734 ms)
[ RUN      ] ModelPredictTest.TestLoadPredictAotInductorLlamaHandler
I0218 02:29:59.265749 177874 log_metric.cc:92] [METRICS]HandlerTime.Milliseconds:669.961616|#ModelName:llama,Level:Model|#hostname:ip-172-31-35-108,1708223399,llm_ts_0,llm_ts_1
I0218 02:29:59.265791 177874 log_metric.cc:92] [METRICS]PredictionTime.Milliseconds:669.961616|#ModelName:llama,Level:Model|#hostname:ip-172-31-35-108,1708223399,llm_ts_0,llm_ts_1
[       OK ] ModelPredictTest.TestLoadPredictAotInductorLlamaHandler (678 ms)
[ RUN      ] ModelPredictTest.TestLoadPredictLlamaCppHandler
/home/ubuntu/serve/cpp/test/examples/examples_test.cc:53: Skipped
Skipping TestLoadPredictLlamaCppHandler because of missing file: _build/test/resources/examples/llamacpp/llamacpp_handler/llama-2-7b-chat.Q5_0.gguf
[  SKIPPED ] ModelPredictTest.TestLoadPredictLlamaCppHandler (0 ms)
[ RUN      ] ModelPredictTest.TestLoadPredictAotInductorBertHandler
I0218 02:29:59.272739 177874 bert_handler.cc:44] start LoadModel
I0218 02:29:59.387844 177874 log_metric.cc:92] [METRICS]HandlerTime.Milliseconds:104.557326|#ModelName:bertcppaot,Level:Model|#hostname:ip-172-31-35-108,1708223399,bert_ts_0,bert_ts_1
I0218 02:29:59.387884 177874 log_metric.cc:92] [METRICS]PredictionTime.Milliseconds:104.557326|#ModelName:bertcppaot,Level:Model|#hostname:ip-172-31-35-108,1708223399,bert_ts_0,bert_ts_1
[       OK ] ModelPredictTest.TestLoadPredictAotInductorBertHandler (139 ms)
[ RUN      ] ModelPredictTest.TestLoadPredictBaseHandler
I0218 02:29:59.427866 177874 log_metric.cc:92] [METRICS]HandlerTime.Milliseconds:4.646883|#ModelName:mnist_scripted_v2,Level:Model|#hostname:ip-172-31-35-108,1708223399,mnist_ts_0,mnist_ts_1
I0218 02:29:59.427894 177874 log_metric.cc:92] [METRICS]PredictionTime.Milliseconds:4.646883|#ModelName:mnist_scripted_v2,Level:Model|#hostname:ip-172-31-35-108,1708223399,mnist_ts_0,mnist_ts_1
[       OK ] ModelPredictTest.TestLoadPredictBaseHandler (18 ms)
[ RUN      ] ModelPredictTest.TestLoadPredictMnistHandler
I0218 02:29:59.441895 177874 log_metric.cc:92] [METRICS]HandlerTime.Milliseconds:4.013683|#ModelName:mnist_scripted_v2,Level:Model|#hostname:ip-172-31-35-108,1708223399,mnist_ts_0,mnist_ts_1
I0218 02:29:59.441939 177874 log_metric.cc:92] [METRICS]PredictionTime.Milliseconds:4.013683|#ModelName:mnist_scripted_v2,Level:Model|#hostname:ip-172-31-35-108,1708223399,mnist_ts_0,mnist_ts_1
[       OK ] ModelPredictTest.TestLoadPredictMnistHandler (13 ms)
[ RUN      ] ModelPredictTest.TestBackendInitWrongModelDir
E0218 02:29:59.442509 177874 model_archive.cc:53] Failed to init Manifest from: _build/test/resources/examples/mnist/MAR-INF/MANIFEST.json
[       OK ] ModelPredictTest.TestBackendInitWrongModelDir (0 ms)
[ RUN      ] ModelPredictTest.TestBackendInitWrongHandler
[       OK ] ModelPredictTest.TestBackendInitWrongHandler (0 ms)
[ RUN      ] ModelPredictTest.TestLoadModelFailure
E0218 02:29:59.448629 177874 torch_scripted_handler.cc:22] loading the model: mnist_scripted_v2, device id: -1, error: open file failed because of errno 2 on fopen: No such file or directory, file path: _build/test/resources/examples/mnist/wrong_model/mnist_script.pt
[       OK ] ModelPredictTest.TestLoadModelFailure (5 ms)
[ RUN      ] ModelPredictTest.TestLoadPredictMnistHandlerFailure
E0218 02:29:59.461757 177874 base_handler.cc:154] Failed to load tensor for request id: mnist_ts_0, c10 error: PytorchStreamReader failed reading zip archive: failed finding central directory
E0218 02:29:59.466233 177874 base_handler.cc:154] Failed to load tensor for request id: mnist_ts_1, c10 error: PytorchStreamReader failed reading zip archive: failed finding central directory
E0218 02:29:59.468318 177874 base_handler.cc:51] Failed to handle this batch after: Preprocessing
[       OK ] ModelPredictTest.TestLoadPredictMnistHandlerFailure (19 ms)
[----------] 10 tests from ModelPredictTest (2612 ms total)

[----------] 1 test from DLLoaderTest
[ RUN      ] DLLoaderTest.TestGetInstance
[       OK ] DLLoaderTest.TestGetInstance (0 ms)
[----------] 1 test from DLLoaderTest (0 ms total)

[----------] 3 tests from LoggingTest
[ RUN      ] LoggingTest.TestIncorrectLogInitialization
[       OK ] LoggingTest.TestIncorrectLogInitialization (0 ms)
[ RUN      ] LoggingTest.TestJSONConfigLogInitialization
[       OK ] LoggingTest.TestJSONConfigLogInitialization (0 ms)
[ RUN      ] LoggingTest.TestFileLogInitialization
[       OK ] LoggingTest.TestFileLogInitialization (0 ms)
[----------] 3 tests from LoggingTest (0 ms total)

[----------] 6 tests from TSLogMetricTest
[ RUN      ] TSLogMetricTest.TestCounterMetric
[       OK ] TSLogMetricTest.TestCounterMetric (1 ms)
[ RUN      ] TSLogMetricTest.TestGaugeMetric
[       OK ] TSLogMetricTest.TestGaugeMetric (1 ms)
[ RUN      ] TSLogMetricTest.TestHistogramMetric
[       OK ] TSLogMetricTest.TestHistogramMetric (1 ms)
[ RUN      ] TSLogMetricTest.TestTSLogMetricEmitWithRequestId
[       OK ] TSLogMetricTest.TestTSLogMetricEmitWithRequestId (1 ms)
[ RUN      ] TSLogMetricTest.TestTSLogMetricEmitWithoutRequestId
[       OK ] TSLogMetricTest.TestTSLogMetricEmitWithoutRequestId (1 ms)
[ RUN      ] TSLogMetricTest.TestTSLogMetricEmitWithIncorrectDimensionData
[       OK ] TSLogMetricTest.TestTSLogMetricEmitWithIncorrectDimensionData (0 ms)
[----------] 6 tests from TSLogMetricTest (7 ms total)

[----------] 2 tests from TSLogMetricsCacheTest
[ RUN      ] TSLogMetricsCacheTest.TestInitialize
[       OK ] TSLogMetricsCacheTest.TestInitialize (3 ms)
[ RUN      ] TSLogMetricsCacheTest.TestGetMetric
I0218 02:29:59.481583 177874 log_metric.cc:89] [METRICS]GaugeTsMetricExample.Count:1.5|#model_name:model_name,host_name:host_name|#hostname:ip-172-31-35-108,1708223399
[       OK ] TSLogMetricsCacheTest.TestGetMetric (1 ms)
[----------] 2 tests from TSLogMetricsCacheTest (4 ms total)

[----------] 3 tests from RegistryTest
[ RUN      ] RegistryTest.TestValidConfigFile
[       OK ] RegistryTest.TestValidConfigFile (1 ms)
[ RUN      ] RegistryTest.TestInvalidConfigFile
[       OK ] RegistryTest.TestInvalidConfigFile (0 ms)
[ RUN      ] RegistryTest.TestReInitialize
[       OK ] RegistryTest.TestReInitialize (2 ms)
[----------] 3 tests from RegistryTest (4 ms total)

[----------] 3 tests from UnitsTest
[ RUN      ] UnitsTest.TestGetExistingUnitMapping
[       OK ] UnitsTest.TestGetExistingUnitMapping (0 ms)
[ RUN      ] UnitsTest.TestGetNonExistentUnitMapping
[       OK ] UnitsTest.TestGetNonExistentUnitMapping (0 ms)
[ RUN      ] UnitsTest.TestGetEmptyUnitMapping
[       OK ] UnitsTest.TestGetEmptyUnitMapping (0 ms)
[----------] 3 tests from UnitsTest (0 ms total)

[----------] 10 tests from YAMLConfigTest
[ RUN      ] YAMLConfigTest.TestLoadValidConfigFrontendContext
[       OK ] YAMLConfigTest.TestLoadValidConfigFrontendContext (1 ms)
[ RUN      ] YAMLConfigTest.TestLoadValidConfigBackendContext
[       OK ] YAMLConfigTest.TestLoadValidConfigBackendContext (1 ms)
[ RUN      ] YAMLConfigTest.TestLoadMinimalValidConfig
[       OK ] YAMLConfigTest.TestLoadMinimalValidConfig (0 ms)
[ RUN      ] YAMLConfigTest.TestLoadInvalidConfigWithDuplicateDimension
[       OK ] YAMLConfigTest.TestLoadInvalidConfigWithDuplicateDimension (0 ms)
[ RUN      ] YAMLConfigTest.TestLoadInvalidConfigWithEmptyDimension
[       OK ] YAMLConfigTest.TestLoadInvalidConfigWithEmptyDimension (0 ms)
[ RUN      ] YAMLConfigTest.TestLoadInvalidConfigWithUndefinedDimension
[       OK ] YAMLConfigTest.TestLoadInvalidConfigWithUndefinedDimension (0 ms)
[ RUN      ] YAMLConfigTest.TestLoadInvalidConfigWithDuplicateMetricDimension
[       OK ] YAMLConfigTest.TestLoadInvalidConfigWithDuplicateMetricDimension (0 ms)
[ RUN      ] YAMLConfigTest.TestLoadInvalidConfigWithMissingMetricName
E0218 02:29:59.490960 177874 yaml_config.cc:203] Configuration for a metric must consist of "name", "unit" and "dimensions"
[       OK ] YAMLConfigTest.TestLoadInvalidConfigWithMissingMetricName (0 ms)
[ RUN      ] YAMLConfigTest.TestLoadInvalidConfigWithEmptyMetricName
E0218 02:29:59.491391 177874 yaml_config.cc:215] Configuration for a metric must consist of a non-empty "name"
[       OK ] YAMLConfigTest.TestLoadInvalidConfigWithEmptyMetricName (0 ms)
[ RUN      ] YAMLConfigTest.TestLoadInvalidConfigWithDuplicateMetricName
[       OK ] YAMLConfigTest.TestLoadInvalidConfigWithDuplicateMetricName (0 ms)
[----------] 10 tests from YAMLConfigTest (6 ms total)

[----------] 1 test from ManifestTest
[ RUN      ] ManifestTest.TestInitialize
[       OK ] ManifestTest.TestInitialize (0 ms)
[----------] 1 test from ManifestTest (0 ms total)

[----------] Global test environment tear-down
[==========] 48 tests from 11 test suites ran. (2721 ms total)
[  PASSED  ] 47 tests.
[  SKIPPED ] 1 test, listed below:
[  SKIPPED ] ModelPredictTest.TestLoadPredictLlamaCppHandler

Checklist:

  • Did you have fun?
  • Have you added tests that prove your fix is effective or that this feature works?
  • Has code been commented, particularly in hard-to-understand areas?
  • Have you made corresponding changes to the documentation?

@lxning lxning marked this pull request as draft February 7, 2024 18:40
Copy link
Collaborator

@mreso mreso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments

cpp/README.md Show resolved Hide resolved
cpp/build.sh Outdated
echo -e "${COLOR_GREEN}[ INFO ] Cloning tokenizers-cpp repo ${COLOR_OFF}"
git clone https://github.com/mlc-ai/tokenizers-cpp.git "$TOKENIZERS_CPP_SRC_DIR"
cd $TOKENIZERS_CPP_SRC_DIR
git submodule update --init --recursive
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to create a submodule in third-party directly instead of cloning it manually. That way we freeze a specific commit and nothing breaks if the tokenizer-cpp repo gets updated. See llama2.so for example. git submodule update --init --recursive is executed in build.sh for all our submodules.

PYTHONPATH=${LLAMA_SO_DIR}:${PYTHONPATH} python ${BASE_DIR}/../examples/cpp/aot_inductor/llama2/compile.py --checkpoint ${HANDLER_DIR}/stories15M.pt ${HANDLER_DIR}/stories15M.so
fi
if [ ! -f "${EX_DIR}/aot_inductor/bert_handler/bert-seq.so" ]; then
pip install transformers
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will better fit into the cpp section of ts_scripts/install_dependencies.py

)

set_seed(1)
# PT2.2 has limitation on the max
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you link the issue or PR here that describes the problem in 2.2?

NEW_DIR,
)

model.save_pretrained(NEW_DIR)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we saving the model here? Its already in the hub cache so no need to save it again.

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

model = model.to(device=device)
dummy_input = "This is a dummy input for torch jit trace"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To not be misleading we should change this into "... for torch export"

#include <folly/FileUtil.h>
#include <folly/dynamic.h>
#include <folly/json.h>
#include <fmt/format.h>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #2944 (comment) regarding header placement

return data;
}

std::unique_ptr<folly::dynamic> BertCppHandler::LoadJsonFile(const std::string& file_path) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should move these into ts_utils

Copy link
Collaborator

@mreso mreso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, lets just check if we need kineto for mac and if not lets remove it.

@@ -394,10 +370,9 @@ cd $BASE_DIR
git submodule update --init --recursive

install_folly
install_kineto
#install_kineto
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check if we actually need this for mac install? And lets remove it if its not necessary

@chauhang chauhang added the c++ label Feb 27, 2024
@chauhang chauhang added this to the v0.10.0 milestone Feb 27, 2024
@lxning lxning self-assigned this Feb 27, 2024
@lxning lxning added documentation Improvements or additions to documentation enhancement New feature or request p0 high priority example labels Feb 27, 2024
@lxning lxning added this pull request to the merge queue Feb 27, 2024
@lxning lxning added this to Done in v0.10.0 lifecycle Feb 27, 2024
Merged via the queue into master with commit 85e4e0f Feb 27, 2024
15 checks passed
muthuraj-i2i pushed a commit to muthuraj-i2i/serve that referenced this pull request Mar 1, 2024
* fix compile error on mac x86

* update install libtorch

* fmt

* fmt

* fmt

* Set return type of bert model and dynamic shapes

* fix json value

* fix build on linux

* add linux dependency

* replace sentenepice with tokenizers-cpp

* update dependency

* add attention mask

* fix compile error

* fix compile error

* fmt

* Fmt

* tockenizer-cpp git submodule

* update handler

* fmt

* fmt

* fmt

* unset env

* fix path

* Fix type error in bert aot example

* fmt

* fmt

* update max setting

* fix lint

* add limitation

* pinned folly to v2024.02.19.00

* pinned yam-cpp with tags/0.8.0

* pinned yaml-cpp 0.8.0

* update build.sh

* pinned yaml-cpp v0.8.0

* fmt

* fix typo

* add submodule kineto

* fmt

* fix workflow

* fix workflow

* fix ubuntu version

* update readme

---------

Co-authored-by: Matthias Reso <13337103+mreso@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ documentation Improvements or additions to documentation enhancement New feature or request example p0 high priority
Projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants