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

[sharktank] Add test for sharded rotary table #274

Merged
merged 2 commits into from
Oct 11, 2024
Merged
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
56 changes: 56 additions & 0 deletions sharktank/tests/layers/sharded_rotary_embedding_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2024 Advanced Micro Devices, Inc.
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception


import torch

from sharktank.layers import RotaryEmbeddingLayer
from sharktank import ops
from sharktank.types import (
ShardedTensor,
SplitPrimitiveTensor,
unbox_tensor,
)

import unittest
from typing import List, Optional
import os


def test_sharded_rotary_table():
bs = 4
rope_dims = 16
heads = 8
max_seqlen = 128
rope_freq_base = None

# First we setup and get the default rotary embedding layer
xq = torch.rand((bs, max_seqlen, heads, rope_dims), dtype=torch.float)
Copy link
Contributor

Choose a reason for hiding this comment

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

You may seed the torch RNG to make sure we get deterministic test results.

xk = torch.rand((bs, max_seqlen, heads, rope_dims), dtype=torch.float)
default_layer = RotaryEmbeddingLayer(
rope_dimension_count=rope_dims,
max_seqlen=max_seqlen,
rope_freq_base=rope_freq_base,
)
oq, ok = default_layer(xq=xq, xk=xk, start_index=0)

# Then we can shard the same inputs and layer
xq = SplitPrimitiveTensor(ts=xq, shard_dim=2, shard_count=4)
xk = SplitPrimitiveTensor(ts=xk, shard_dim=2, shard_count=4)
shard_layer = RotaryEmbeddingLayer(
rope_dimension_count=rope_dims,
max_seqlen=max_seqlen,
rope_freq_base=rope_freq_base,
tensor_parallelism_size=4,
)
sq, sk = shard_layer(xq=xq, xk=xk, start_index=0)

# Gathering and unboxing should yield the same results
sq = ops.unshard(sq)
sk = ops.unshard(sk)

torch.testing.assert_close(sq, oq)
torch.testing.assert_close(sk, ok)
Loading