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

Fix gpt trainer test #6915

Merged
merged 156 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 142 commits
Commits
Show all changes
156 commits
Select commit Hold shift + click to select a range
5f0d53b
Add trainer.test() for GPT
hsiehjackson Jun 24, 2023
e6865c8
Remove unused part
hsiehjackson Jun 24, 2023
de3cc19
Add trainer.test() for GPT
hsiehjackson Jun 24, 2023
87ce549
Remove unused part
hsiehjackson Jun 24, 2023
74c6fd8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 24, 2023
98da36e
Fix training part
hsiehjackson Jun 27, 2023
9656fa9
Fix conflict
hsiehjackson Jun 27, 2023
7c240b3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 27, 2023
c22cfd6
Fix config
hsiehjackson Jun 27, 2023
83ff763
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jun 27, 2023
afa9030
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jun 27, 2023
ae0f1f0
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jun 27, 2023
a4588f4
Fix references and add CI
hsiehjackson Jun 28, 2023
689f96c
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jun 28, 2023
6d7c857
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 28, 2023
3bbc4e7
Fix config error
hsiehjackson Jun 28, 2023
7539545
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jun 28, 2023
bad78a6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 28, 2023
f2405bc
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jun 28, 2023
d74f8d9
Fix dataset
hsiehjackson Jun 28, 2023
cadfaca
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jun 28, 2023
6c4950c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 28, 2023
2f6d963
Add metadata
hsiehjackson Jun 28, 2023
9b00e69
Fix conflict
hsiehjackson Jun 28, 2023
ea6968b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 28, 2023
e96f4cd
Fix config
hsiehjackson Jun 29, 2023
a347456
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jun 29, 2023
c88f17f
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jun 29, 2023
b8616f4
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jun 29, 2023
3f7074d
Fix empty batch
hsiehjackson Jun 29, 2023
b6e0572
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jun 29, 2023
9292781
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 29, 2023
63f05cc
Fix config
hsiehjackson Jun 29, 2023
4e91a40
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jun 29, 2023
20173e6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 29, 2023
db27d89
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jun 29, 2023
855dbec
Fix config
hsiehjackson Jun 30, 2023
6f36a52
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jun 30, 2023
546a5d6
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jun 30, 2023
2de6e3f
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jun 30, 2023
7daf79e
Fix max seq length
hsiehjackson Jun 30, 2023
0c3da74
Fix dataset
hsiehjackson Jun 30, 2023
fca0024
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 30, 2023
6735077
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jul 1, 2023
e33d255
Fix dataset
hsiehjackson Jul 2, 2023
da33fc8
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jul 2, 2023
f383adb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 2, 2023
711b122
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jul 5, 2023
b3d01bd
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jul 5, 2023
aa756c5
Add token f1
hsiehjackson Jul 6, 2023
caebbd5
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jul 6, 2023
2a54be3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 6, 2023
7099fc4
Add FA in sft
hsiehjackson Jul 6, 2023
f9396f5
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jul 6, 2023
1b43a95
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 6, 2023
2d6109b
Add inference config
hsiehjackson Jul 6, 2023
5057605
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jul 6, 2023
463e9a6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 6, 2023
c4bad4b
Fix bug
hsiehjackson Jul 7, 2023
5e5cf2a
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jul 7, 2023
61bf69a
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jul 7, 2023
734b5df
Fix pad
hsiehjackson Jul 7, 2023
5bba479
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jul 7, 2023
179039a
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jul 8, 2023
c1bf6e3
Fix num batch
hsiehjackson Jul 8, 2023
516f942
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jul 8, 2023
e456359
Add query_key
hsiehjackson Jul 10, 2023
3b7a758
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 10, 2023
4a62efb
Remove pdb
hsiehjackson Jul 10, 2023
fc37a11
Fix conflict
hsiehjackson Jul 10, 2023
e7aba8e
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jul 10, 2023
2372ec5
Fix write json
hsiehjackson Jul 11, 2023
f305e1f
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jul 11, 2023
54ed4da
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 11, 2023
aede844
Fix dataset bug and refactor
hsiehjackson Jul 11, 2023
e844073
Fix conflict
hsiehjackson Jul 11, 2023
9d6aaef
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 11, 2023
3bca274
Add logging for prediction
hsiehjackson Jul 11, 2023
b01949e
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jul 11, 2023
b7a82c6
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jul 11, 2023
01b0465
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jul 11, 2023
1fb3048
Fix retrain
hsiehjackson Jul 11, 2023
c7c936d
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jul 11, 2023
2323131
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jul 12, 2023
c9329c7
Add query_key in config
hsiehjackson Jul 12, 2023
8fd391e
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jul 12, 2023
4dd4840
Fix bug
hsiehjackson Jul 13, 2023
5413add
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 13, 2023
6557e07
Fix config
hsiehjackson Jul 13, 2023
3dda9ef
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jul 13, 2023
be7bb04
Fix bug
hsiehjackson Jul 14, 2023
cbd68dd
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jul 14, 2023
9cd83ce
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 14, 2023
6715fd0
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jul 17, 2023
ad22848
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jul 18, 2023
b83a7bb
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jul 18, 2023
9adcd83
Add inference config
hsiehjackson Jul 19, 2023
986fc2e
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jul 19, 2023
de81d72
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 19, 2023
4b40783
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jul 19, 2023
2abb44e
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jul 20, 2023
7b561cd
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jul 21, 2023
c73d0bb
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jul 21, 2023
c85e135
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jul 21, 2023
c33f7b4
Fix bug
hsiehjackson Jul 26, 2023
1ec15bc
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jul 26, 2023
3315ed2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 26, 2023
b7d114d
Fix mask
hsiehjackson Jul 26, 2023
23af796
Fix mask
hsiehjackson Jul 26, 2023
a9fba67
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 26, 2023
14a09f1
Fix mask
hsiehjackson Jul 26, 2023
0b019a1
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jul 26, 2023
f80b651
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jul 26, 2023
febfc0f
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jul 28, 2023
2ba5aed
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Jul 31, 2023
bc28be9
Split PR
hsiehjackson Jul 31, 2023
5c61dfb
Undo commit
hsiehjackson Jul 31, 2023
35f2613
Add query_key to doc_string
hsiehjackson Jul 31, 2023
806b88e
Adjust yzhang123 comments
hsiehjackson Jul 31, 2023
3fade76
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 31, 2023
818a679
Fix error and follow comments
hsiehjackson Jul 31, 2023
12bbae1
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Jul 31, 2023
ba2548d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 31, 2023
84027bf
Remove query key
hsiehjackson Aug 1, 2023
535bbcd
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Aug 1, 2023
44bc444
Remove logic and query
hsiehjackson Aug 1, 2023
bd2aa94
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 1, 2023
650d5ea
Remove query from model
hsiehjackson Aug 1, 2023
dfcec5b
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Aug 1, 2023
4f82530
Remove query_key
hsiehjackson Aug 2, 2023
64f9453
Fix error
hsiehjackson Aug 2, 2023
a14f79d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 2, 2023
8f9598c
Fix pdb
hsiehjackson Aug 2, 2023
0125c58
Fix conflict
hsiehjackson Aug 2, 2023
727c750
Add default tokens_to_generate
hsiehjackson Aug 2, 2023
ea3710b
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Aug 2, 2023
dfac0f0
Revert prompt truncate re-prompt
hsiehjackson Aug 4, 2023
0ab4eeb
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Aug 4, 2023
df4d4c1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 4, 2023
f457c3f
Revert
hsiehjackson Aug 4, 2023
1007152
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Aug 4, 2023
407aae3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 4, 2023
4c712ae
skip generation with metric loss
hsiehjackson Aug 4, 2023
b99f522
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Aug 4, 2023
2b59de8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 4, 2023
ee74e54
Revert
hsiehjackson Aug 4, 2023
9b9626b
Fix conflict
hsiehjackson Aug 4, 2023
67ec1b2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 4, 2023
82a940c
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Aug 7, 2023
c51754e
Merge branch 'main' into fix-gpt-trainer-test
hsiehjackson Aug 7, 2023
ac1241f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 7, 2023
29f775c
Fix bug
hsiehjackson Aug 7, 2023
cf2f2b9
support GPTSFTChatDataset
hsiehjackson Aug 10, 2023
6764801
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 10, 2023
9f936c0
Add comment
hsiehjackson Aug 10, 2023
3d3fccc
Merge branch 'fix-gpt-trainer-test' of https://github.com/NVIDIA/NeMo…
hsiehjackson Aug 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Jenkinsfile
hsiehjackson marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3812,6 +3812,8 @@ assert_frame_equal(training_curve, gt_curve, rtol=1e-3, atol=1e-3)"'''
model.data.test_ds.global_batch_size=1 \
model.data.test_ds.micro_batch_size=1 \
model.data.test_ds.tokens_to_generate=10 \
model.data.test_ds.write_predictions_to_file=True \
model.data.test_ds.output_file_path_prefix='/home/TestData/nlp/lora_tuning_tp2/out' \
inference.greedy=True \
inference.repetition_penalty=1.0 \
inference.outfile_path='/home/TestData/nlp/lora_tuning_tp2/out.jsonl'"
Expand Down Expand Up @@ -3871,6 +3873,8 @@ assert_frame_equal(training_curve, gt_curve, rtol=1e-3, atol=1e-3)"'''
model.data.test_ds.micro_batch_size=1 \
model.data.test_ds.tokens_to_generate=30 \
model.data.test_ds.max_seq_length=6000 \
model.data.test_ds.write_predictions_to_file=True \
model.data.test_ds.output_file_path_prefix='examples/nlp/language_modeling/out' \
inference.greedy=True \
inference.repetition_penalty=1.0 \
inference.outfile_path='examples/nlp/language_modeling/out.jsonl' && \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ model:
# of each chunk at the specified granularity
# 'block' checkpoints the specified number of layers per pipeline stage at the specified granularity
activations_checkpoint_num_layers: null # not used with 'selective'
activations_checkpoint_layers_per_pipeline: null
answer_only_loss: False # not used right now
gradient_as_bucket_view: False

