def to_abstract_test(self, x: (bool, int, float, str, np.floating, np.integer, type(None), EnvInstance)): return AbstractScalar({ VALUE: x, TYPE: dtype.pytype_to_myiatype(type(x)), })
def to_abstract_test(self, x: object): if is_dataclass(x): typ = dtype.pytype_to_myiatype(type(x), x) new_args = {} for name, field in x.__dataclass_fields__.items(): new_args[name] = self(getattr(x, name)) return AbstractClass(typ.tag, new_args, typ.methods) else: raise Exception(f'Cannot convert: {x}')
def S(x=ANYTHING, t=None): return AbstractScalar({ VALUE: x, TYPE: t or dtype.pytype_to_myiatype(type(x)), })
def test_prim_make_record(): t = pytype_to_myiatype(Point) assert make_record(t, 1, 2) == Point(1, 2)
def abs(self): return (self.x**2 + self.y**2)**0.5 @dataclass(frozen=True) class Point3D: x: i64 y: i64 z: i64 def abs(self): return (self.x**2 + self.y**2 + self.z**2)**0.5 Point_t = pytype_to_myiatype(Point) Point3D_t = pytype_to_myiatype(Point3D) Thing_f = pytype_to_myiatype(Thing, Thing(1.0)) Thing_ftup = pytype_to_myiatype(Thing, Thing((1.0, 2.0))) mysum = MultitypeGraph('mysum') @mysum.register(i64) def _mysum1(x): return x @mysum.register(i64, i64) def _mysum2(x, y): return x + y
li32 = List[Int[32]] li64 = List[Int[64]] lf16 = List[Float[16]] lf32 = List[Float[32]] lf64 = List[Float[64]] ai16 = Array[Int[16]] ai32 = Array[Int[32]] ai64 = Array[Int[64]] af16 = Array[Float[16]] af32 = Array[Float[32]] af64 = Array[Float[64]] @dataclass(frozen=True) class Point: x: i64 y: i64 def abs(self): return (self.x ** 2 + self.y ** 2) ** 0.5 def __add__(self, other): return Point(self.x * other.x, self.y * other.y) pt = pytype_to_myiatype(Point) lpt = List[pt]
return x ######### # Tests # ######### def make_model(dtype='float64'): return Model(layers=( TanhLayer(MA(6, 10, dtype=dtype), zeros(1, 10, dtype=dtype)), TanhLayer(MB(10, 8, dtype=dtype), zeros(1, 8, dtype=dtype)), )) Model_t = pytype_to_myiatype(Model, make_model()) Model_t_f32 = pytype_to_myiatype(Model, make_model('float32')) def cost(model, x, y): yy = model.apply(x) diff = (yy - y) return asscalar(array_reduce(scalar_add, diff**2, ())) @infer_std(type=[ ({ 'value': make_model() }, { 'value': MC(3, 6) }, af64),