def test_stack():
    W = ng.make_axis(length=4)
    H = ng.make_axis(length=5)
    I = ng.make_axis(length=3)

    axes = ng.make_axes([W, H])

    rng = RandomTensorGenerator(0, np.float32)

    a_v = [rng.uniform(0, 1, axes) for i in range(I.length)]

    for pos in range(len(axes) + 1):
        a = [ng.placeholder(axes, initial_value=p) for p in a_v]

        s = ng.stack(a, I, pos)

        with ExecutorFactory() as ex:
            num_funs = [
                ex.numeric_derivative(s, p, delta,
                                      *(np for np in a if np is not p))
                for p in a
            ]
            sym_funs = [
                ex.derivative(s, p, *(np for np in a if np is not p))
                for p in a
            ]

            for n_fun, s_fun, a_i in zip(num_funs, sym_funs, a_v):
                na_is = list(na_i for na_i in a_v if na_i is not a_i)
                d_n = n_fun(a_i, *na_is)
                d_s = s_fun(a_i, *na_is)
                ng.testing.assert_allclose(d_n, d_s, rtol=rtol, atol=atol)
Esempio n. 2
0
def test_stack(transformer_factory):
    if transformer_factory.name == flex_gpu_transformer_name:
        pytest.skip("Allowed to fail until PR2")

    W = ng.make_axis(length=4)
    H = ng.make_axis(length=5)
    I = ng.make_axis(length=3)

    axes = ng.make_axes([W, H])

    rng = RandomTensorGenerator(0, np.float32)

    a_v = [rng.uniform(0, 1, axes) for i in range(I.length)]

    for pos in range(len(axes) + 1):
        a = [ng.placeholder(axes, initial_value=_) for _ in a_v]

        s = ng.stack(a, I, pos)

        with ExecutorFactory() as ex:
            num_funs = [ex.numeric_derivative(s, _, delta) for _ in a]
            sym_funs = [ex.derivative(s, _) for _ in a]

            for n_fun, s_fun, a_i in zip(num_funs, sym_funs, a_v):
                d_n = n_fun(a_i)
                d_s = s_fun(a_i)
            ng.testing.allclose(d_n, d_s, rtol=rtol, atol=atol)
Esempio n. 3
0
def execute_convolution(image_height,
                        image_width,
                        filter_height,
                        filter_width,
                        channel=16,
                        batch_size=32,
                        filter_count=8,
                        image_3rd_dim=1,
                        filter_3rd_dim=1,
                        padding=(0, 0, 0),
                        stride=(1, 1, 1),
                        dilation=1,
                        np_comparison=False):

    pad_h, pad_w, pad_d = padding
    str_h, str_w, str_d = stride
    cf = ConvParams(C=channel,
                    N=batch_size,
                    K=filter_count,
                    D=image_3rd_dim,
                    H=image_height,
                    W=image_width,
                    T=filter_3rd_dim,
                    R=filter_height,
                    S=filter_width,
                    pad_d=pad_d,
                    pad_h=pad_h,
                    pad_w=pad_w,
                    str_d=str_d,
                    str_h=str_h,
                    str_w=str_w,
                    dil_d=dilation,
                    dil_h=dilation,
                    dil_w=dilation)

    inputs = ng.placeholder(cf.ax_i)
    filters = ng.placeholder(cf.ax_f)
    rng = RandomTensorGenerator(0, np.float32)
    input_value = rng.uniform(-4, 4, cf.ax_i, dtype=int)
    filter_value = rng.uniform(-4, 4, cf.ax_f, dtype=int)
    error_value = rng.uniform(-0.5, 0.5, cf.ax_o)
    with executor(
            ng.convolution(cf.conv_params, inputs, filters, axes=cf.ax_o),
            inputs, filters) as const_executor:
        out = const_executor(input_value, filter_value)

    if np_comparison:
        np_out, gradInp, gradF_np = \
            reference_conv(cf.dimI, cf.dimF, cf.dimO, cf.conv_params, input_value, filter_value,
                           error_value)
        return out, np_out
    return out
Esempio n. 4
0
import ngraph as ng
import ngraph.transformers as ngt
from ngraph.testing import RandomTensorGenerator, ExecutorFactory
from ngraph.frontends.neon import ax
from third_party.warp_ctc.ctc import CTC
import pytest

pytestmark = [
    pytest.mark.transformer_dependent("module"),
    pytest.config.flex_disabled(reason="#1898 no CTCKernel for Flex"),
    pytest.config.argon_disabled(
        reason="Argon Transformer error - TODO triage)")
]

rng = RandomTensorGenerator(0, np.float32)