Expand Down Expand Up @@ -113,7 +114,7 @@ model:
truncation_field: ${data.train_ds.truncation_field} # Options: ['context', 'answer']
index_mapping_dir: null # Path to a directory to write index mapping files.
prompt_template: ${data.train_ds.prompt_template}
tokens_to_generate: ???
tokens_to_generate: 32 # decide how many tokens we want to generate to evaluate performance with string metrics

metric:
name: "loss" # Name of the evaluation metric to use. Options: ['exact_string_match', 'loss']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ model:
# of each chunk at the specified granularity
# 'block' checkpoints the specified number of layers per pipeline stage at the specified granularity
activations_checkpoint_num_layers: null # not used with 'selective'
activations_checkpoint_layers_per_pipeline: null
answer_only_loss: True
gradient_as_bucket_view: False

Expand Down Expand Up @@ -160,7 +161,7 @@ model:
truncation_field: "context" # Options: ['context', 'answer']
index_mapping_dir: null # Path to a directory to write index mapping files.
prompt_template: ${model.data.train_ds.prompt_template} # fstring to use for assistant prompt. Example: "Q: {input}\nA: {output}"

tokens_to_generate: 32 # decide how many tokens we want to generate to evaluate performance with string metrics
metric:
name: "loss" # Name of the evaluation metric to use. Options: ['exact_string_match', 'loss']
average: null # Average the metric over the dataset. Options: ['macro', 'micro']. Works only for 'F1', 'accuracy' etc. Refer to torchmetrics for metrics where this is supported.
Expand Down Expand Up @@ -188,7 +189,7 @@ model:
truncation_field: "context" # Options: ['context', 'answer']
index_mapping_dir: null # Path to a directory to write index mapping files.
prompt_template: ${model.data.train_ds.prompt_template}

tokens_to_generate: 32 # decide how many tokens we want to generate to evaluate performance with string metrics
metric:
name: "loss" # Name of the evaluation metric to use. Options: ['exact_string_match', 'loss']
average: null # Average the metric over the dataset. Options: ['macro', 'micro']. Works only for 'F1', 'accuracy' etc. Refer to torchmetrics for metrics where this is supported.
Expand Down
31 changes: 22 additions & 9 deletions examples/nlp/language_modeling/tuning/conf/megatron_gpt_sft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ model:
# of each chunk at the specified granularity
# 'block' checkpoints the specified number of layers per pipeline stage at the specified granularity
activations_checkpoint_num_layers: null # not used with 'selective'
activations_checkpoint_layers_per_pipeline: null
hsiehjackson marked this conversation as resolved.
Show resolved Hide resolved
answer_only_loss: False # not used right now
gradient_as_bucket_view: False
seq_len_interpolation_factor: null # if not None, seq_len_interpolation_factor will match the base model's value
Expand Down Expand Up @@ -108,15 +109,15 @@ model:
names: null # Names of the corresponding datasets used to log metrics.
global_batch_size: ${model.global_batch_size}
micro_batch_size: ${model.micro_batch_size}
shuffle: True
shuffle: False
hsiehjackson marked this conversation as resolved.
Show resolved Hide resolved
num_workers: 4
memmap_workers: ${model.data.train_ds.memmap_workers}
pin_memory: True
max_seq_length: 2048
min_seq_length: 1
drop_last: True
context_key: 'input'
label_key: 'output'
drop_last: False
Copy link
Collaborator

Choose a reason for hiding this comment

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

don't change the default behavior

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Why we drop last if we evaluate with the validation dataset?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I have one use case that I have to drop the last because I am preparing a dataset that computes contrastive loss. I need to make sure all the batch has the same batch size.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If we set drop_last=False, we can use pad_samples_to_global_batch_size=True right? Or this doesn't fulfill your settings? https://github.com/NVIDIA/NeMo/blob/fix-gpt-trainer-test/nemo/collections/nlp/models/language_modeling/megatron_gpt_sft_model.py#L788
If we set default drop_last=True, then we may drop some samples for evaluation which may show incorrect results for comparison.

context_key: ${model.data.train_ds.context_key}
label_key: ${model.data.train_ds.label_key}
add_eos: ${model.data.train_ds.add_eos}
add_sep: ${model.data.train_ds.add_sep}
add_bos: ${model.data.train_ds.add_bos}
Expand All @@ -126,9 +127,10 @@ model:
truncation_field: "context" # Options: ['context', 'answer']
index_mapping_dir: null # Path to a directory to write index mapping files.
prompt_template: ${model.data.train_ds.prompt_template} # fstring to use for assistant prompt. Example: "Q: {input}\nA: {output}"
tokens_to_generate: 32 # decide how many tokens we want to generate to evaluate performance with string metrics

metric:
name: "loss" # Name of the evaluation metric to use. Options: ['exact_string_match', 'loss']
name: "loss" # Name of the evaluation metric to use. Options: ['exact_string_match', 'loss', 'rouge', 'token_f1']
average: null # Average the metric over the dataset. Options: ['macro', 'micro']. Works only for 'F1', 'accuracy' etc. Refer to torchmetrics for metrics where this is supported.
num_classes: null

Expand All @@ -137,15 +139,15 @@ model:
names: null # Names of the corresponding datasets used to log metrics.
global_batch_size: ${model.global_batch_size}
micro_batch_size: ${model.micro_batch_size}
shuffle: True
shuffle: False
num_workers: 4
memmap_workers: ${model.data.train_ds.memmap_workers}
pin_memory: True
max_seq_length: 2048
min_seq_length: 1
drop_last: True
context_key: 'input'
label_key: 'output'
drop_last: False
context_key: ${model.data.train_ds.context_key}
label_key: ${model.data.train_ds.label_key}
add_eos: ${model.data.train_ds.add_eos}
add_sep: ${model.data.train_ds.add_sep}
add_bos: ${model.data.train_ds.add_bos}
Expand All @@ -168,3 +170,14 @@ model:
betas:
- 0.9
- 0.98

inference:
greedy: True # Whether or not to use sampling ; use greedy decoding otherwise
top_k: 0 # The number of highest probability vocabulary tokens to keep for top-k-filtering.
top_p: 0.9 # If set to float < 1, only the most probable tokens with probabilities that add up to top_p or higher are kept for generation.
temperature: 1.0 # sampling temperature
all_probs: False # whether return the log prob for all the tokens in vocab
repetition_penalty: 1.2 # The parameter for repetition penalty. 1.0 means no penalty.
min_tokens_to_generate: 0 # The minimum length of the sequence to be generated.
compute_logprob: False # a flag used to compute logprob of all the input text, a very special case of running inference, default False
compute_attention_mask: True
35 changes: 3 additions & 32 deletions examples/nlp/language_modeling/tuning/megatron_gpt_peft_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def main(cfg) -> None:
peft_model_cfg.data.test_ds = cfg.model.data.test_ds
hsiehjackson marked this conversation as resolved.
Show resolved Hide resolved
peft_model_cfg.activations_checkpoint_granularity = None
peft_model_cfg.activations_checkpoint_method = None
peft_model_cfg.activations_checkpoint_layers_per_pipeline = None
if peft_model_cfg.get("use_flash_attention", False):
peft_model_cfg.use_flash_attention = cfg.model.use_flash_attention
if cfg.model.get("seq_len_interpolation_factor", None) is not None:
Expand Down Expand Up @@ -167,40 +168,10 @@ def main(cfg) -> None:
)

model.freeze()
_test_ds = model._build_dataset(peft_model_cfg.data.test_ds, is_train=False)
request_dl = DataLoader(
dataset=_test_ds[0],
batch_size=peft_model_cfg.data.test_ds.global_batch_size,
collate_fn=_test_ds[0].collate_fn,
)
config = OmegaConf.to_container(cfg.inference, resolve=True)
model.set_inference_config(config)
response = trainer.predict(model, request_dl)

if model.global_rank == 0:
print("***************************")
if cfg.inference.outfile_path is not None:
with open(cfg.inference.outfile_path, "w", encoding="utf-8") as f:
for batch in response:
batch_sentences = [s for s in batch['sentences']]
batch_tokens = [s for s in batch['tokens']]
if cfg.inference.compute_logprob:
batch_logprob = [s.tolist() for s in batch['logprob']]
for s, t, l in zip(batch_sentences, batch_tokens, batch_logprob):
if cfg.inference.get("verbose", False):
d = {
'sentence': s,
'tokens_with_logprobs': ', '.join([f"{_t} {_l:.4f}" for _t, _l in zip(t, l)]),
}
f.write(json.dumps(d, sort_keys=True, indent=2) + '\n')
else:
for s in batch_sentences:
d = {'sentence': s}
f.write(json.dumps(d) + '\n')
print("predictions saved to {}".format(cfg.inference.outfile_path))
else:
print(response)
print("***************************")

hsiehjackson marked this conversation as resolved.
Show resolved Hide resolved
trainer.test(model)


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ def _modify_config(gpt_cfg, cfg, add_cfg_to_tree=False):
gpt_cfg.activations_checkpoint_granularity = cfg.model.get("activations_checkpoint_granularity", None)
gpt_cfg.activations_checkpoint_num_layers = cfg.model.get("activations_checkpoint_num_layers", None)
gpt_cfg.activations_checkpoint_method = cfg.model.get("activations_checkpoint_method", None)
gpt_cfg.activations_checkpoint_layers_per_pipeline = cfg.model.get(
hsiehjackson marked this conversation as resolved.
Show resolved Hide resolved
"activations_checkpoint_layers_per_pipeline", None
)
gpt_cfg.data = cfg.model.data
gpt_cfg.optim = cfg.model.optim
gpt_cfg.precision = cfg.trainer.precision
Expand Down
9 changes: 9 additions & 0 deletions examples/nlp/language_modeling/tuning/megatron_gpt_sft.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def _modify_config(gpt_cfg, cfg, add_cfg_to_tree=False):
gpt_cfg.activations_checkpoint_granularity = cfg.model.get("activations_checkpoint_granularity", None)
gpt_cfg.activations_checkpoint_num_layers = cfg.model.get("activations_checkpoint_num_layers", None)
gpt_cfg.activations_checkpoint_method = cfg.model.get("activations_checkpoint_method", None)
gpt_cfg.activations_checkpoint_layers_per_pipeline = cfg.model.get(
"activations_checkpoint_layers_per_pipeline", None
)
gpt_cfg.data = cfg.model.data
gpt_cfg.optim = cfg.model.optim
gpt_cfg.precision = cfg.trainer.precision
Expand All @@ -61,6 +64,8 @@ def _modify_config(gpt_cfg, cfg, add_cfg_to_tree=False):
gpt_cfg.hidden_dropout = cfg.model.get('hidden_dropout', 0.0)
gpt_cfg.attention_dropout = cfg.model.get('attention_dropout', 0.0)
gpt_cfg.ffn_dropout = cfg.model.ffn_dropout
gpt_cfg.use_flash_attention = cfg.model.get('use_flash_attention', False)

