#
# 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, 'paddle.enable_static', 'pdpd.enable_static', True, 'import paddle as pdpd\n'), (15, 'paddle.static.data', 'pdpd.static.data', True, 'import paddle as pdpd\n'), (16, 'paddle.fluid.layers.slice', 'pdpd.fluid.layers.slice', True, 'import paddle as pdpd\n'), (18, 'paddle.static.cpu_places', 'pdpd.static.cpu_places', True, 'import paddle as pdpd\n'), (19, 'paddle.static.Executor', 'pdpd.static.Executor', True, 'import paddle as pdpd\n'), (27, 'save_model.saveModel', 'saveModel', False, 'from save_model import saveModel\n'), (14, 'paddle.static.Program', 'pdpd.static.Program', True, 'import paddle as pdpd\n'), (14, 'paddle.static.Program', 'pdpd.static.Program', True, 'import paddle as pdpd\n'), (21, 'paddle.static.default_startup_program', 'pdpd.static.default_startup_program', True, 'import paddle as pdpd\n'), (32, 'numpy.linspace', 'np.linspace', True, 'import numpy as np\n'), (35, 'numpy.linspace', 'np.linspace', True, 'import numpy as np\n')]