def pytest_generate_tests(metafunc):
    bsz = [1]

    if 'data_args' in metafunc.fixturenames:
        fargs = []
        nout = [5, 7, 9]
        bsz = [4, 32]
        max_utt_len = [23, 31, 39]
        max_lbl_len = [8, 9, 10]
        fargs = itt.product(nout, bsz, max_utt_len, max_lbl_len)
        metafunc.parametrize('data_args', fargs)

Esempio n. 5
0
def test_recvop_tensorupdate():
    """
    The tensor (RecvOp_#_#) associated with the following conv op has two views:
    1) Non-flat view (e.g. RecvOp_#_#_1_1_1_1_4.shape=(1,1,1,1,4))
    2) Flat view (e.g. RecvOp_#_#_1_4.shape = (1,4))
    This test ensures that inside RecvOp code generation, the generated code
    should make sure both views get updated (e.g. by using update_RecvOp_#_# API)
    In this test, ng.dot operation tends to use the flat view (i.e. RecvOp_#_#_1_4)
    And previously RecvOp with RecvOp_#_#_1_1_1_1_4 = recv_from_send(send_id) failed
    to update both two views (i.e. flat and non-flat view of the same buffer/tensor)
    """
    class ConvParams(object):
        def __init__(self,
                     C=1,
                     N=1,
                     K=1,
                     D=1,
                     H=1,
                     W=1,
                     T=1,
                     R=1,
                     S=1,
                     pad_d=0,
                     pad_h=0,
                     pad_w=0,
                     str_d=1,
                     str_h=1,
                     str_w=1):

            from ngraph.frontends.common.utils import conv_output_dim
            M = conv_output_dim(D, T, pad_d, str_d)
            P = conv_output_dim(H, R, pad_h, str_h)
            Q = conv_output_dim(W, S, pad_w, str_w)

            self.dimO = (K, M, P, Q, N)
            self.dimI = (C, D, H, W, N)
            self.dimF = (C, T, R, S, K)

            self.conv_params = dict(pad_d=pad_d,
                                    pad_h=pad_h,
                                    pad_w=pad_w,
                                    str_d=str_d,
                                    str_h=str_h,
                                    str_w=str_w,
                                    dil_d=1,
                                    dil_h=1,
                                    dil_w=1)

            self.batch_axis = ng.make_axis(name='N', length=N)

            self.ax_i = ng.make_axes([
                ng.make_axis(name='C', length=C),
                ng.make_axis(name='D', length=D),
                ng.make_axis(name='H', length=H),
                ng.make_axis(name='W', length=W), self.batch_axis
            ])

            self.ax_f = ng.make_axes([
                ng.make_axis(name='C', length=C),
                ng.make_axis(name='D', length=T),
                ng.make_axis(name='H', length=R),
                ng.make_axis(name='W', length=S),
                ng.make_axis(name='K', length=K),
            ])

            self.ax_o = ng.make_axes([
                ng.make_axis(name='C', length=K),
                ng.make_axis(name='D', length=M),
                ng.make_axis(name='H', length=P),
                ng.make_axis(name='W', length=Q), self.batch_axis
            ])

    # Layer 1, using convolutation introduces multi/flatten view of tensors
    cf = ConvParams(C=2, N=4, K=1, H=2, W=2, R=2, S=2)

    inputs = ng.placeholder(axes=cf.ax_i)
    filters = ng.placeholder(axes=cf.ax_f)

    # randomly initialize
    from ngraph.testing import RandomTensorGenerator
    rng = RandomTensorGenerator(0, np.float32)
    # put value 1 into inputs/filters for conv
    input_value = rng.uniform(1, 1, cf.ax_i)
    filter_value = rng.uniform(1, 1, cf.ax_f)

    conv = ng.convolution(cf.conv_params, inputs, filters, axes=cf.ax_o)

    # Layer 2, using dot to ensure recv_op.axes == send_op.axes
    from ngraph.frontends.neon import UniformInit
    # put value 1 into weights for dot
    init_uni = UniformInit(1, 1)
    W_A = ng.make_axis(length=2)
    w_axes = ng.make_axes(W_A) + conv.axes.feature_axes()
    w = ng.variable(axes=w_axes, initial_value=init_uni)

    with ng.metadata(device_id='1'):
        dot = ng.dot(w, conv)

    with ExecutorFactory() as ex:
        dot_comp = ex.executor(dot, filters, inputs)
        dot_val = dot_comp(filter_value, input_value)

    np.testing.assert_array_equal(dot_val,
                                  [[8., 8., 8., 8.], [8., 8., 8., 8.]])