generated from rochacbruno/python-project-template
-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(kan_gpt,mlp_gpt,kan): test cases for forward-backward passes
- Loading branch information
Showing
5 changed files
with
349 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import torch | ||
from kan_gpt.model import GPT as KAN_GPT | ||
|
||
VOCAB_SIZE = 8 | ||
BLOCK_SIZE = 16 | ||
MODEL_TYPE = "gpt-nano" | ||
|
||
|
||
def get_gpt_model() -> KAN_GPT: | ||
model_config = KAN_GPT.get_default_config() | ||
model_config.model_type = MODEL_TYPE | ||
model_config.vocab_size = VOCAB_SIZE | ||
model_config.block_size = BLOCK_SIZE | ||
model = KAN_GPT(model_config) | ||
return model | ||
|
||
|
||
def test_forward(): | ||
with torch.no_grad(): | ||
model = get_gpt_model() | ||
x = torch.zeros((1, BLOCK_SIZE), dtype=torch.long) | ||
|
||
y, loss = model.forward(x) | ||
|
||
assert y.shape == ( | ||
1, | ||
BLOCK_SIZE, | ||
VOCAB_SIZE, | ||
), f"Shape mismatch: {y.shape}" | ||
|
||
|
||
def test_backward(): | ||
model = get_gpt_model() | ||
x = torch.zeros((1, BLOCK_SIZE), dtype=torch.long) | ||
|
||
# Make sure grads exist | ||
requires_grad_set = set() | ||
for param in model.parameters(): | ||
if param.requires_grad: | ||
requires_grad_set.add(param) | ||
assert len(requires_grad_set) > 0, "requires_grad is not set" | ||
|
||
y, loss = model.forward(x) | ||
|
||
assert y.shape == (1, BLOCK_SIZE, VOCAB_SIZE), f"Shape mismatch: {y.shape}" | ||
|
||
loss = y.mean() | ||
loss.backward() | ||
|
||
# Make sure grads exist | ||
grad_set = set() | ||
for param in model.parameters(): | ||
if isinstance(param.grad, torch.Tensor): | ||
grad_set.add(param) | ||
assert len(grad_set) > 0, f"Tensor.grad missing" | ||
|
||
|
||
def test_forward_batched(): | ||
with torch.no_grad(): | ||
model = get_gpt_model() | ||
x = torch.zeros((2, BLOCK_SIZE), dtype=torch.long) | ||
|
||
y, loss = model.forward(x) | ||
|
||
assert y.shape == ( | ||
2, | ||
BLOCK_SIZE, | ||
VOCAB_SIZE, | ||
), f"Shape mismatch: {y.shape}" | ||
|
||
|
||
def test_backward_batched(): | ||
model = get_gpt_model() | ||
x = torch.zeros((2, BLOCK_SIZE), dtype=torch.long) | ||
|
||
# Make sure grads exist | ||
requires_grad_set = set() | ||
for param in model.parameters(): | ||
if param.requires_grad: | ||
requires_grad_set.add(param) | ||
assert len(requires_grad_set) > 0, "requires_grad is not set" | ||
|
||
y, loss = model.forward(x) | ||
|
||
assert y.shape == (2, BLOCK_SIZE, VOCAB_SIZE), f"Shape mismatch: {y.shape}" | ||
|
||
loss = y.mean() | ||
loss.backward() | ||
|
||
# Make sure grads exist | ||
grad_set = set() | ||
for param in model.parameters(): | ||
if isinstance(param.grad, torch.Tensor): | ||
grad_set.add(param) | ||
assert len(grad_set) > 0, f"Tensor.grad missing" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import torch | ||
from kan_gpt.mingpt.model import GPT as MLP_GPT | ||
|
||
VOCAB_SIZE = 8 | ||
BLOCK_SIZE = 16 | ||
MODEL_TYPE = "gpt-nano" | ||
|
||
|
||
def get_gpt_model() -> MLP_GPT: | ||
model_config = MLP_GPT.get_default_config() | ||
model_config.model_type = MODEL_TYPE | ||
model_config.vocab_size = VOCAB_SIZE | ||
model_config.block_size = BLOCK_SIZE | ||
model = MLP_GPT(model_config) | ||
return model | ||
|
||
|
||
def test_forward(): | ||
with torch.no_grad(): | ||
model = get_gpt_model() | ||
x = torch.zeros((1, BLOCK_SIZE), dtype=torch.long) | ||
|
||
y, loss = model.forward(x) | ||
|
||
assert y.shape == ( | ||
1, | ||
BLOCK_SIZE, | ||
VOCAB_SIZE, | ||
), f"Shape mismatch: {y.shape}" | ||
|
||
|
||
def test_backward(): | ||
model = get_gpt_model() | ||
x = torch.zeros((1, BLOCK_SIZE), dtype=torch.long) | ||
|
||
# Make sure grads exist | ||
requires_grad_set = set() | ||
for param in model.parameters(): | ||
if param.requires_grad: | ||
requires_grad_set.add(param) | ||
assert len(requires_grad_set) > 0, "requires_grad is not set" | ||
|
||
y, loss = model.forward(x) | ||
|
||
assert y.shape == (1, BLOCK_SIZE, VOCAB_SIZE), f"Shape mismatch: {y.shape}" | ||
|
||
loss = y.mean() | ||
loss.backward() | ||
|
||
# Make sure grads exist | ||
grad_set = set() | ||
for param in model.parameters(): | ||
if isinstance(param.grad, torch.Tensor): | ||
grad_set.add(param) | ||
assert len(grad_set) > 0, f"Tensor.grad missing" | ||
|
||
|
||
def test_forward_batched(): | ||
with torch.no_grad(): | ||
model = get_gpt_model() | ||
x = torch.zeros((2, BLOCK_SIZE), dtype=torch.long) | ||
|
||
y, loss = model.forward(x) | ||
|
||
assert y.shape == ( | ||
2, | ||
BLOCK_SIZE, | ||
VOCAB_SIZE, | ||
), f"Shape mismatch: {y.shape}" | ||
|
||
|
||
def test_backward_batched(): | ||
model = get_gpt_model() | ||
x = torch.zeros((2, BLOCK_SIZE), dtype=torch.long) | ||
|
||
# Make sure grads exist | ||
requires_grad_set = set() | ||
for param in model.parameters(): | ||
if param.requires_grad: | ||
requires_grad_set.add(param) | ||
assert len(requires_grad_set) > 0, "requires_grad is not set" | ||
|
||
y, loss = model.forward(x) | ||
|
||
assert y.shape == (2, BLOCK_SIZE, VOCAB_SIZE), f"Shape mismatch: {y.shape}" | ||
|
||
loss = y.mean() | ||
loss.backward() | ||
|
||
# Make sure grads exist | ||
grad_set = set() | ||
for param in model.parameters(): | ||
if isinstance(param.grad, torch.Tensor): | ||
grad_set.add(param) | ||
assert len(grad_set) > 0, f"Tensor.grad missing" |
Oops, something went wrong.