~magnusmorton/delayrepay

ref: 5052fe013379135757f6e26effed71e083221f26 delayrepay/delayrepay/cpu.py -rw-r--r-- 638 bytes
5052fe01 — Magnus Morton env based backend select 5 months ago
                                                                                
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
import numpy
from .visitor import Visitor

np = numpy
fallback = numpy


def is_ndarray(arr):
    return isinstance(arr, numpy.ndarray)


class CpuVisitor(Visitor):
    def visit_NPArray(self, node):
        return node.array

    def visit_Scalar(self, node):
        return node.val

    def visit_UnaryFuncEx(self, node):
        return node.func(*self.visit(node.children))

    def visit_BinaryFuncEx(self, node):
        return node.func(*self.visit(node.children))

    def visit_BinaryNumpyEx(self, node):
        return node.func(*self.visit(node.children))


def run(ex):
    visitor = CpuVisitor()
    return visitor.visit(ex)