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 SDPA backend tests and refactor generate.py #1477

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
62 changes: 62 additions & 0 deletions .github/workflows/more-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
gpu-arch-version: "12.4"
timeout: 60
script: |
set -xeou pipefail
echo "::group::Print machine info"
uname -a
echo "::endgroup::"
Expand Down Expand Up @@ -83,3 +84,64 @@ jobs:
echo "tests complete"
echo "******************************************"
echo "::endgroup::"

test-sdpa-backends:
permissions:
id-token: write
contents: read
uses: pytorch/test-infra/.github/workflows/linux_job_v2.yml@main
with:
runner: linux.g5.4xlarge.nvidia.gpu
gpu-arch-type: cuda
gpu-arch-version: "12.4"
timeout: 60
script: |
set -xeou pipefail
echo "::group::Print machine info"
uname -a
echo "::endgroup::"

echo "::group::Download checkpoints"
# Install requirements
./install/install_requirements.sh cuda
pip3 list
python3 -c 'import torch;print(f"torch: {torch.__version__, torch.version.git_version}")'
echo "::endgroup::"

echo "::group::Download checkpoints"
mkdir -p checkpoints/stories15M
pushd checkpoints/stories15M
wget https://huggingface.co/karpathy/tinyllamas/resolve/main/stories15M.pt
wget https://github.com/karpathy/llama2.c/raw/master/tokenizer.model
popd
echo "::endgroup::"

echo "::group::Run inference"
export MODEL_PATH=checkpoints/stories15M/stories15M.pt
export MODEL_NAME=stories15M
export MODEL_DIR=/tmp

for DEVICE in cpu cuda; do
for DTYPE in bfloat16 float16 float32; do
for SDPA in 'math' 'flash_attention' 'efficient_attention' 'cudnn_attention'; do
echo "******************************************************************"
echo "******* $DEVICE $DTYPE $SDPA "
###################################################################
# Python execution interpreted vanilla
python torchchat.py generate --checkpoint-path ${MODEL_PATH} --attention-backend ${SDPA} --device ${DEVICE} --dtype ${DTYPE} --temperature 0
###################################################################
# prefill, and compile and prefill compile
python torchchat.py generate --checkpoint-path ${MODEL_PATH} --attention-backend ${SDPA} --device ${DEVICE} --dtype ${DTYPE} --temperature 0 --compile --compile-prefill
###################################################################
# sequential prefill
python torchchat.py generate --checkpoint-path ${MODEL_PATH} --attention-backend ${SDPA} --device ${DEVICE} --dtype ${DTYPE} --temperature 0 --sequential-prefill
###################################################################
# prefill, and compile
python torchchat.py generate --checkpoint-path ${MODEL_PATH} --attention-backend ${SDPA} --device ${DEVICE} --dtype ${DTYPE} --temperature 0 --sequential-prefill --compile
done
done
done

echo "tests complete"
echo "******************************************"
echo "::endgroup::"
10 changes: 4 additions & 6 deletions torchchat/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,6 @@ def decode_n_tokens(
callback=lambda _: _,
eos_token_id: int = 2,
eot_id: Optional[int] = None,
attention_backend: SDPBackend = torch.nn.attention.SDPBackend.MATH,
**sampling_kwargs,
):
new_tokens, new_probs = [], []
Expand All @@ -541,7 +540,8 @@ def decode_n_tokens(
num_new_tokens - 1
): # -1 to save space to run an EoS if dont generate it naturally
# Actually better for Inductor to codegen attention here
with torch.nn.attention.sdpa_kernel([attention_backend]):
# with torch.nn.attention.sdpa_kernel([attention_backend]):
if True: # preserve indentation while testing

out_token = cur_token.clone()
next_token, next_prob = self.decode_one_token(
Expand Down Expand Up @@ -685,7 +685,6 @@ def generate(
sequential_prefill=True,
callback=lambda x: x,
max_seq_length: int,
attention_backend: SDPBackend = torch.nn.attention.SDPBackend.MATH,
seed: Optional[int] = None,
**sampling_kwargs,
) -> torch.Tensor:
Expand Down Expand Up @@ -802,7 +801,6 @@ def generate(
if self.is_llama3_model
else None
),
attention_backend=attention_backend,
**sampling_kwargs,
):
generated_tokens.append(generated_token.view(-1))
Expand Down Expand Up @@ -1174,7 +1172,8 @@ def callback(x, *, done_generating=False):
prof = torch.profiler.profile()
t0 = time.perf_counter()
num_tokens_generated = 0
with prof:
# always allow math as fallback
with torch.nn.attention.sdpa_kernel([self.builder_args.attention_backend, torch.nn.attention.SDPBackend.MATH]), prof:
generator_func = self.generate(
self.model,
encoded,
Expand All @@ -1190,7 +1189,6 @@ def callback(x, *, done_generating=False):
start_pos=start_pos,
skip_cache_setup=not is_first_sample,
max_seq_length=max_seq_length,
attention_backend=self.builder_args.attention_backend,
)
if generator_args.chat_mode:
start_pos += encoded.size(0)
Expand Down
Loading