def __call__(self, ops): # boxing inputs if needed ops = box(ops) # graph computation or eager? if self.debug: input_types = [type(op) for op in ops] self.print_debug('inputs type:%s' % (input_types)) if issubclass(type(ops[0]), OP): # graph mode self.print_debug('graph mode') input_shapes = [] for op in ops: assert issubclass(type(op), OP) self.input_ops.append(op) input_shapes.append(op.get_output_shapes()) self.compute_output_shape(input_shapes) return self else: # eager mode self.print_debug('eager mode') # check inputs are valis for op in ops: if not B.is_tensor(op): raise ValueError("Expecting list(tensors) or a tensor") # input shapes input_shapes = [] for op in ops: input_shapes.append(op.shape) self.compute_output_shape(input_shapes) # compute concrete results - dispatch iterate through populations. return unbox(self.dispatch(ops, self.EAGER))
def _call_from_graph(self, populations): "Function called during graph executions" populations = box(populations) # dispatch take care of iterating through populations return unbox(self.dispatch(populations, self.GRAPH))
def get_output_shapes(self): "return the shape of tensor returned by the op" return unbox(self.output_shapes)
def get_populations(self): return unbox(self._populations)
def test_box_unbox_tensor(): val = tensor([1, 2, 3]) assert tensor_equal(unbox(box(val)), val)
def test_box_unbox(): vals = ['a', [1, 2, 3]] for val in vals: assert unbox(box(val)) == val