77a8919349
* UV+DTR model * DTR model.. again. * fix naviGPS * fix radar... * fix.. * test * fix.. * carrot serv * fix.. * fix.. fleet * fix.. radar * fix atc * Steam Powered model.. * fix.. radarLatFactor range.. 200->500 * fix.. dbc.. * side * SP v2 * brake light * fix brakelight * fix.. * add datetime... * fix.. * fix.. * fix.. * fix.. * blind spot * fix tz * fix.. * ff * radarLatFactor * fix.. bsd * Revert "fix.. bsd" This reverts commit 1d0d1434470e1b92c65eaffaeb8dd7cd779f85ee. * fix.. bsd side.. * test * fix.. e2e conditions * Revert "test" This reverts commit 0ce791dbd66c17260366ed1a4df2626c602dbb7d. * TR16 * fix cut-in detect threshold 3.4 -> 2.6 * fix.. jerk_l limit 5->10 * fix.. * fix.. gm * fix.. OPTIMA_H mass * fix.. radar.. * fix radar.. * fix.. * Radar... * fix.. * fix.. * fix.. * fix.. radartrack 3 * fix.. * fix.. * fix.. * merge.. * fix.. canfd * fix.. * fix.. * fix.. * fix.. radard * new cut_in * Revert "new cut_in" This reverts commit b9b6e9b33318fe1ce7d626468139b17848efcdcd. * fix.. * new cut_in detect... * fix.. disp.. * fix.. * fix.. * fix.. center radar.. * fix.. radar y_sane.. * fix.. * fix.. * hkg jerk 10 -> 5 * fix.. * fix.. * fix.. radar dbc.. * fix.. * fix.. jLead filter.. * test new radar interface.. * fix.. * fix.. * test time... * Revert "test time..." This reverts commit 63e9187736985c4dc4b4f3736674ba7cda6adc3f. * fix radar.. * fix.. * FireHose model.. * tinygrad * Update interface.py * fix.. * fix.. nff toyota corolla_tss2 * fix.. * fix.. * fix.. radar * fix.. * fix.. radar, y_gate * fix.. radar.. * fix.. for clone.. * scc radar enable at low speed.. * fix.. settings.. * fix. * fix.. * fix.. radarTimeStep. * TR16 model again.. * RELEASE.md * fix cut-in detection... * fix.. registeration timeout 15sec.. * fix.. * fix.. radar processing. * fix.. * fix.. * fix.. * fix.. * fix.. * fix..
60 lines
2.0 KiB
Python
60 lines
2.0 KiB
Python
import unittest
|
|
from tinygrad import nn, Tensor, Variable, Context, Device
|
|
from tinygrad.helpers import trange
|
|
|
|
class Model:
|
|
def __init__(self): self.layer = nn.Linear(28*28, 10)
|
|
def __call__(self, x:Tensor) -> Tensor: return self.layer(x.flatten(1))
|
|
|
|
class TestStunning(unittest.TestCase):
|
|
def test_indexing_variable(self):
|
|
a = Tensor.arange(100*10).reshape(100, 10).contiguous()
|
|
|
|
# index without variable
|
|
nv = a[12].tolist()
|
|
|
|
# index with variable
|
|
vi = Variable('i', 0, a.shape[0]-1)
|
|
wv = a[vi.bind(12)].tolist()
|
|
|
|
self.assertListEqual(nv, wv)
|
|
|
|
def test_indexing_two_bind(self):
|
|
a = Tensor.arange(100*10).reshape(100, 10).contiguous()
|
|
|
|
nv = a[12].cat(a[76]).tolist()
|
|
|
|
vi = Variable('i', 0, a.shape[0]-1)
|
|
with self.assertRaisesRegex(AssertionError, "bind mismatch on"):
|
|
wv = a[vi.bind(12)].cat(a[vi.bind(76)]).tolist()
|
|
self.assertListEqual(nv, wv)
|
|
|
|
@unittest.skipIf(Device.DEFAULT in {"WEBGPU", "NV", "CUDA"}, "Too many buffers / too slow")
|
|
@unittest.skip("This is binding a Variable to two different values")
|
|
def test_simple_train(self, steps=6, bs=4, adam=True):
|
|
X_train, Y_train, _, _ = nn.datasets.mnist()
|
|
model = Model()
|
|
if adam: opt = nn.optim.Adam(nn.state.get_parameters(model))
|
|
else: opt = nn.optim.SGD(nn.state.get_parameters(model), momentum=0.1)
|
|
samples = Tensor.randint(steps, bs, high=X_train.shape[0])
|
|
Y_train = Y_train.one_hot(10)
|
|
X_samp, Y_samp = X_train[samples], Y_train[samples]
|
|
vi = Variable('i', 0, samples.shape[0]-1)
|
|
with Context(FUSE_ARANGE=1, SPLIT_REDUCEOP=0):
|
|
with Tensor.train():
|
|
losses = []
|
|
for i in range(samples.shape[0]):
|
|
vib = vi.bind(i)
|
|
opt.zero_grad()
|
|
pred = model(X_samp[vib].realize())
|
|
loss = (pred - Y_samp[vib]).square().mean()
|
|
losses.append(loss.backward())
|
|
opt.schedule_step()
|
|
#losses = Tensor.stack(*losses)
|
|
|
|
# run
|
|
for i in (t:=trange(len(losses))): t.set_description(f"loss: {losses[i].item():6.2f}")
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|