#
# slice paddle model generator
#
import numpy as np
from save_model import saveModel
import paddle as pdpd
import sys

data_type = 'float32'

def slice(name : str, x, axes : list, start : list, end : list):
    pdpd.enable_static()

    with pdpd.static.program_guard(pdpd.static.Program(), pdpd.static.Program()):
        node_x = pdpd.static.data(name='x', shape=x.shape, dtype = data_type)
        out = pdpd.fluid.layers.slice(node_x, axes = axes, starts = start, ends = end)

        cpu = pdpd.static.cpu_places(1)
        exe = pdpd.static.Executor(cpu[0])
        # startup program will call initializer to initialize the parameters.
        exe.run(pdpd.static.default_startup_program())

        outs = exe.run(
            feed={'x': x},
            fetch_list=[out])             

        saveModel(name, exe, feedkeys=['x'], fetchlist=[out], inputs=[x], outputs=[outs[0]], target_dir=sys.argv[1])

    return outs[0]

def main():
    x = np.linspace(1, 60, num = 60, dtype=np.int32).reshape(4, 3, 5).astype(data_type)
    slice("slice", x, axes=[1, 2], start=(0, 1), end=(-1, 3))

    x = np.linspace(1, 60, num = 60, dtype=np.int32).reshape(2, 30).astype(data_type)
    slice("slice_1d", x, axes=[0], start=[0], end=[1])

if __name__ == "__main__":
    main()
[((12, 4, 12, 24), 'paddle.enable_static', 'pdpd.enable_static', ({}, {}), '()', True, 'import paddle as pdpd\n'), ((15, 17, 15, 77), 'paddle.static.data', 'pdpd.static.data', (), '', True, 'import paddle as pdpd\n'), ((16, 14, 16, 86), 'paddle.fluid.layers.slice', 'pdpd.fluid.layers.slice', (), '', True, 'import paddle as pdpd\n'), ((18, 14, 18, 39), 'paddle.static.cpu_places', 'pdpd.static.cpu_places', ({(18, 37, 18, 38): '1'}, {}), '(1)', True, 'import paddle as pdpd\n'), ((19, 14, 19, 42), 'paddle.static.Executor', 'pdpd.static.Executor', ({(19, 35, 19, 41): 'cpu[0]'}, {}), '(cpu[0])', True, 'import paddle as pdpd\n'), ((27, 8, 27, 116), 'save_model.saveModel', 'saveModel', (), '', False, 'from save_model import saveModel\n'), ((14, 35, 14, 56), 'paddle.static.Program', 'pdpd.static.Program', ({}, {}), '()', True, 'import paddle as pdpd\n'), ((14, 58, 14, 79), 'paddle.static.Program', 'pdpd.static.Program', ({}, {}), '()', True, 'import paddle as pdpd\n'), ((21, 16, 21, 53), 'paddle.static.default_startup_program', 'pdpd.static.default_startup_program', ({}, {}), '()', True, 'import paddle as pdpd\n'), ((32, 8, 32, 52), 'numpy.linspace', 'np.linspace', (), '', True, 'import numpy as np\n'), ((35, 8, 35, 52), 'numpy.linspace', 'np.linspace', (), '', True, 'import numpy as np\n')]