-
Notifications
You must be signed in to change notification settings - Fork 1
/
base.py
110 lines (88 loc) · 3.44 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from typing import List, Dict, Union
class module:
def __init__(self, layers, embed, *args, useLogFix=True, **kwargs):
from rwkvstic.agnostic.samplers.numpy import npsample
self.VectorType = List[float]
self.useLogFix = useLogFix
self.MatrixType = List[List[float]]
self.TensorType = Union[self.VectorType, self.MatrixType]
self.MatVec = (
self.MatrixType, self.VectorType), self.VectorType
def raiseNotImplemented(*args: List[self.TensorType], **kwargs: Dict[str, self.TensorType]) -> self.TensorType:
print(NotImplementedError())
raise
self.RnnOnly = False
print("init RWKVOPS, from super")
self.initTensor = raiseNotImplemented
self.n_layers = layers
self.processEmbed = lambda x: x
self.initCpuTensor = lambda x: self.initTensor(x)
self.sqrt = raiseNotImplemented
self.mean = raiseNotImplemented
self.relu = raiseNotImplemented
self.exp = raiseNotImplemented
self.maximum = raiseNotImplemented
def add(x, y): return x+y
def divide(x, y): return x/y
def multiply(x, y): return x*y
def subtract(x, y): return x-y
self.intTensor = lambda x: [x] if type(x) == int else x
self.add = add
self.divide = divide
self.multiply = multiply
self.subtract = subtract
self.stack = raiseNotImplemented
self.matvec = raiseNotImplemented
self.prod = raiseNotImplemented
self.layernorm = raiseNotImplemented
self.lerp = raiseNotImplemented
def ppt(x: self.VectorType):
return x
self.postProcessTensor = ppt
# module def
self.module = raiseNotImplemented
self.log = raiseNotImplemented
self.minimum = raiseNotImplemented
self.klimit = raiseNotImplemented
# tensorflow function defs
self.initfunc = lambda x: x
self.layerdef = lambda x: x
self.mainfunc = lambda x: x
import numpy as np
self.emptyState: self.MatrixType = np.array([(([[0.00]*embed, [0.00]*embed, [0.00]*embed, [
0.00]*embed]+([[-1e30]*embed] if self.useLogFix else [])))]*layers)
print(self.emptyState.shape)
def logistical(x: self.VectorType) -> self.VectorType:
return 1 / (self.exp(-x) + 1)
self.logistical = logistical
self.neg = lambda x: -x
self.postProcessModule = lambda x: x
self.mnstack = lambda x: x
self.sample = npsample
def emptyarray(x: int): return [0]*x
self.emptyarray = emptyarray
def arrayPush(x: list, y, i: int): return [*x[0:i], y, *x[i+1:]]
self.arrayPush = arrayPush
def rng(x: int): return range(x)
self.rng = rng
def pop(x): return x[-1:]
self.pop = pop
def arrayGet(x, i: int): return x[i]
self.arrayGet = arrayGet
def push(x, y):
x[0] = y
return x
self.push = push
self.roll = lambda x: [*x[0:1], *x[0:-1]]
# typing, set as any
self.tensorDef = None
def lenn(x): return len(x)
self.len = lenn
self.stackEmb = False
def getIndex(x, y): return x[y]
self.getIndex = getIndex
def scatter(x, y, z):
x[y] = z
return x
self.scatterindices = [slice(2, 5), slice(2, 4), slice(0, 2)]
self.scatter = scatter