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

Add Bert Language Modeling example #21818

Merged
merged 9 commits into from
Jun 15, 2022
Merged

Conversation

yeandy
Copy link
Contributor

@yeandy yeandy commented Jun 13, 2022

Add Bert Language Modeling example


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Choose reviewer(s) and mention them in a comment (R: @username).
  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests

See CI.md for more information about GitHub Actions CI.

@yeandy
Copy link
Contributor Author

yeandy commented Jun 13, 2022

R: @AnandInguva @tvalentyn

@yeandy
Copy link
Contributor Author

yeandy commented Jun 13, 2022

Run Python 3.8 PostCommit

@yeandy
Copy link
Contributor Author

yeandy commented Jun 13, 2022

Postcommits and unit tests pass locally. PTAL @tvalentyn

@asf-ci
Copy link

asf-ci commented Jun 13, 2022

Can one of the admins verify this patch?

@codecov
Copy link

codecov bot commented Jun 13, 2022

Codecov Report

Merging #21818 (ac12207) into master (d2fb942) will decrease coverage by 0.07%.
The diff coverage is 0.00%.

@@            Coverage Diff             @@
##           master   #21818      +/-   ##
==========================================
- Coverage   74.15%   74.07%   -0.08%     
==========================================
  Files         698      699       +1     
  Lines       92417    92504      +87     
==========================================
- Hits        68530    68526       -4     
- Misses      22636    22727      +91     
  Partials     1251     1251              
Flag Coverage Δ
python 83.65% <0.00%> (-0.12%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...am/examples/inference/pytorch_language_modeling.py 0.00% <0.00%> (ø)
.../python/apache_beam/testing/test_stream_service.py 88.09% <0.00%> (-4.77%) ⬇️
sdks/python/apache_beam/utils/interactive_utils.py 95.12% <0.00%> (-2.44%) ⬇️
...n/apache_beam/ml/gcp/recommendations_ai_test_it.py 73.46% <0.00%> (-2.05%) ⬇️
.../python/apache_beam/transforms/periodicsequence.py 96.77% <0.00%> (-1.59%) ⬇️
sdks/python/apache_beam/io/source_test_utils.py 88.01% <0.00%> (-1.39%) ⬇️
...che_beam/runners/interactive/interactive_runner.py 90.06% <0.00%> (-1.33%) ⬇️
...eam/runners/portability/fn_api_runner/execution.py 92.44% <0.00%> (-0.65%) ⬇️
sdks/python/apache_beam/transforms/combiners.py 93.05% <0.00%> (-0.39%) ⬇️
sdks/python/apache_beam/pipeline.py 91.80% <0.00%> (ø)
... and 45 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d2fb942...ac12207. Read the comment docs.

@yeandy
Copy link
Contributor Author

yeandy commented Jun 14, 2022

Changed the dataset to a custom file with my own sentences. If we get the Ok for the model, then I think this example should be goo.

'--output',
dest='output',
help='Path where to save output predictions.'
' text file.')
Copy link
Contributor

Choose a reason for hiding this comment

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

broken help string

'masked_text': text_and_masked_text_tuple,
'predicted_text': text_and_predictions
})
| 'Merge' >> beam.CoGroupByKey()
Copy link
Contributor

Choose a reason for hiding this comment

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

is this something that can be folded into PostProcess? isn't the masked_text already a part of predicted result?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we can fold it in. The original text, not the masked text, is the key of predicted_text. i.e. the format is
masked_text is (original_text, masked_text)
predicted_text is (original_text, predicted_word)
and then we join on the original_text key

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, the CoGBK can't be folded.

text_and_tokenized_text_tuple
| 'PyTorchRunInference' >> RunInference(
KeyedModelHandler(model_handler)).with_output_types(
Tuple[str, PredictionResult])
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need these hints anymore.

@yeandy
Copy link
Contributor Author

yeandy commented Jun 14, 2022

Added custom text of sentences. Ran local tests

python -m apache_beam.examples.inference.pytorch_language_modeling \
  --input apache_beam/examples/inference/data/sentences.txt \
  --output predictions.csv \
  --model_state_dict_path BertForMaskedLM.pth

python -m apache_beam.examples.inference.pytorch_language_modeling \
  --output predictions.csv \
  --model_state_dict_path BertForMaskedLM.pth

@yeandy
Copy link
Contributor Author

yeandy commented Jun 15, 2022

Removed CoGBK for simplicity.

test_pipeline = TestPipeline(is_integration_test=True)
# Path to text file containing some sentences
file_of_sentences = 'gs://apache-beam-ml/datasets/custom/sentences.txt' # disable: line-too-long
output_file_dir = 'gs://apache-beam-ml/testing/predictions'
Copy link
Contributor

Choose a reason for hiding this comment

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

For test output, it's better to use a bucket with a lifecycle configured to leave less clutter behind, for example:

:~$ gsutil lifecycle get gs://temp-storage-for-end-to-end-tests/
{"rule": [{"action": {"type": "Delete"}, "condition": {"age": 14}}]}

Lifecycle may be per bucket (not sure if we can configure it just for ./testing/predictions), so switching outputs for all tests to gs://temp-storage-for-end-to-end-tests/ may be easiest.

cc: @AnandInguva FYI.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SG, thanks.

@tvalentyn tvalentyn merged commit ff8f51b into apache:master Jun 15, 2022
bullet03 pushed a commit to akvelon/beam that referenced this pull request Jun 20, 2022
prodriguezdefino pushed a commit to prodriguezdefino/beam-pabs that referenced this pull request Jun 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants