Add ModelOpt QAT example for Llama2 SFT model
State: closed | Pull request: yes
Labels: name: NLP

name: CI

name: Run CICD

# What does this PR do ?
- Add an example for doing Quantization aware Training (QAT) for Llama SFT model.
- Requires https://github.com/NVIDIA/NeMo/pull/9276 to be merged first

**Collection**: nlp
- `nemo/collections/nlp/parts/nlp_overrides.py` is modified to save/restore model optimizer sharded state to the .nemo checkpoint

# Changelog 
- Add support to save model optimizer's quantization state to the `.nemo` checkpoint (similar support is already merged to Megatron-LM) to save/resume PyTorch Quantization state before TRT-LLM export is done
- Add an example to perform Quantization-aware training (QAT) for the Llama2 SFT Model
- Updated necessary documentation
- Add INT4 QAT CI test 
- Upgrade ModelOpt version from 0.11 to 0.13

# Usage
```bash
python examples/nlp/language_modeling/tuning/megatron_gpt_qat.py \
  quantization.algorithm=fp8 \
  quantization.num_calib_size=8 \
  trainer.devices=1 \
  trainer.num_nodes=1 \
  trainer.max_steps=4 \
  trainer.val_check_interval=4 \
  +trainer.limit_val_batches=2 \
  exp_manager.explicit_log_dir=llama2_qat_results \
  model.restore_from_path=/home/TestData/nlp/megatron_llama/llama_ci.nemo \
  model.tensor_model_parallel_size=1 \
  model.pipeline_model_parallel_size=1 \
  model.global_batch_size=2 \
  model.data.train_ds.file_names=[/home/TestData/nlp/megatron_sft/quarel.jsonl] \
  model.data.train_ds.concat_sampling_probabilities=[1.0]
  model.data.validation_ds.file_names=[/home/TestData/nlp/megatron_sft/quarel.jsonl] \
```

# GitHub Actions CI

The Jenkins CI system has been replaced by GitHub Actions self-hosted runners.

The GitHub Actions CI will run automatically when the "Run CICD" label is added to the PR.
To re-run CI remove and add the label again.
To run CI on an untrusted fork, a NeMo user with write access must first click "Approve and run".

# Before your PR is "Ready for review"
**Pre checks**:
- [x] Make sure you read and followed [Contributor guidelines](https://github.com/NVIDIA/NeMo/blob/main/CONTRIBUTING.md)
- [x] Did you write any new necessary tests?
- [x] Did you add or update any necessary documentation?
- [x] Does the PR affect components that are optional to install? (Ex: Numba, Pynini, Apex etc)
  - [ ] Reviewer: Does the PR have correct import guards for all optional libraries?
  
**PR Type**:
- [x] New Feature
- [ ] Bugfix
- [x] Documentation

If you haven't finished some of the above items you can still open "Draft" PR.


## Who can review?

Anyone in the community is free to review the PR once the checks have passed. 
[Contributor guidelines](https://github.com/NVIDIA/NeMo/blob/main/CONTRIBUTING.md) contains specific people who can review PRs to various areas.

# Additional Information
* Related to # (issue)


Can we enable the gradient/activation checkpointing from Nemo trainer? This will make the GPU memory requirement for QAT similar to that of original finetuning.

@realAsma should we disable FP8 KV cache for INT4 weight only?

| FP8 KV Cache | PTQ | QAT |
| --------|-------|-------- |
| Enabled | 1.345 | 1.216 |
| Disabled | 1.305 | 1.172 |

BF16 baseline is 1.15

LGTM;

> @realAsma should we disable FP8 KV cache for INT4 weight only?
> 
> FP8 KV Cache	PTQ	QAT
> Enabled	1.345	1.216
> Disabled	1.305	1.172
> BF16 baseline is 1.15

That depends on how easy it is to enable KV cache quantization from the config file. If it is easy to do, sure we can enable. However this might not have huge value if there are already good PTQ examples demonstrating KV cache quantization.

> That depends on how easy it is to enable KV cache quantization from the config file. If it is easy to do, sure we can enable. However this might not have huge value if there are already good PTQ examples demonstrating KV cache quantization.

We currently don't have KV cache quantization parameter in config and its enabled by default for all except int8_sq.

For INT4 having it enabled (default) makes QAT look less appealing as we are still a bit off from bf16 so I though it would be better to disable it.

Let me make if a config parameter and disable it for now -- maybe speedup may be lower

LGTM!