def package_simple_addition_model(self, do_fail=False):
        with TemporaryDirectory() as test_dir:
            neuropod_path = os.path.join(test_dir, "test_neuropod")
            model_code_dir = os.path.join(test_dir, "model_code")
            os.makedirs(model_code_dir)

            with open(os.path.join(model_code_dir, "addition_model.py"),
                      "w") as f:
                f.write(ADDITION_MODEL_SOURCE)

            # `create_python_neuropod` runs inference with the test data immediately
            # after creating the neuropod. Raises a ValueError if the model output
            # does not match the expected output.
            create_python_neuropod(
                neuropod_path=neuropod_path,
                model_name="addition_model",
                data_paths=[],
                code_path_spec=[{
                    "python_root":
                    model_code_dir,
                    "dirs_to_package": [
                        ""  # Package everything in the python_root
                    ],
                }],
                entrypoint_package="addition_model",
                entrypoint="get_model",
                # Get the input/output spec along with test data
                **get_addition_model_spec(do_fail=do_fail))

            # Run some additional checks
            check_addition_model(neuropod_path)
Example #2
0
    def test_noncontiguous_array(self):
        x = np.arange(16).astype(np.int64).reshape(4, 4)

        with TemporaryDirectory() as test_dir:
            neuropod_path = os.path.join(test_dir, "test_neuropod")
            model_code_dir = os.path.join(test_dir, "model_code")
            os.makedirs(model_code_dir)

            with open(os.path.join(model_code_dir, "splitter_model.py"),
                      "w") as f:
                f.write(NONCONTIGUOUS_MODEL_SOURCE)

            create_python_neuropod(
                neuropod_path=neuropod_path,
                model_name="splitter",
                data_paths=[],
                code_path_spec=[{
                    "python_root":
                    model_code_dir,
                    "dirs_to_package": [
                        ""  # Package everything in the python_root
                    ],
                }],
                entrypoint_package="splitter_model",
                entrypoint="get_model",
                input_spec=[{
                    "name": "x",
                    "dtype": "int64",
                    "shape": (4, 4)
                }],
                output_spec=[
                    {
                        "name": "x1",
                        "dtype": "int64",
                        "shape": (4, 2)
                    },
                    {
                        "name": "x2",
                        "dtype": "int64",
                        "shape": (4, 2)
                    },
                ],
                test_input_data={"x": x},
                test_expected_out={
                    "x1": x[:, :2],
                    "x2": x[:, 2:]
                },
            )
Example #3
0
    def package_sklearn_model(self, test_dir):
        neuropod_path = os.path.join(test_dir, "test_neuropod")
        model_code_dir = os.path.join(test_dir, "model_code")
        os.makedirs(model_code_dir)

        with open(os.path.join(model_code_dir, "sklearn_model.py"), "w") as f:
            f.write(SKLEARN_MODEL_SOURCE)

        create_python_neuropod(
            neuropod_path=neuropod_path,
            model_name="sklearn_model",
            data_paths=[],
            code_path_spec=[{
                "python_root": model_code_dir,
                "dirs_to_package":
                [""],  # Package everything in the python_root
            }],
            entrypoint_package="sklearn_model",
            entrypoint="get_model",
            input_spec=[{
                "name": "x",
                "dtype": "float64",
                "shape": ("batch_size", 2)
            }],
            output_spec=[{
                "name": "out",
                "dtype": "float64",
                "shape": ("batch_size", )
            }],
            test_input_data={"x": np.array([[3, 5]], dtype=np.float64)},
            test_expected_out={"out": np.array([16], dtype=np.float64)},
            # We define requirements this way because this test runs on python 2.7 - 3.8
            # but there isn't a single version of scikit-learn that works on all of them
            requirements="""
            # Requirements for this model
            scikit-learn=={}
            """.format("0.20.0" if sys.version_info.major == 2 else "0.22.0"),
        )

        return neuropod_path
    def package_dummy_model(self, test_dir, target):
        neuropod_path = os.path.join(test_dir, "test_neuropod")
        model_code_dir = os.path.join(test_dir, "model_code")
        os.makedirs(model_code_dir)

        with open(os.path.join(model_code_dir, "dummy_model.py"), "w") as f:
            f.write(DUMMY_MODEL_SOURCE.format(target))

        # `create_python_neuropod` runs inference with the test data immediately
        # after creating the neuropod. Raises a ValueError if the model output
        # does not match the expected output.
        create_python_neuropod(
            neuropod_path=neuropod_path,
            model_name="dummy_model",
            data_paths=[],
            code_path_spec=[{
                "python_root": model_code_dir,
                "dirs_to_package":
                [""],  # Package everything in the python_root
            }],
            entrypoint_package="dummy_model",
            entrypoint="get_model",
            input_spec=[{
                "name": "x",
                "dtype": "float32",
                "shape": (1, )
            }],
            output_spec=[{
                "name": "out",
                "dtype": "float32",
                "shape": (1, )
            }],
            test_input_data={"x": np.array([0.0], dtype=np.float32)},
            test_expected_out={"out": np.array([target], dtype=np.float32)},
        )

        return neuropod_path