def test_reshape(): element_type = Type.f32 shape = Shape([2, 3]) A = Parameter(element_type, shape) parameter_list = [A] function = Function([Reshape(A, AxisVector([0, 1]), Shape([3, 2]))], parameter_list, 'test') backend = Backend.create(test.BACKEND_NAME) a = backend.create_tensor(element_type, shape) result = backend.create_tensor(element_type, Shape([3, 2])) a.write( util.numpy_to_c(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)), 24) result_arr = np.array([[0, 0], [0, 0], [0, 0]], dtype=np.float32) result.write(util.numpy_to_c(result_arr), 24) handle = backend.compile(function) handle.call([result], [a]) result.read(util.numpy_to_c(result_arr), 24) a_arr = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32) result_arr_ref = np.reshape(a_arr, (3, 2)) assert np.allclose(result_arr, result_arr_ref)
def transpose(op, order): # type: (Node, List[int]) -> Node """Transpose data via reshape.""" v = [] for i in range(len(order)): v.append(op.get_shape()[order[i]]) new_shape = v return Reshape(op, AxisVector(order), Shape(new_shape))
def test_reshape(): element_type = Type.f32 shape = Shape([2, 3]) A = Parameter(element_type, shape) parameter_list = [A] function = Function( NodeVector([Reshape(A, AxisVector([0, 1]), Shape([3, 2]))]), parameter_list, 'test') backend, cf = make_backend_call_frame(function) a = backend.make_primary_tensor_view(element_type, shape) result = backend.make_primary_tensor_view(element_type, Shape([3, 2])) a.write( util.numpy_to_c(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)), 0, 24) result_arr = np.array([[0, 0], [0, 0], [0, 0]], dtype=np.float32) result.write(util.numpy_to_c(result_arr), 0, 24) cf.call([result], [a]) result.read(util.numpy_to_c(result_arr), 0, 24) a_arr = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32) result_arr_ref = np.reshape(a_arr, (3, 2)) assert np.allclose(result_arr, result_arr_ref)
def visit(self, op, x): self.computation.set_op_rank(op) self.computation.set_op_rank(op) ngraph_unflatten = PyngReshape(self.computation.lookup_cpp_op(x), AxisVector(list(range(0, len(x.axes)))), Shape(list(op.axes.lengths))) self.computation.register_cpp_op(op, ngraph_unflatten)
def test_reshape(): element_type = Type.f32 shape = Shape([2, 3]) A = Parameter(element_type, shape) parameter_list = [A] function = Function( NodeVector([Reshape(A, AxisVector([0, 1]), Shape([3, 2]))]), parameter_list, 'test') backend = Backend.create(pytest.config.getoption('backend')) a = backend.create_tensor(element_type, shape) result = backend.create_tensor(element_type, Shape([3, 2])) a.write( util.numpy_to_c(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)), 0, 24) result_arr = np.array([[0, 0], [0, 0], [0, 0]], dtype=np.float32) result.write(util.numpy_to_c(result_arr), 0, 24) backend.call(function, [result], [a]) result.read(util.numpy_to_c(result_arr), 0, 24) a_arr = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32) result_arr_ref = np.reshape(a_arr, (3, 2)) assert np.allclose(result_arr, result_arr_ref)
def reshape(node, input_order, output_shape, name=None): # type: (Node, List[int], List[int], str) -> None """Return reshaped node according to provided parameters. :param node: The tensor we want to reshape. :param input_order: The order in which to iterate over input axes of input tensor. :param output_shape: The new shape for input tensor. """ return Reshape(node, AxisVector(input_order), Shape(output_shape))
def visit(self, op, input): self.computation.set_op_rank(op) axis_order = [] reorder_axes = list(op.axes.lengths) reorder_axes_names = op.axes.names input_axes_names = op.args[0].axes.names # determine the axis order for the reshape for reorder_axis_name in reorder_axes_names: index = input_axes_names.index(reorder_axis_name) axis_order.append(index) ngraph_input = self.computation.lookup_cpp_op(op.args[0]) # print(ngraph_input.get_output_shape(0)) ngraph_cpp_reorder_op = PyngReshape(ngraph_input, AxisVector(axis_order), Shape(reorder_axes)) self.computation.register_cpp_op(op, ngraph_cpp_reorder_op)
def visit(self, op, x): self.computation.set_op_rank(op) # op.args[0] : x # op.slices lowers = [] uppers = [] strides = [] axes_to_remove = [] for axis, s in zip(x.axes, op.slices): if isinstance(s, int): lowers.append(s) uppers.append(s + 1) strides.append(1) axes_to_remove.append(axis) else: if s.start is None: lowers.append(0) else: lowers.append(s.start) if s.step is None: strides.append(1) else: strides.append(s.step) if s.stop is None: uppers.append(axis.length) else: uppers.append(s.stop) op_element_type = self.computation.lookup_cpp_op(x) ngraph_sliced = PyngSlice(op_element_type, Coordinate(lowers), Coordinate(uppers), Strides(strides)) if axes_to_remove: ngraph_sliced = PyngReshape( ngraph_sliced, AxisVector(list(range(0, len(x.axes)))), Shape(list(op.axes.lengths))) self.computation.register_cpp_op(op, ngraph_sliced)
def visit(self, op, input1, input2): self.computation.set_op_rank(op) # determine the reduction_axes count reduction_axes_count, reduction_axes = self.get_reduction_axis(op) reshape_input_needed = False reshape_output_needed = False # check if the input1/input2 needs to be Transposed and if yes, Transpose if (len(input1.axes.names) != 0 and len(input2.axes.names) != 0) \ and (input1.axes.names[-1] != input2.axes.names[0]): reshape_input_needed = True input1_axes = list((op.x_out_axes + op.reduction_axes).names) input2_axes = list((op.reduction_axes + op.y_out_axes).names) input1_axes_order = self.get_axes_order_from_axes_name( input1.axes.names, input1_axes) input1_axes_shape = self.get_shape_from_axes_order( input1_axes_order, input1.axes.lengths) input2_axes_order = self.get_axes_order_from_axes_name( input2.axes.names, input2_axes) input2_axes_shape = self.get_shape_from_axes_order( input2_axes_order, input2.axes.lengths) else: input1_axes_shape = list(input1.axes.lengths) input1_axes_order = list(range(0, len(input1.axes))) input2_axes_shape = list(input2.axes.lengths) input2_axes_order = list(range(0, len(input2.axes))) # flatten reduction_axes if reduction_axes_count > 1: reshape_input_needed = True input1_axes_shape = input1_axes_shape[:-reduction_axes_count] + \ [np.prod(input1_axes_shape[-reduction_axes_count:])] input2_axes_shape = [np.prod(input2_axes_shape[:reduction_axes_count])] + \ input2_axes_shape[reduction_axes_count:] reduction_axes_count = 1 # check if other axes need to be flatten to force 2D dot if reduction_axes_count == 1: if len(op.x_out_axes) > 1: reshape_input_needed = True reshape_output_needed = True input1_axes_shape = [np.prod(input1_axes_shape[:-1]) ] + input1_axes_shape[-1:] if len(op.y_out_axes) > 1: reshape_input_needed = True reshape_output_needed = True input2_axes_shape = input2_axes_shape[:1] + [ np.prod(input2_axes_shape[1:]) ] # reshape input if reshape_input_needed: input1_op = PyngReshape(self.computation.lookup_cpp_op(input1), AxisVector(input1_axes_order), Shape(input1_axes_shape)) input2_op = PyngReshape(self.computation.lookup_cpp_op(input2), AxisVector(input2_axes_order), Shape(input2_axes_shape)) else: input1_op = self.computation.lookup_cpp_op(input1) input2_op = self.computation.lookup_cpp_op(input2) ngraph_op = PyngDot(input1_op, input2_op, reduction_axes_count) # reshape output if reshape_output_needed: ngraph_op = PyngReshape( ngraph_op, AxisVector(list(range(0, 4 - 2 * reduction_axes_count))), Shape( list(op.x_out_axes.lengths) + list(op.y_out_axes.lengths))) # reshape output self.computation.register_cpp_op(op, ngraph_op)
def transpose(op, order): # type: (Node, List[int]) -> Node """Transpose data via reshape.""" v = [] for i in range(len(order)): v.append(op.get_shape()[order[i]]) new_shape = v return Reshape(op, AxisVector(order), Shape(new_shape)) def relu(op): # type: (Node) -> Node """Relu operator.""" return Maximum(op, make_float32_constant_like(0., op)) # Flatten X1 = Reshape(Input, AxisVector([0, 1, 2]), Shape([bz, 784])) # Normalize X2 = X1 / make_float32_constant_like(255., X1) # Affine 1 W1 = Parameter(float_element_type, Shape([784, 100])) b1 = Parameter(float_element_type, Shape([100])) X3 = Dot(X2, W1) + Broadcast(b1, Shape([bz, 100]), AxisSet({0})) X4 = relu(X3) # Affine 2 W2 = Parameter(float_element_type, Shape([100, 10])) b2 = Parameter(float_element_type, Shape([10])) X5 = Dot(X4, W2) + Broadcast(b2, Shape([bz, 10]), AxisSet({0}))