sft_cls = MegatronGPTSFTModel
gpt_cfg.target = f"{sft_cls.__module__}.{sft_cls.__name__}"

Expand Down Expand Up @@ -198,6 +203,10 @@ def main(cfg) -> None:
validate_checkpoint_loading_args(cfg.model.pretrained_checkpoint)
model = load_from_checkpoint_dir(MegatronGPTSFTModel, cfg, trainer, modify_confg_fn=_modify_config)

if 'inference' in cfg:
config = OmegaConf.to_container(cfg.inference, resolve=True)
model.set_inference_config(config)

trainer.fit(model)


Expand Down
55 changes: 54 additions & 1 deletion nemo/collections/common/metrics/classification_accuracy.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
# limitations under the License.

import logging
from typing import List
import re
import string
from collections import Counter
from typing import List, Union

import torch
from torchmetrics import Metric
Expand Down Expand Up @@ -205,3 +208,53 @@ def update(self, pred: str, target: str):

def compute(self):
return self.correct.float() / self.total


class TokenF1Score(Metric):
"""Taken from the official evaluation script for v1.1 of the SQuAD dataset"""

def __init__(self, dist_sync_on_step=False, *args, **kwargs):
super().__init__(dist_sync_on_step=dist_sync_on_step)

self.add_state("correct", default=torch.tensor(0.0), dist_reduce_fx="sum")
self.add_state("total", default=torch.tensor(0), dist_reduce_fx="sum")

def update(self, pred: str, target: Union[str, List[str]]):
if isinstance(target, str):
self.correct += self.f1_score(pred, target)
elif isinstance(target, list):
self.correct += max([self.f1_score(pred, tgt) for tgt in target])
self.total += 1

def compute(self):
return self.correct.float() / self.total

def f1_score(self, prediction, ground_truth):
prediction_tokens = self.normalize(prediction).split()
ground_truth_tokens = self.normalize(ground_truth).split()
common = Counter(prediction_tokens) & Counter(ground_truth_tokens)
num_same = sum(common.values())
if num_same == 0:
return 0.0
precision = 1.0 * num_same / len(prediction_tokens)
recall = 1.0 * num_same / len(ground_truth_tokens)
f1 = (2 * precision * recall) / (precision + recall)
return f1

def normalize(self, s):
"""Lower text and remove punctuation, articles and extra whitespace."""
hsiehjackson marked this conversation as resolved.
Show resolved Hide resolved

def remove_articles(text):
return re.sub(r"\b(a|an|the)\b", " ", text)

def white_space_fix(text):
return " ".join(text.split())

def remove_punc(text):
exclude = set(string.punctuation)
return "".join(ch for ch in text if ch not in exclude)

def lower(text):
return text.lower()

return white_space_fix(remove_articles(remove_punc(lower(s))))
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from torchmetrics import Accuracy, AveragePrecision, F1Score, MatthewsCorrCoef, PearsonCorrCoef, SpearmanCorrCoef
from torchmetrics.text.rouge import ROUGEScore

from nemo.collections.common.metrics.classification_accuracy import ExactStringMatchMetric
from nemo.collections.common.metrics.classification_accuracy import ExactStringMatchMetric, TokenF1Score

__all__ = ['MetricStringToTorchMetric']

Expand All @@ -25,6 +25,7 @@
'accuracy': Accuracy,
'average_precision': AveragePrecision,
'f1': F1Score,
'token_f1': TokenF1Score,
'pearson_corr_coef': PearsonCorrCoef,
'spearman_corr_coef': SpearmanCorrCoef,
'matthews_corr_coef': MatthewsCorrCoef,
Expand Down
Loading
Loading