mirror of
https://github.com/MLSysBook/TinyTorch.git
synced 2026-05-31 06:11:52 -05:00
This commit includes: - Exported tinytorch package files from nbdev (autograd, losses, optimizers, training, etc.) - Updated activations.py and layers.py with __call__ methods - New module exports: attention, spatial, tokenization, transformer, etc. - Removed old _modidx.py file - Cleanup of duplicate milestone directories These are the generated package files that correspond to the source modules we've been developing. Students will import from these when using TinyTorch.
58 lines
1.7 KiB
Python
Generated
58 lines
1.7 KiB
Python
Generated
# AUTOGENERATED! DO NOT EDIT! File to edit: ../../modules/source/14_kvcaching/kvcaching_dev.ipynb.
|
|
|
|
# %% auto 0
|
|
__all__ = ['Tensor']
|
|
|
|
# %% ../../modules/source/14_kvcaching/kvcaching_dev.ipynb 1
|
|
import numpy as np
|
|
import time
|
|
from typing import Tuple, Optional, Dict, List
|
|
from dataclasses import dataclass
|
|
|
|
# Import our TinyTorch components (Modules 01-13)
|
|
### BEGIN SOLUTION
|
|
# Note: In real implementation, these would import from previous modules
|
|
# For now, we'll implement minimal versions to focus on caching concepts
|
|
|
|
class Tensor:
|
|
"""Minimal Tensor for KV Caching focus (from Module 01)"""
|
|
def __init__(self, data, requires_grad=False):
|
|
self.data = np.array(data)
|
|
self.shape = self.data.shape
|
|
self.requires_grad = requires_grad
|
|
self.grad = None
|
|
|
|
def __getitem__(self, key):
|
|
return Tensor(self.data[key])
|
|
|
|
def __setitem__(self, key, value):
|
|
if isinstance(value, Tensor):
|
|
self.data[key] = value.data
|
|
else:
|
|
self.data[key] = value
|
|
|
|
def size(self, dim=None):
|
|
if dim is None:
|
|
return self.shape
|
|
return self.shape[dim]
|
|
|
|
def view(self, *shape):
|
|
return Tensor(self.data.reshape(shape))
|
|
|
|
def transpose(self, dim0, dim1):
|
|
axes = list(range(len(self.shape)))
|
|
axes[dim0], axes[dim1] = axes[dim1], axes[dim0]
|
|
return Tensor(np.transpose(self.data, axes))
|
|
|
|
@staticmethod
|
|
def cat(tensors, dim=0):
|
|
"""Concatenate tensors along dimension"""
|
|
arrays = [t.data for t in tensors]
|
|
return Tensor(np.concatenate(arrays, axis=dim))
|
|
|
|
@staticmethod
|
|
def zeros(*shape):
|
|
"""Create zero tensor"""
|
|
return Tensor(np.zeros(shape))
|
|
### END SOLUTION
|