def test_update_function(self): optimizer = dragon.optimizers.SGD() try: _ = optimizer.op_type except KeyError: pass value = dragon.Tensor((), dtype='float32').set_value(1.) grad = dragon.Tensor((), dtype='float32').set_value(1.) optimizer.apply_gradients([(value, grad)]) dragon.create_function(optimizer=optimizer)()
def _plan_params(self): if self.mode == 'lstm': gate_size = 4 * self.hidden_size elif self.mode == 'gru': gate_size = 3 * self.hidden_size else: gate_size = self.hidden_size # 1. plan weights self._matrix_weights = [] self._bias_weights = [] for layer in range(self.num_layers): for direction in range(self.num_directions): layer_input_size = self.input_size if layer == 0 \ else self.hidden_size * self.num_directions w_names = [ 'layer_{}/{}/{}'.format(layer, p, 'L' if direction == 0 else 'R') for p in ('matrix_ih', 'matrix_hh', 'bias_ih', 'bias_hh') ] w_ih = dg.Tensor(name=w_names[0], shape=[gate_size, layer_input_size]) w_hh = dg.Tensor(name=w_names[1], shape=[gate_size, self.hidden_size]) b_ih = dg.Tensor(name=w_names[2], shape=[ gate_size, ]) b_hh = dg.Tensor(name=w_names[3], shape=[ gate_size, ]) # W (0 ~ 3), R (4 ~ 7) self._matrix_weights.extend([w_ih, w_hh]) # Bw (0 ~ 3), Br (4 ~ 7) self._bias_weights.extend([b_ih, b_hh]) # 2. compute total number of parameters self._weights_count = 0 for w in self._matrix_weights + self._bias_weights: self._weights_count += np.prod(w.shape) # 3. register the packed weights self.weights = Parameter(Tensor(int(self._weights_count))) # 4. create the initialization grids if self.mode == 'lstm': num_params_per_layer = 8 elif self.mode == 'gru': num_params_per_layer = 6 else: num_params_per_layer = 2 self._matrix_init_grids = [[[ 'orthogonal' for _ in range(num_params_per_layer) ] for _ in range(self.num_directions)] for _ in range(self.num_layers)] self._bias_init_grids = [[[ 'zero' for _ in range(num_params_per_layer) ] for _ in range(self.num_directions)] for _ in range(self.num_layers)] # 5. set the init flag self._init_params = False
def test_dot(self): with dragon.graph_mode(): self.assertEqual( dragon.math.dot([self.sym1, self.sym1]).shape, None) self.assertEqual(dragon.math.dot([self.sym2, self.sym2]).shape, ()) self.assertEqual( dragon.math.dot([dragon.Tensor(()), dragon.Tensor(())]).shape, ()) self.assertEqual( dragon.math.dot([self.sym3, self.sym3]).shape, (self.sym3.shape[0], self.sym3.shape[1])) self.assertEqual( dragon.math.dot([self.sym3, self.sym2]).shape, self.sym3.shape[:-1])
class TestFunction(unittest.TestCase): """Test the graph function.""" @dragon.function(input_signature=[ dragon.Tensor((1, ), dtype='int32'), dragon.Tensor((1, ), dtype='int32'), dragon.Tensor((1, ), dtype='int32'), ]) def func1(self, a, b, c=0, **kwargs): _ = kwargs return a + b + c def test_def_function(self): @dragon.function(input_signature=[dragon.Tensor(None, symbolic=True)]) def func2(a, b): return a + b self.assertEqual(self.func1([1, 2], [3, 4]).numpy().tolist(), [4, 6]) self.assertEqual(self.func1([1, 2], b=[3, 4]).numpy().tolist(), [4, 6]) self.assertEqual( self.func1([1, 2], b=[3, 4], c=1).numpy().tolist(), [5, 7]) self.assertEqual( self.func1([1, 2], b=[3, 4], c=1).numpy().tolist(), [5, 7]) self.assertEqual( self.func1([1, 2], [3, 4], executing_stage='forward').numpy().tolist(), [4, 6]) dragon.function(func=lambda: dragon.optimizers.SGD())() try: self.func1(1, 2, 3, 4) except ValueError: pass try: func2(1, 2) except ValueError: pass def test_update_function(self): optimizer = dragon.optimizers.SGD(lr=1, momentum=0) try: _ = optimizer.op_type except KeyError: pass var = dragon.constant(1, dtype='float32') grad = dragon.constant(1, dtype='float32') with dragon.eager_mode(): optimizer.apply_gradients([(grad, var)]) with dragon.graph_mode(): optimizer.apply_gradients([(grad, var)]).run() self.assertEqual(float(var), -1.)
def test_def_function(self): @dragon.function(input_signature=[dragon.Tensor(None)]) def func2(a, b): return a + b self.assertEqual( self.func1([1, 2], [3, 4]).get_value().tolist(), [4, 6]) self.assertEqual( self.func1([1, 2], b=[3, 4]).get_value().tolist(), [4, 6]) self.assertEqual( self.func1([1, 2], b=[3, 4], c=1).get_value().tolist(), [5, 7]) self.assertEqual( self.func1([1, 2], b=[3, 4], c=1).get_value().tolist(), [5, 7]) self.assertEqual( self.func1([1, 2], [3, 4], executing_stage='forward').get_value().tolist(), [4, 6]) dragon.function(func=lambda: dragon.optimizers.SGD())() try: self.func1(1, 2, 3, 4) except ValueError: pass try: func2(1, 2) except ValueError: pass
def test_conv_transpose(self): w = dragon.Tensor((3, 3, 3, 3)) with dragon.graph_mode(): self.assertEqual( dragon.nn.conv2d_transpose([self.sym1, self.sym1]).shape, None) self.assertEqual( dragon.nn.conv2d_transpose([self.sym3, self.sym1]).shape, None) self.assertEqual( dragon.nn.conv2d_transpose([self.sym3, w]).shape, (self.sym3.shape[0], w.shape[0], None, None)) self.assertEqual( dragon.nn.conv2d_transpose([w, w], output_padding=self.shape1).shape, (w.shape[0], w.shape[0], None, None)) self.assertEqual( dragon.nn.conv2d_transpose([w, w], output_padding=self.shape2).shape, (w.shape[0], w.shape[0], None, None)) self.assertEqual( dragon.nn.conv2d_transpose([w, w], output_shape=self.shape1).shape, (w.shape[0], w.shape[0], None, None)) self.assertEqual( dragon.nn.conv2d_transpose([w, w], output_shape=self.shape2).shape, (w.shape[0], w.shape[0], None, None))
def test_conv(self): w = dragon.Tensor((3, 3, 3, 3)) with dragon.graph_mode(): self.assertEqual( dragon.nn.conv2d([self.sym1, self.sym1]).shape, None) self.assertEqual( dragon.nn.conv2d([self.sym4, w]).shape, (self.sym4.shape[0], w.shape[0], None, None)) self.assertEqual( dragon.nn.conv2d([w, w], kernel_shape=1, out_channels=w.shape[0]).shape, w.shape) self.assertEqual( dragon.nn.conv2d([w, w], kernel_shape=1, padding='SAME').shape, w.shape) self.assertEqual( dragon.nn.conv2d_transpose([self.sym4, w], out_channels=w.shape[1]).shape, (self.sym4.shape[0], w.shape[1], None, None)) self.assertEqual( dragon.nn.conv2d_transpose([w, w], output_padding=(2, 2), kernel_shape=1).shape, (w.shape[0], w.shape[1], w.shape[2] + 2, w.shape[3] + 2)) self.assertEqual( dragon.nn.conv2d_transpose([w, w], output_shape=(4, 4), output_padding=(2, 2), kernel_shape=1).shape, (w.shape[0], w.shape[1], 6, 6))
def test_merge_form(self): w1, w2 = dragon.Workspace(), dragon.Workspace() with w1.as_default(): x = dragon.Tensor((), name='test_merge_from/x').set_value(0) w2.merge_from(w1) with w2.as_default(): self.assertEqual(int(x), 0)
def test_clear(self): w = dragon.Workspace() with w.as_default(): x = dragon.Tensor((1,)) self.assertEqual(x.size, 1) w.clear() self.assertEqual(x.size, 0)
def test_pool(self): func = functools.partial(dragon.nn.pool2d, kernel_shape=3, strides=1, pads=1) with dragon.graph_mode(): self.assertEqual(func(self.sym1).shape, None) self.assertEqual(func(self.sym3).shape, (1, None)) self.assertEqual(func(self.sym4).shape, (1, None, None, None)) self.assertEqual( func(self.sym4, global_pool=True).shape, (1, None, 1, 1)) self.assertEqual( func(dragon.Tensor((1, 3, 4, 4))).shape, (1, 3, 4, 4)) self.assertEqual( func(dragon.Tensor((1, 3, 4, 4)), padding='SAME').shape, (1, 3, 4, 4))
def test_gemm(self): w = dragon.Tensor((3, 2)) with dragon.graph_mode(): self.assertEqual(dragon.math.gemm([self.sym1, w]).shape, None) self.assertEqual( dragon.math.gemm([self.sym1, w], axis=1).shape, (None, 2)) self.assertEqual( dragon.math.gemm([self.sym1, self.sym1]).shape, None)
def test_feed_tensor(self): w = dragon.Workspace() with w.as_default(): v1, v2 = dragon.EagerTensor(1), np.array(2) x = dragon.Tensor((), name='test_feed_tensor/x') w.feed_tensor(x, v1) self.assertEqual(int(x), 1) w.feed_tensor(x, v2) self.assertEqual(int(x), 2)
def test_create_function(self): a = dragon.Tensor((), dtype='int32').set_value(1) b = dragon.Tensor((), dtype='int32').set_value(2) y = a + 1 try: dragon.create_function(outputs=y, optimizer=dragon.optimizers.SGD()) except ValueError: pass try: dragon.create_function(outputs=dragon.EagerTensor(1)) except ValueError: pass try: f = dragon.create_function(outputs=y, givens={a: 1}) except ValueError: f = dragon.create_function(outputs=y, givens={a: b}) self.assertEqual(int(f()), 3)
def test_roi_pool(self): rois = dragon.Tensor((2, 5)) func = functools.partial(dragon.vision.roi_pool, pooled_h=7, pooled_w=7) with dragon.graph_mode(): self.assertEqual(func([self.sym1, rois]).shape, None) self.assertEqual(func([self.sym4, rois]).shape, (2, None, 7, 7)) self.assertEqual( func([self.sym4, self.sym1]).shape, (None, None, 7, 7))
def test_gemm(self): w = dragon.Tensor((3, 2), symbolic=True) with dragon.graph_mode(): self.assertEqual(dragon.math.gemm([self.sym1, w]).shape, None) self.assertEqual( dragon.math.gemm([self.sym3, w], transpose_a=True).shape, (None, 2)) self.assertEqual( dragon.math.gemm([self.sym1, self.sym1]).shape, None) self.assertEqual( dragon.math.gemm([w, self.sym1], transpose_b=True).shape, None)
def WrapScalar(scalar, dtype, ctx): # We use (DType + Value) to hash different scalars # Setting a Tensor with same DType and shape will not deconstruct it value = np.array([scalar], dtype=dtype) if 'float' in dtype: scalar = float(scalar) if 'int' in dtype: scalar = int(scalar) t = dg.Tensor('/share/scalar/{}/{}'.format(dtype, str(scalar))).Variable() t.set_value(value) t = Tensor(dg_tensor=t.name, dtype=dtype, ctx=ctx, own_storage=False) t.requires_grad = False return t
def test_depth_to_space(self): func1 = functools.partial(dragon.nn.depth_to_space, block_size=1) func2 = functools.partial(dragon.nn.space_to_depth, block_size=1) with dragon.graph_mode(): for func in (func1, func2): self.assertEqual(func(self.sym1).shape, None) self.assertEqual(func(self.sym2).shape, None) self.assertEqual( func(self.sym4, data_format='NCHW').shape, (self.sym4.shape[0], ) + (None, ) * (len(self.sym4.shape) - 1)) self.assertEqual( func(self.sym4, data_format='NCHW').shape, (self.sym4.shape[0], ) + (None, ) * (len(self.sym4.shape) - 1)) self.assertEqual( func(dragon.Tensor((1, 2, 3)), data_format='NCHW').shape, dragon.Tensor((1, 2, 3)).shape) self.assertEqual( func(dragon.Tensor((1, 2, 3)), data_format='NHWC').shape, dragon.Tensor((1, 2, 3)).shape)
def test_moments(self): with dragon.graph_mode(): self.assertEqual(dragon.math.moments(self.sym1)[0].shape, ()) self.assertEqual( dragon.math.moments(self.sym1, axis=0)[0].shape, None) self.assertEqual( dragon.math.moments(self.sym1, keepdims=True)[0].shape, (1, )) self.assertEqual(dragon.math.moments(self.sym2)[0].shape, ()) self.assertEqual( dragon.math.moments(self.sym2, axis=0)[0].shape, ()) self.assertEqual( dragon.math.moments(self.sym2, axis=1)[0].shape, (1, )) self.assertEqual( dragon.math.moments(self.sym2, axis=0, keepdims=True)[0].shape, (1, )) self.assertEqual( dragon.math.moments(dragon.Tensor(None, 'float64'))[0].dtype, 'float64') self.assertEqual( dragon.math.moments(dragon.Tensor(None, 'int64'))[0].dtype, 'float64')
def dragon(self): """Create a dragon tensor sharing this tensor. Returns ------- Tensor The dragon tensor. """ if isinstance(self._dg_tensor, str): return dg.Tensor(_name=self._dg_tensor) else: return self._dg_tensor
def NetInit(self, proto_txt, phase='TRAIN'): """Construct a Net by the ``proto_txt`` file. Parameters ---------- proto_txt : str The path of ``proto_txt`` file. phase : str The phase, ``TRAIN`` or ``TEST``. Returns ------- Net The net. References ---------- The implementation of `Net_Init(_caffe.cpp, L109)`_. """ self._net = pb.NetParameter() parse_text_proto(open(proto_txt, 'r').read(), self._net) self._phase = phase self._layers = [] self._inputs_to_tensors = {} if not hasattr(self, '_blobs'): self._blobs = {} self._losses, self._trainable_vars = [], [] if len(self._net.input) > 0: for input in self._net.input: if not input in self._blobs: variable = dragon.Tensor(input).Variable() self._blobs[input] = { 'data': variable, 'diff': dragon.Tensor.Ref(variable.name + '_grad'), } self._inputs_to_tensors[input] = self._blobs[input]['data'] for layer in self._net.layer: if not self.FilterLayer(layer): continue self._layers.append( getattr(layer_factory, layer.type + 'Layer')(layer)) self.Setup() for layer in self._net.layer: if not self.FilterLayer(layer): continue self.CheckBackward(layer)
def _set_param(self, layer_id, param_id, param_type, param): if not isinstance(param, Tensor): if isinstance(param, np.ndarray): paramT = dg.Tensor('/tmp/rnn_param').Variable() paramT.set_value(param) param = paramT else: raise ValueError('Excepted a tensor or numpy array.') W = self.weights.dragon() outputs = RNNParamSet([W, param], layer_id, param_id, param_type, rnn_mode=self.mode, input_size=self.input_size, hidden_size=self.hidden_size, num_layers=self.num_layers, num_directions=self.num_directions) for k, v in outputs.expressions.items(): dg.workspace.RunOperator(v)
def shared(value, name=None, **kwargs): """Construct a Tensor initialized with ``value``. Parameters ---------- value : number, list or numpy.ndarray The numerical values. name : str The name of tensor. Returns ------- Tensor The initialized tensor. """ if not isinstance(value, (int, float, list, np.ndarray)): raise TypeError("Unsupported type of value: {}".format(type(value))) tensor = dg.Tensor(name).Variable() dg.workspace.FeedTensor(tensor, value) return tensor
def native_run_graph(graph_def, inputs, initializer, init_func=None): # De-Optimization for i in range(len(graph_def.arg)): if graph_def.arg[i].name == 'optimization_level': graph_def.arg[i].i = 0 # Create an anonymous workspace ws = Workspace() with dg.ws_scope(ws.name): # Register all the initializer before feeding them for name in initializer: dg.Tensor(name=name).Variable() # Feed the given values if necessary if init_func: init_func() # Feed the external inputs for name, blob in inputs.items(): dg.workspace.FeedTensor(name, blob) # Create and Run the graph graph_name = dg.workspace.CreateGraph(graph_def) dg.workspace.RunGraph(graph_name, return_outputs=False) # Fetch the outputs output_names = graph_def.output output_values = [dg.workspace.FetchTensor(name) for name in output_names] # Fetch the initializer initializer = [ numpy_helper.from_array( dg.workspace.FetchTensor(name), name=name) for name in initializer ] # Return the outputs return ws, namedtupledict('Outputs', output_names)(*output_values), initializer
class TestOpSpecWithTensorDesc(unittest.TestCase): """Test the op spec with tensor descriptors.""" sym1 = dragon.Tensor(None) sym2 = dragon.Tensor((1, None)) sym3 = dragon.Tensor((1, None, None, None)) shape1 = dragon.shape(sym1) shape2 = [1, shape1, 1] def test_broadcast_to(self): with dragon.graph_mode(): self.assertEqual( dragon.broadcast_to(self.sym1, shape=self.shape1).shape, None) self.assertEqual( dragon.broadcast_to(self.sym2, shape=self.shape1).shape, (None, ) * len(self.sym2.shape)) self.assertEqual( dragon.broadcast_to(self.sym2, shape=self.shape2).shape, (None, ) * len(self.shape2)) def test_channel_normalize(self): func = functools.partial(dragon.channel_normalize, mean=(1., 1., 1.), std=(1., 1., 1.)) with dragon.graph_mode(): self.assertEqual(func(self.sym1).shape, None) self.assertEqual(func(self.sym1, perm=self.shape1).shape, None) self.assertEqual(func(self.sym2).shape, self.sym2.shape) self.assertEqual( func(self.sym2, perm=self.shape1).shape, (None, ) * len(self.sym2.shape)) self.assertEqual( func(self.sym2, perm=self.shape2).shape, (None, ) * len(self.sym2.shape)) def test_conv_transpose(self): w = dragon.Tensor((3, 3, 3, 3)) with dragon.graph_mode(): self.assertEqual( dragon.nn.conv2d_transpose([self.sym1, self.sym1]).shape, None) self.assertEqual( dragon.nn.conv2d_transpose([self.sym3, self.sym1]).shape, None) self.assertEqual( dragon.nn.conv2d_transpose([self.sym3, w]).shape, (self.sym3.shape[0], w.shape[0], None, None)) self.assertEqual( dragon.nn.conv2d_transpose([w, w], output_padding=self.shape1).shape, (w.shape[0], w.shape[0], None, None)) self.assertEqual( dragon.nn.conv2d_transpose([w, w], output_padding=self.shape2).shape, (w.shape[0], w.shape[0], None, None)) self.assertEqual( dragon.nn.conv2d_transpose([w, w], output_shape=self.shape1).shape, (w.shape[0], w.shape[0], None, None)) self.assertEqual( dragon.nn.conv2d_transpose([w, w], output_shape=self.shape2).shape, (w.shape[0], w.shape[0], None, None)) def test_init_ops(self): init_funcs_v1 = [ dragon.fill, dragon.ones, dragon.random.glorot_normal, dragon.random.glorot_uniform, dragon.random.normal, dragon.random.uniform, dragon.random.truncated_normal, dragon.zeros ] init_funcs_v2 = [ dragon.ones_like, dragon.random.normal_like, dragon.random.uniform_like, dragon.zeros_like ] for func in init_funcs_v1: with dragon.graph_mode(): self.assertEqual(func(shape=self.shape1).shape, None) self.assertEqual( func(shape=self.shape2).shape, (None, ) * len(self.shape2)) for func in init_funcs_v2: with dragon.graph_mode(): self.assertEqual(func(self.sym1).shape, None) self.assertEqual(func(self.sym2).shape, self.sym2.shape) def test_permutation(self): with dragon.graph_mode(): self.assertEqual( dragon.random.permutation(self.sym1).shape, (None, )) def test_repeat(self): with dragon.graph_mode(): self.assertEqual( dragon.repeat(self.sym1, repeats=self.shape1).shape, None) self.assertEqual( dragon.repeat(self.sym2, repeats=self.shape1).shape, None) def test_reshape(self): with dragon.graph_mode(): self.assertEqual( dragon.reshape(self.sym1, shape=self.shape1).shape, None) self.assertEqual( dragon.reshape(self.sym2, shape=self.shape1).shape, None) self.assertEqual( dragon.reshape(self.sym2, shape=self.shape2).shape, (None, ) * len(self.shape2)) def test_resize(self): with dragon.graph_mode(): self.assertEqual( dragon.vision.resize(self.sym1, sizes=self.shape1).shape, None) self.assertEqual( dragon.vision.resize(self.sym1, scales=self.shape1).shape, None) self.assertEqual( dragon.vision.resize(self.sym2, sizes=self.shape1).shape, (None, ) * len(self.sym2.shape)) self.assertEqual( dragon.vision.resize(self.sym2, scales=self.shape1).shape, (None, ) * len(self.sym2.shape)) self.assertEqual( dragon.vision.resize(self.sym2, sizes=self.shape2).shape, (None, ) * len(self.sym2.shape)) self.assertEqual( dragon.vision.resize(self.sym2, scales=self.shape2).shape, (None, ) * len(self.sym2.shape)) def test_slice(self): with dragon.graph_mode(): self.assertEqual( dragon.slice(self.sym1, starts=self.shape1, sizes=self.shape1).shape, None) self.assertEqual( dragon.slice(self.sym2, starts=self.shape1, sizes=self.shape1).shape, None) self.assertEqual( dragon.slice(self.sym2, starts=self.shape2, sizes=self.shape2).shape, None) def test_tile(self): with dragon.graph_mode(): self.assertEqual( dragon.tile(self.sym1, repeats=self.shape1).shape, None) self.assertEqual( dragon.tile(self.sym2, repeats=self.shape1).shape, (None, ) * len(self.sym2.shape)) self.assertEqual( dragon.tile(self.sym2, repeats=self.shape2).shape, (None, ) * len(self.sym2.shape)) def test_transpose(self): with dragon.graph_mode(): self.assertEqual(dragon.transpose(self.sym1).shape, None) self.assertEqual( dragon.transpose(self.sym1, perm=self.shape1).shape, None) self.assertEqual( dragon.transpose(self.sym2).shape, self.sym2.shape[::-1]) self.assertEqual( dragon.transpose(self.sym2, perm=self.shape1).shape, (None, ) * len(self.sym2.shape)) self.assertEqual( dragon.transpose(self.sym2, perm=self.shape2).shape, (None, ) * len(self.sym2.shape))
None """ x1 = dg.workspace.FetchTensor(inputs[0]) x2 = dg.workspace.FetchTensor(inputs[1]) dy = dg.workspace.FetchTensor(inputs[-1]) dx1 = dy * x2 dx2 = dy * x1 dg.workspace.FeedTensor(outputs[0], dx1) dg.workspace.FeedTensor(outputs[1], dx2) if __name__ == '__main__': # def x1 = dg.Tensor('x1').Variable() x2 = dg.Tensor('x2').Variable() y = dg.ops.Template([x1, x2], module=__name__, op='VecMultOp', nout=1) dx1 = dg.grad(y, x1) dx2 = dg.grad(y, x2) foo = dg.function(outputs=y) # feed dg.workspace.FeedTensor(x1, np.ones((5, 3), dtype=np.float32)) dg.workspace.FeedTensor(x2, np.ones((5, 3), dtype=np.float32) * 5.0) # run foo() # fetch print('y \n-------------- \n', y.get_value(), '\n')
def test_properties(self): a, b = dragon.Tensor(()), dragon.EagerTensor(0) self.assertEqual(dragon.Tensor(()).ndim, 0) self.assertEqual(dragon.Tensor(shape=(2, )).ndim, 1) self.assertEqual(dragon.Tensor(None).shape, None) self.assertEqual(dragon.Tensor(shape=(2, )).shape, (2, )) self.assertEqual(dragon.Tensor(None).size, 0) self.assertEqual(dragon.Tensor(()).size, 1) self.assertEqual(dragon.Tensor(shape=(2, None)).size, math.inf) self.assertEqual(dragon.Tensor(shape=(2, )).size, 2) self.assertEqual(dragon.Tensor(None, None).dtype, None) self.assertEqual(dragon.Tensor(None, dtype='float32').dtype, 'float32') self.assertEqual(dragon.EagerTensor(shape=(2, )).ndim, 1) self.assertEqual(dragon.EagerTensor(shape=(2, )).shape, (2, )) self.assertEqual(dragon.EagerTensor(shape=(2, )).size, 2) self.assertEqual( dragon.EagerTensor(shape=(2, ), dtype='float32').dtype, 'float32') self.assertEqual(dragon.EagerTensor().device, dragon.EagerTensor().device) self.assertNotEqual(a.__hash__(), b.__hash__()) self.assertNotEqual(a.__repr__(), b.__repr__()) self.assertNotEqual(b.__repr__(), dragon.EagerTensor((2, )).__repr__()) self.assertEqual(int(a.constant().set_value(1)), 1) self.assertEqual(float(dragon.Tensor.from_value(1)), 1.) self.assertEqual(float(dragon.EagerTensor.from_value(1)), 1.) self.assertEqual(int(b.set_value(1)), 1) self.assertEqual(float(b), 1.) self.assertEqual(int(b.get_value()), 1) try: a.shape = 1 except TypeError: pass try: b.shape = (2, 3) except RuntimeError: pass try: b.dtype = 'float64' except RuntimeError: pass try: b = dragon.EagerTensor(0, 0) except ValueError: pass with dragon.name_scope('a'): a.name = 'a' self.assertEqual(a.name, 'a/a') with dragon.name_scope(''): b.name = 'b' self.assertEqual(b.name, 'b') b.requires_grad = True self.assertEqual(b.requires_grad, True)
def test_properties(self): a, b = dragon.Tensor(()), dragon.Tensor(()) self.assertEqual(dragon.Tensor(()).ndim, 0) self.assertEqual(dragon.Tensor(()).size, 1) self.assertEqual(dragon.Tensor(shape=(2,)).ndim, 1) self.assertEqual(dragon.Tensor(shape=(2,)).shape, (2,)) self.assertEqual(dragon.Tensor(shape=(2,)).size, 2) self.assertEqual(dragon.Tensor(shape=(2,), dtype='float32').dtype, 'float32') self.assertEqual(dragon.Tensor(None, symbolic=True).size, 0) self.assertEqual(dragon.Tensor((), symbolic=True).size, 1) self.assertEqual(dragon.Tensor(None, symbolic=True).shape, None) self.assertEqual(dragon.Tensor(shape=(2, None), symbolic=True).size, math.inf) self.assertEqual(dragon.Tensor(None, dtype='float32', symbolic=True).dtype, 'float32') self.assertEqual(dragon.Tensor(None, None, symbolic=True).dtype, None) self.assertNotEqual(a.__hash__(), b.__hash__()) self.assertEqual(a.__repr__(), b.__repr__()) self.assertNotEqual(a.__repr__(), dragon.Tensor((), symbolic=True).__repr__()) self.assertEqual(float(int(a)), float(b)) self.assertEqual(dragon.constant([2]).item(), 2) self.assertEqual(dragon.constant([2, 3]).tolist(), [2, 3]) try: _ = dragon.Tensor(None) except ValueError: pass b.requires_grad = True self.assertEqual(b.requires_grad, True)
class TestOpSpec(unittest.TestCase): """Test the op spec.""" sym1 = dragon.Tensor(None, None) sym2 = dragon.Tensor((1, )) sym3 = dragon.Tensor((1, None)) sym4 = dragon.Tensor((1, None, None, None)) sym5 = dragon.Tensor((1, None, None, None, None)) def test_accuracy(self): with dragon.graph_mode(): self.assertEqual( dragon.metrics.accuracy([self.sym1, self.sym1]).shape, ()) def test_arg_reduce(self): with dragon.graph_mode(): self.assertEqual( dragon.math.argmax(self.sym1, axis=0, keepdims=True).shape, None) self.assertEqual( dragon.math.argmax(self.sym1, axis=0, keepdims=False).shape, None) self.assertEqual( dragon.math.argmax(self.sym1, axis=None, keepdims=True).shape, (1, )) self.assertEqual( dragon.math.argmax(self.sym1, axis=None, keepdims=False).shape, ()) self.assertEqual( dragon.math.argmax(self.sym2, axis=0, keepdims=True).shape, (1, )) self.assertEqual( dragon.math.argmax(self.sym2, axis=0, keepdims=False).shape, ()) def test_binary_ops(self): with dragon.graph_mode(): self.assertEqual( dragon.math.add([self.sym1, self.sym1]).shape, None) self.assertEqual( dragon.math.add([self.sym2, self.sym2]).shape, (1, )) self.assertEqual( dragon.math.add([self.sym2, self.sym3]).shape, (1, None)) self.assertEqual( dragon.math.add([self.sym3, self.sym2]).shape, (1, None)) self.assertEqual( dragon.math.equal([self.sym1, self.sym1]).shape, None) def test_broadcast(self): with dragon.graph_mode(): self.assertEqual( dragon.broadcast_to(self.sym1, shape=(1, )).shape, None) self.assertEqual( dragon.broadcast_to(self.sym2, shape=(1, 2)).shape, (1, 2)) self.assertEqual( dragon.broadcast_to(self.sym3, shape=(2, )).shape, self.sym3.shape[:-1] + (2, )) self.assertEqual( dragon.broadcast_to(self.sym3, shape=(-1, 2, 2)).shape, (1, 2, 2)) def test_cast(self): with dragon.graph_mode(): self.assertEqual(dragon.cast(self.sym1, 'float32').shape, None) def test_concat(self): with dragon.graph_mode(): self.assertEqual(dragon.concat([self.sym1, self.sym1]).shape, None) self.assertEqual( dragon.concat([self.sym1, self.sym2]).shape, (None, )) self.assertEqual( dragon.concat([self.sym2, self.sym3], axis=0).shape, (2, )) self.assertEqual( dragon.concat([self.sym2, self.sym3], axis=1).shape, None) def test_conv(self): w = dragon.Tensor((3, 3, 3, 3)) with dragon.graph_mode(): self.assertEqual( dragon.nn.conv2d([self.sym1, self.sym1]).shape, None) self.assertEqual( dragon.nn.conv2d([self.sym4, w]).shape, (self.sym4.shape[0], w.shape[0], None, None)) self.assertEqual( dragon.nn.conv2d([w, w], kernel_shape=1, out_channels=w.shape[0]).shape, w.shape) self.assertEqual( dragon.nn.conv2d([w, w], kernel_shape=1, padding='SAME').shape, w.shape) self.assertEqual( dragon.nn.conv2d_transpose([self.sym4, w], out_channels=w.shape[1]).shape, (self.sym4.shape[0], w.shape[1], None, None)) self.assertEqual( dragon.nn.conv2d_transpose([w, w], output_padding=(2, 2), kernel_shape=1).shape, (w.shape[0], w.shape[1], w.shape[2] + 2, w.shape[3] + 2)) self.assertEqual( dragon.nn.conv2d_transpose([w, w], output_shape=(4, 4), output_padding=(2, 2), kernel_shape=1).shape, (w.shape[0], w.shape[1], 6, 6)) def test_depth_to_space(self): func1 = functools.partial(dragon.nn.depth_to_space, block_size=1) func2 = functools.partial(dragon.nn.space_to_depth, block_size=1) with dragon.graph_mode(): for func in (func1, func2): self.assertEqual(func(self.sym1).shape, None) self.assertEqual(func(self.sym2).shape, None) self.assertEqual( func(self.sym4, data_format='NCHW').shape, (self.sym4.shape[0], ) + (None, ) * (len(self.sym4.shape) - 1)) self.assertEqual( func(self.sym4, data_format='NCHW').shape, (self.sym4.shape[0], ) + (None, ) * (len(self.sym4.shape) - 1)) self.assertEqual( func(dragon.Tensor((1, 2, 3)), data_format='NCHW').shape, dragon.Tensor((1, 2, 3)).shape) self.assertEqual( func(dragon.Tensor((1, 2, 3)), data_format='NHWC').shape, dragon.Tensor((1, 2, 3)).shape) def test_dot(self): with dragon.graph_mode(): self.assertEqual( dragon.math.dot([self.sym1, self.sym1]).shape, None) self.assertEqual(dragon.math.dot([self.sym2, self.sym2]).shape, ()) self.assertEqual( dragon.math.dot([dragon.Tensor(()), dragon.Tensor(())]).shape, ()) self.assertEqual( dragon.math.dot([self.sym3, self.sym3]).shape, (self.sym3.shape[0], self.sym3.shape[1])) self.assertEqual( dragon.math.dot([self.sym3, self.sym2]).shape, self.sym3.shape[:-1]) def test_eltwise_loss(self): with dragon.graph_mode(): self.assertEqual( dragon.losses.l2_loss([self.sym1, self.sym1]).shape, ()) self.assertEqual( dragon.losses.l2_loss([self.sym1, self.sym1], reduction='none').shape, None) def test_expand_dims(self): with dragon.graph_mode(): self.assertEqual(dragon.expand_dims(self.sym1, axis=1).shape, None) self.assertEqual( dragon.expand_dims(self.sym2, axis=1).shape, (1, 1)) self.assertEqual( dragon.expand_dims(self.sym2, axis=-1).shape, (1, 1)) self.assertEqual( dragon.expand_dims(self.sym3, axis=0).shape, (1, 1, None)) self.assertEqual( dragon.expand_dims(self.sym3, axis=(0, 3)).shape, (1, 1, None, 1)) self.assertEqual( dragon.expand_dims(self.sym3, axis=(0, 3, 5)).shape, (1, 1, None, 1)) def test_init_ops(self): init_funcs_v1 = [ dragon.fill, dragon.ones, dragon.random.glorot_normal, dragon.random.glorot_uniform, dragon.random.normal, dragon.random.uniform, dragon.random.truncated_normal, dragon.zeros ] for func in init_funcs_v1: with dragon.graph_mode(): self.assertEqual(func(shape=self.sym1.shape).shape, None) self.assertEqual( func(shape=self.sym2.shape).shape, self.sym2.shape) def test_flatten(self): with dragon.graph_mode(): self.assertEqual(dragon.flatten(self.sym1, axis=1).shape, None) self.assertEqual( dragon.flatten(self.sym1, keep_axes=2).shape, (None, None)) self.assertEqual( dragon.flatten(self.sym2, keep_axes=2).shape, (1, None)) self.assertEqual( dragon.flatten(self.sym4, keep_axes=2).shape, (1, None)) self.assertEqual( dragon.flatten(self.sym4, axis=1, num_axes=3).shape, (1, None)) self.assertEqual( dragon.flatten(self.sym4, axis=1, num_axes=-1).shape, (1, None)) def test_gemm(self): w = dragon.Tensor((3, 2)) with dragon.graph_mode(): self.assertEqual(dragon.math.gemm([self.sym1, w]).shape, None) self.assertEqual( dragon.math.gemm([self.sym1, w], axis=1).shape, (None, 2)) self.assertEqual( dragon.math.gemm([self.sym1, self.sym1]).shape, None) def test_index_select(self): with dragon.graph_mode(): self.assertEqual( dragon.index_select(self.sym1, self.sym1).shape, None) self.assertEqual( dragon.index_select(self.sym1, self.sym2, axis=-1).shape, None) self.assertEqual( dragon.index_select(self.sym3, self.sym2, axis=1).shape, (1, 1)) def test_linspace(self): with dragon.graph_mode(): self.assertEqual( dragon.linspace(start=1, stop=5, num=3).shape, (3, )) self.assertEqual( dragon.linspace(start=(1, 2), stop=(3, 4), num=3, axis=1).shape, (2, 3)) self.assertEqual( dragon.linspace(start=(1, 2), stop=(3, 4), num=3, axis=0).shape, (3, 2)) def test_mask_select(self): with dragon.graph_mode(): self.assertEqual( dragon.masked_select([self.sym1, self.sym1]).shape, (None, )) def test_matmul(self): with dragon.graph_mode(): self.assertEqual( dragon.math.matmul([self.sym1, self.sym1]).shape, None) self.assertEqual( dragon.math.matmul([self.sym1, self.sym2]).shape, None) self.assertEqual( dragon.math.matmul([self.sym1, self.sym3]).shape, None) self.assertEqual( dragon.math.matmul([self.sym2, self.sym3]).shape, (None, )) self.assertEqual( dragon.math.matmul([self.sym3, self.sym2]).shape, (1, )) self.assertEqual( dragon.math.matmul([self.sym3, self.sym3]).shape, (1, None)) self.assertEqual( dragon.math.matmul([self.sym4, self.sym3]).shape, (1, None, None, None)) self.assertEqual( dragon.math.matmul([self.sym4, self.sym4]).shape, (1, None, None, None)) def test_moments(self): with dragon.graph_mode(): self.assertEqual(dragon.math.moments(self.sym1)[0].shape, ()) self.assertEqual( dragon.math.moments(self.sym1, axis=0)[0].shape, None) self.assertEqual( dragon.math.moments(self.sym1, keepdims=True)[0].shape, (1, )) self.assertEqual(dragon.math.moments(self.sym2)[0].shape, ()) self.assertEqual( dragon.math.moments(self.sym2, axis=0)[0].shape, ()) self.assertEqual( dragon.math.moments(self.sym2, axis=1)[0].shape, (1, )) self.assertEqual( dragon.math.moments(self.sym2, axis=0, keepdims=True)[0].shape, (1, )) self.assertEqual( dragon.math.moments(dragon.Tensor(None, 'float64'))[0].dtype, 'float64') self.assertEqual( dragon.math.moments(dragon.Tensor(None, 'int64'))[0].dtype, 'float64') def test_multinomial(self): with dragon.graph_mode(): self.assertEqual(dragon.random.multinomial(self.sym1).shape, None) self.assertEqual( dragon.random.multinomial(self.sym2, num_samples=2).shape, (2, )) def test_non_zero(self): with dragon.graph_mode(): self.assertEqual(dragon.nonzero(self.sym1).shape, None) self.assertEqual(dragon.nonzero(self.sym2).shape, (None, 1)) def test_one_hot(self): with dragon.graph_mode(): self.assertEqual(dragon.one_hot(self.sym1, depth=2).shape, None) self.assertEqual(dragon.one_hot(self.sym2, depth=2).shape, (1, 2)) def test_pad(self): with dragon.graph_mode(): self.assertEqual(dragon.pad(self.sym1, pads=[(1, 1)]).shape, None) self.assertEqual( dragon.pad(self.sym3, pads=[(1, 1)]).shape, (3, None)) self.assertEqual( dragon.pad(self.sym3, pads=[(1, 1), (1, 1)]).shape, (3, None)) def test_permutation(self): with dragon.graph_mode(): self.assertEqual(dragon.random.permutation(5).shape, (5, )) def test_pool(self): func = functools.partial(dragon.nn.pool2d, kernel_shape=3, strides=1, pads=1) with dragon.graph_mode(): self.assertEqual(func(self.sym1).shape, None) self.assertEqual(func(self.sym3).shape, (1, None)) self.assertEqual(func(self.sym4).shape, (1, None, None, None)) self.assertEqual( func(self.sym4, global_pool=True).shape, (1, None, 1, 1)) self.assertEqual( func(dragon.Tensor((1, 3, 4, 4))).shape, (1, 3, 4, 4)) self.assertEqual( func(dragon.Tensor((1, 3, 4, 4)), padding='SAME').shape, (1, 3, 4, 4)) def test_predicative(self): with dragon.graph_mode(): self.assertEqual( dragon.math.is_inf(self.sym1).shape, self.sym1.shape) self.assertEqual( dragon.math.is_inf(self.sym3).shape, self.sym3.shape) self.assertEqual( dragon.math.is_nan(self.sym1).shape, self.sym1.shape) self.assertEqual( dragon.math.is_nan(self.sym3).shape, self.sym3.shape) def test_range(self): with dragon.graph_mode(): self.assertEqual(dragon.range(3).shape, (3, )) self.assertEqual(dragon.range(3, 4).shape, (1, )) self.assertEqual(dragon.range(3, delta=0).shape, None) def test_reduce(self): with dragon.graph_mode(): self.assertEqual(dragon.math.sum(self.sym1).shape, ()) self.assertEqual(dragon.math.sum(self.sym1, axis=0).shape, None) self.assertEqual( dragon.math.sum(self.sym1, keepdims=True).shape, ()) self.assertEqual(dragon.math.sum(self.sym2, axis=0).shape, ()) self.assertEqual(dragon.math.sum(self.sym2, axis=1).shape, (1, )) self.assertEqual( dragon.math.sum(self.sym2, axis=0, keepdims=True).shape, (1, )) def test_repeat(self): with dragon.graph_mode(): self.assertEqual( dragon.repeat(self.sym1, axis=None, repeats=2).shape, (None, )) self.assertEqual( dragon.repeat(self.sym1, axis=0, repeats=2).shape, None) self.assertEqual( dragon.repeat(self.sym2, axis=None, repeats=2).shape, (2, )) self.assertEqual( dragon.repeat(self.sym3, axis=0, repeats=2).shape, (2, None)) self.assertEqual( dragon.repeat(self.sym3, axis=1, repeats=2).shape, (1, None)) def test_reshape(self): with dragon.graph_mode(): self.assertEqual( dragon.reshape(self.sym2, shape=(0, 1)).shape, (1, 1)) self.assertEqual( dragon.reshape(self.sym3, shape=(0, -1)).shape, (1, None)) self.assertEqual( dragon.reshape(self.sym3, shape=(0, 1, 0)).shape, None) def test_resize(self): with dragon.graph_mode(): self.assertEqual( dragon.vision.resize(self.sym4, sizes=(1, )).shape, (1, None, 1, 1)) self.assertEqual( dragon.vision.resize(self.sym4, sizes=(1, 1)).shape, (1, None, 1, 1)) self.assertEqual( dragon.vision.resize(self.sym4, sizes=(1, 1, 1, 1)).shape, (1, None, 1, 1)) self.assertEqual( dragon.vision.resize(self.sym4, scales=(1, )).shape, (1, None, None, None)) self.assertEqual( dragon.vision.resize(self.sym4, scales=(1, 1)).shape, (1, None, None, None)) self.assertEqual( dragon.vision.resize(self.sym4, scales=(1, 1, 1, 1)).shape, (1, None, None, None)) self.assertEqual( dragon.vision.resize(self.sym5, sizes=(1, 1, 1, 1)).shape, None) def test_roi_pool(self): rois = dragon.Tensor((2, 5)) func = functools.partial(dragon.vision.roi_pool, pooled_h=7, pooled_w=7) with dragon.graph_mode(): self.assertEqual(func([self.sym1, rois]).shape, None) self.assertEqual(func([self.sym4, rois]).shape, (2, None, 7, 7)) self.assertEqual( func([self.sym4, self.sym1]).shape, (None, None, 7, 7)) def test_slice(self): with dragon.graph_mode(): self.assertEqual(dragon.slice(self.sym1, (1, ), (1, )).shape, None) self.assertEqual( dragon.slice(self.sym3, (1, ), (1, )).shape, (1, None)) def test_softmax_loss(self): with dragon.graph_mode(): self.assertEqual( dragon.losses.sparse_softmax_cross_entropy( [self.sym1, self.sym1]).shape, ()) self.assertEqual( dragon.losses.sparse_softmax_cross_entropy( [self.sym1, self.sym1], reduction='none').shape, None) self.assertEqual( dragon.losses.sparse_softmax_cross_entropy( [self.sym3, self.sym1], reduction='none').shape, (self.sym3.shape[0], )) def test_sort(self): with dragon.graph_mode(): self.assertEqual(dragon.sort(self.sym1)[0].shape, None) self.assertEqual(dragon.sort(self.sym2)[0].shape, self.sym2.shape) def test_split(self): with dragon.graph_mode(): self.assertEqual(dragon.split(self.sym1, 2)[0].shape, None) self.assertEqual(dragon.split(self.sym2, 2)[0].shape, (1, )) self.assertEqual(dragon.split(self.sym2, 2, axis=1)[0].shape, None) self.assertEqual(dragon.split(self.sym2, (1, 1))[0].shape, (1, )) self.assertEqual( dragon.split(self.sym2, 2, slice_points=(1, ))[0].shape, (1, )) self.assertEqual( dragon.split(self.sym3, 2, axis=1)[0].shape, (1, None)) self.assertEqual( dragon.split(self.sym3, 2, axis=1, slice_points=(1, ))[1].shape, (1, None)) def test_squeeze(self): with dragon.graph_mode(): self.assertEqual(dragon.squeeze(self.sym1).shape, None) self.assertEqual(dragon.squeeze(self.sym2).shape, ()) self.assertEqual(dragon.squeeze(self.sym2, axis=-1).shape, ()) self.assertEqual(dragon.squeeze(self.sym3).shape, (None, )) def test_stack(self): with dragon.graph_mode(): self.assertEqual(dragon.stack([self.sym1, self.sym1]).shape, None) self.assertEqual( dragon.stack([self.sym3, self.sym2]).shape, (2, 1, None)) self.assertEqual( dragon.stack([self.sym3, self.sym3]).shape, (2, 1, None)) self.assertEqual( dragon.stack([self.sym3, self.sym3], axis=-1).shape, (1, None, 2)) def test_tile(self): with dragon.graph_mode(): self.assertEqual( dragon.tile(self.sym1, repeats=(1, 2)).shape, None) self.assertEqual( dragon.tile(self.sym3, repeats=(1, 2)).shape, (1, None)) def test_topk(self): with dragon.graph_mode(): self.assertEqual(dragon.math.top_k(self.sym1)[0].shape, None) self.assertEqual(dragon.math.top_k(self.sym2, k=2)[0].shape, (2, )) self.assertEqual( dragon.math.top_k(self.sym2, axis=1)[0].shape, None) def test_unchanged(self): with dragon.graph_mode(): self.assertEqual(dragon.math.negative(self.sym1).shape, None) def test_unique(self): with dragon.graph_mode(): self.assertEqual(dragon.unique(self.sym1).shape, (None, )) self.assertEqual( dragon.unique(self.sym1, return_counts=True)[1].shape, (None, )) self.assertEqual( dragon.unique(self.sym1, return_inverse=True)[1].shape, None) self.assertEqual( dragon.unique(self.sym1, return_inverse=True, return_counts=True)[1].shape, None)
def register_argument(self, name): with dg.name_scope(self.persistent_key): return dg.Tensor(name).Variable().name
class TestFunction(unittest.TestCase): """Test the graph function.""" @dragon.function(input_signature=[ dragon.Tensor((1, ), dtype='int32'), dragon.Tensor((1, ), dtype='int32'), dragon.Tensor((1, ), dtype='int32'), ]) def func1(self, a, b, c=0, **kwargs): _ = kwargs return a + b + c def test_create_function(self): a = dragon.Tensor((), dtype='int32').set_value(1) b = dragon.Tensor((), dtype='int32').set_value(2) y = a + 1 try: dragon.create_function(outputs=y, optimizer=dragon.optimizers.SGD()) except ValueError: pass try: dragon.create_function(outputs=dragon.EagerTensor(1)) except ValueError: pass try: f = dragon.create_function(outputs=y, givens={a: 1}) except ValueError: f = dragon.create_function(outputs=y, givens={a: b}) self.assertEqual(int(f()), 3) def test_def_function(self): @dragon.function(input_signature=[dragon.Tensor(None)]) def func2(a, b): return a + b self.assertEqual( self.func1([1, 2], [3, 4]).get_value().tolist(), [4, 6]) self.assertEqual( self.func1([1, 2], b=[3, 4]).get_value().tolist(), [4, 6]) self.assertEqual( self.func1([1, 2], b=[3, 4], c=1).get_value().tolist(), [5, 7]) self.assertEqual( self.func1([1, 2], b=[3, 4], c=1).get_value().tolist(), [5, 7]) self.assertEqual( self.func1([1, 2], [3, 4], executing_stage='forward').get_value().tolist(), [4, 6]) dragon.function(func=lambda: dragon.optimizers.SGD())() try: self.func1(1, 2, 3, 4) except ValueError: pass try: func2(1, 2) except ValueError: pass def test_update_function(self): optimizer = dragon.optimizers.SGD() try: _ = optimizer.op_type except KeyError: pass value = dragon.Tensor((), dtype='float32').set_value(1.) grad = dragon.Tensor((), dtype='float32').set_value(1.) optimizer.apply_gradients([(value, grad)]) dragon.create_function(optimizer=optimizer)()