2b.3.6 LLM demos
See
M06 Tiny model on HF 26.0613
# m06_test.py
import torch
import torch.nn as nn
from huggingface_hub import hf_hub_download
# -----------------------
# Architecture
# -----------------------
class TinyModel(nn.Module):
def __init__(self):
super().__init__()
self.fc = nn.Linear(2, 1)
def forward(self, x):
return self.fc(x)
# -----------------------
# Download .pt from HF
# -----------------------
pt_file = hf_hub_download(
repo_id="terrytaylorbonn/m06-tiny-pytorch-model",
filename="m01_tiny_model.pt"
)
print("Downloaded:")
print(pt_file)
print()
# -----------------------
# Load model
# -----------------------
model = TinyModel()
model.load_state_dict(torch.load(pt_file))
print("Loaded weights:")
print(model.fc.weight)
print(model.fc.bias)
print()
# -----------------------
# Inference
# -----------------------
x = torch.tensor([[1.0, 2.0]])
y = model(x)
print("Input:")
print(x)
print()
print("Output:")
print(y)
python m06_test.py
Downloaded:
/home/terry/.cache/hf_cache/hub/models--terrytaylorbonn--m06-tiny-pytorch-model/snapshots/0e9dd63cdf7a94a04663a1c5f7618f725c15ce67/m01_tiny_model.pt
Loaded weights:
Parameter containing:
tensor([[0.0598, 0.0642]], requires_grad=True)
Parameter containing:
tensor([-0.4107], requires_grad=True)
Input:
tensor([[1., 2.]])
Output:
tensor([[-0.2226]], grad_fn=<AddmmBackward0>)
(venv) terry@LAPTOP-HKPDHF7M:/mnt/c/Users/terry/Downloads/607_predictive$
26.0613 (v1 26.0529)