[GitHub] eric-haibin-lin commented on a change in pull request #11229: [MXNET-379] L1 Normalization
eric-haibin-lin commented on a change in pull request #11229: [MXNET-379] L1 Normalization URL: https://github.com/apache/incubator-mxnet/pull/11229#discussion_r199217354 ## File path: tests/python/unittest/test_operator.py ## @@ -3009,6 +3009,50 @@ def npy_layer_norm(data, gamma, beta, axis=1, eps=1E-5): grad_nodes={'data': req, 'gamma': req, 'beta': req}, numeric_eps=1e-2, rtol=1e-2, atol=1e-2) +@with_seed() +def test_norm(): +def l1norm(input_data, axis=0, keepdims=True): +return np.sum(abs(input_data), axis=axis, keepdims=keepdims) +def l2norm(input_data, axis=0, keepdims=True): +return np.linalg.norm(input_data, axis=axis, keepdims=keepdims) + +ctx = default_context() +data = mx.symbol.Variable('data') +in_data_dim = random_sample([4,5,6], 1)[0] +in_shape = rand_shape_nd(in_data_dim) +epsilon = 1e-3 +for order in [1, 2]: +for dtype in [np.float16, np.float32, np.float64]: +in_data = np.random.uniform(-1, 1, in_shape).astype(dtype) +in_data[abs(in_data) < epsilon] = epsilon +for i in range(in_data_dim): +norm_sym = mx.symbol.norm(data=data, ord=order, axis=i, keepdims=True) +npy_out = l1norm(in_data, i) if order==1 else l2norm(in_data, i) +npy_out_backward = np.sign(in_data) if order==1 else in_data/npy_out +check_symbolic_forward(norm_sym, [in_data], [npy_out], +rtol=1e-2 if dtype is np.float16 else 1e-5, +atol=1e-2 if dtype is np.float16 else 1e-5, ctx=ctx) +check_symbolic_backward(norm_sym, [in_data], [np.ones(npy_out.shape)], +[npy_out_backward], +rtol=1e-2 if dtype is np.float16 else 1e-5, +atol=1e-2 if dtype is np.float16 else 1e-5, ctx=ctx) +# check gradient +check_numeric_gradient(norm_sym, [in_data], numeric_eps=epsilon, rtol=1e-2, atol=1e-3) Review comment: rtol and atol at line 3040 and 3053 seems high. Is this intended? This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services
[GitHub] eric-haibin-lin commented on a change in pull request #11229: [MXNET-379] L1 Normalization
eric-haibin-lin commented on a change in pull request #11229: [MXNET-379] L1 Normalization URL: https://github.com/apache/incubator-mxnet/pull/11229#discussion_r199026685 ## File path: src/operator/tensor/broadcast_reduce_op.h ## @@ -880,27 +880,24 @@ inline bool L2NormStorageType(const nnvm::NodeAttrs& attrs, int& out_stype = out_attrs->at(0); const NormParam& param = nnvm::get(attrs.parsed); bool dispatched = false; - // l2 norm on a particular axis only supports cpu - const bool invalid_ctx = dev_mask != mshadow::cpu::kDevMask; - const auto dispatch_ex = + if (param.ord == 2) { +// l2 norm on a particular axis only supports cpu +const bool invalid_ctx = dev_mask != mshadow::cpu::kDevMask; +const auto dispatch_ex = invalid_ctx ? DispatchMode::kFComputeFallback : DispatchMode::kFComputeEx; - if (!dispatched && in_stype == kDefaultStorage) { -// dns -> dns -dispatched = storage_type_assign(_stype, kDefaultStorage, dispatch_mode, - DispatchMode::kFCompute); - } - const TShape axis = param.axis.has_value() ? param.axis.value() : TShape(); - if (!dispatched && (in_stype == kRowSparseStorage || in_stype == kCSRStorage) && - axis.ndim() == 0 && param.ord == 2) { -// l2 norm: rsp/csr, axis = () -> dns -dispatched = storage_type_assign(_stype, kDefaultStorage, dispatch_mode, - DispatchMode::kFComputeEx); - } - if (!dispatched && in_stype == kCSRStorage && axis.ndim() == 1 && !param.keepdims && - (axis[0] == 0 || axis[0] == 1) && param.ord == 2) { -// l2 norm: csr, axis = 0/1 -> dns -dispatched = storage_type_assign(_stype, kDefaultStorage, dispatch_mode, - dispatch_ex); +const TShape axis = param.axis.has_value() ? param.axis.value() : TShape(); +if (!dispatched && (in_stype == kRowSparseStorage || in_stype == kCSRStorage) && +axis.ndim() == 0 && param.ord == 2) { + // l2 norm: rsp/csr, axis = () -> dns + dispatched = storage_type_assign(_stype, kDefaultStorage, dispatch_mode, + DispatchMode::kFComputeEx); +} +if (!dispatched && in_stype == kCSRStorage && axis.ndim() == 1 && !param.keepdims && +(axis[0] == 0 || axis[0] == 1) && param.ord == 2) { + // l2 norm: csr, axis = 0/1 -> dns + dispatched = storage_type_assign(_stype, kDefaultStorage, dispatch_mode, + dispatch_ex); +} } if (!dispatched) { dispatched = dispatch_fallback(out_attrs, dispatch_mode); Review comment: Do not dispatch to fallback if all your inputs are dense. This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services
[GitHub] eric-haibin-lin commented on a change in pull request #11229: [MXNET-379] L1 Normalization
eric-haibin-lin commented on a change in pull request #11229: [MXNET-379] L1 Normalization URL: https://github.com/apache/incubator-mxnet/pull/11229#discussion_r195276646 ## File path: tests/python/unittest/test_operator.py ## @@ -2879,6 +2879,32 @@ def npy_layer_norm(data, gamma, beta, axis=1, eps=1E-5): grad_nodes={'data': req, 'gamma': req, 'beta': req}, numeric_eps=1e-2, rtol=1e-2, atol=1e-2) +@with_seed() +def test_l1_norm(): +ctx = default_context() +data = mx.symbol.Variable('data') +in_data_dim = random_sample([4,5,6], 1)[0] +in_shape = rand_shape_nd(in_data_dim) +for dtype in [np.float16, np.float32, np.float64]: +in_data = np.random.uniform(-1, 1, in_shape).astype(dtype) +for i in range(in_data_dim): +for keep_dims in [True, False]: +norm_sym = mx.symbol.norm(data=data, ord=1, axis=i, keepdims=keep_dims) +npy_out = np.sum(abs(in_data), axis=i, keepdims=keep_dims) +check_symbolic_forward(norm_sym, [in_data], [npy_out], + rtol=1e-2 if dtype is np.float16 else 1e-5, + atol=1e-5, ctx=ctx) +# check gradient +#check_numeric_gradient(norm_sym, [in_data], numeric_eps=1e-3, rtol=1e-2, atol=1e-3) +if i < in_data_dim-1: +norm_sym = mx.symbol.norm(data=data, ord=1, axis=(i, i+1), keepdims=keep_dims) +npy_out = np.sum(abs(in_data), axis=(i, i+1), keepdims=keep_dims) +check_symbolic_forward(norm_sym, [in_data], [npy_out], + rtol=1e-2 if dtype is np.float16 else 1e-5, + atol=1e-5, ctx=ctx) +# check gradient +#check_numeric_gradient(norm_sym, [in_data], numeric_eps=1e-3, rtol=1e-2, atol=1e-3) Review comment: why comment this out? This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services
[GitHub] eric-haibin-lin commented on a change in pull request #11229: [MXNET-379] L1 Normalization
eric-haibin-lin commented on a change in pull request #11229: [MXNET-379] L1 Normalization URL: https://github.com/apache/incubator-mxnet/pull/11229#discussion_r195276671 ## File path: tests/python/unittest/test_operator.py ## @@ -2879,6 +2879,32 @@ def npy_layer_norm(data, gamma, beta, axis=1, eps=1E-5): grad_nodes={'data': req, 'gamma': req, 'beta': req}, numeric_eps=1e-2, rtol=1e-2, atol=1e-2) +@with_seed() +def test_l1_norm(): +ctx = default_context() +data = mx.symbol.Variable('data') +in_data_dim = random_sample([4,5,6], 1)[0] +in_shape = rand_shape_nd(in_data_dim) +for dtype in [np.float16, np.float32, np.float64]: +in_data = np.random.uniform(-1, 1, in_shape).astype(dtype) +for i in range(in_data_dim): +for keep_dims in [True, False]: +norm_sym = mx.symbol.norm(data=data, ord=1, axis=i, keepdims=keep_dims) +npy_out = np.sum(abs(in_data), axis=i, keepdims=keep_dims) +check_symbolic_forward(norm_sym, [in_data], [npy_out], + rtol=1e-2 if dtype is np.float16 else 1e-5, + atol=1e-5, ctx=ctx) +# check gradient +#check_numeric_gradient(norm_sym, [in_data], numeric_eps=1e-3, rtol=1e-2, atol=1e-3) +if i < in_data_dim-1: +norm_sym = mx.symbol.norm(data=data, ord=1, axis=(i, i+1), keepdims=keep_dims) +npy_out = np.sum(abs(in_data), axis=(i, i+1), keepdims=keep_dims) +check_symbolic_forward(norm_sym, [in_data], [npy_out], + rtol=1e-2 if dtype is np.float16 else 1e-5, + atol=1e-5, ctx=ctx) +# check gradient +#check_numeric_gradient(norm_sym, [in_data], numeric_eps=1e-3, rtol=1e-2, atol=1e-3) Review comment: Also - check_symbolic_backward? This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services