Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

4.4 KB
Raw Blame
# Copyright (C) 2018-2026 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

import jax.numpy as jnp
import numpy as np
import pytest

from jax_layer_test_class import JaxLayerTest


def get_rand_jnp_array(shape, dtype):
    if dtype in [np.float32, np.float16, np.float64]:
        res = np.random.uniform(-1000, 1000, shape).astype(dtype)
    else:
        res = np.random.randint(-10, 10, shape).astype(dtype)
    res[res == 0] = 1e-6
    return jnp.array(res)


class TestDiv(JaxLayerTest):
    def _prepare_input(self):
        lhs = get_rand_jnp_array(self.lhs_shape, self.lhs_type)
        rhs = get_rand_jnp_array(self.rhs_shape, self.rhs_type)
        return (lhs, rhs)

    def create_model(self, lhs_shape, rhs_shape, lhs_type, rhs_type):
        self.lhs_shape = lhs_shape
        self.rhs_shape = rhs_shape
        self.lhs_type = lhs_type
        self.rhs_type = rhs_type

        def jax_div(lhs, rhs):
            return lhs / rhs

        return jax_div, None, 'div'

    test_data = [
        dict(lhs_shape=[4], rhs_shape=[4]),
        dict(lhs_shape=[2, 5], rhs_shape=[2, 5]),
        dict(lhs_shape=[2, 3, 4, 5], rhs_shape=[2, 3, 4, 5]),
    ]

    input_types = [
        (np.float32, np.float32),
        (np.int32, np.float32),
        (np.float32, np.float16),
        (np.int32, np.int64),
    ]

    @pytest.mark.nightly
    @pytest.mark.precommit
    @pytest.mark.precommit_jax_fe
    @pytest.mark.parametrize("params", test_data)
    @pytest.mark.parametrize("input_type", input_types)
    def test_div(self, ie_device, precision, ir_version, params, input_type):
        self._test(*self.create_model(**params, lhs_type=input_type[0], rhs_type=input_type[1]), ie_device, precision,
                   ir_version)


class TestDivWithConstant(JaxLayerTest):
    def _prepare_input(self):
        lhs = get_rand_jnp_array(self.lhs_shape, self.lhs_type)
        rhs = get_rand_jnp_array(self.rhs_shape, self.rhs_type)
        return (lhs, rhs)

    def create_model(self, lhs_shape, rhs_shape, lhs_type, rhs_type):
        self.lhs_shape = lhs_shape
        self.rhs_shape = rhs_shape
        self.lhs_type = lhs_type
        self.rhs_type = rhs_type
        self.const = jnp.ones(self.lhs_shape, dtype=self.lhs_type)

        def jax_div_with_constant(lhs, rhs):
            return lhs / rhs / self.const

        return jax_div_with_constant, None, 'div'

    test_data = [
        dict(lhs_shape=[4], rhs_shape=[4]),
        dict(lhs_shape=[2, 5], rhs_shape=[2, 5]),
        dict(lhs_shape=[2, 3, 4, 5], rhs_shape=[2, 3, 4, 5]),
    ]

    input_types = [
        (np.float32, np.float32),
        (np.int32, np.float32),
        (np.float32, np.float16),
        (np.int32, np.int64),
    ]

    @pytest.mark.nightly
    @pytest.mark.precommit
    @pytest.mark.precommit_jax_fe
    @pytest.mark.parametrize("params", test_data)
    @pytest.mark.parametrize("input_type", input_types)
    def test_div_with_constant(self, ie_device, precision, ir_version, params, input_type):
        self._test(*self.create_model(**params, lhs_type=input_type[0], rhs_type=input_type[1]), ie_device, precision,
                   ir_version)


class TestDivWithLiteralInvar(JaxLayerTest):
    def _prepare_input(self):
        lhs = get_rand_jnp_array(self.lhs_shape, self.lhs_type)
        rhs = get_rand_jnp_array(self.rhs_shape, self.rhs_type)
        return (lhs, rhs)

    def create_model(self, lhs_shape, rhs_shape, lhs_type, rhs_type):
        self.lhs_shape = lhs_shape
        self.rhs_shape = rhs_shape
        self.lhs_type = lhs_type
        self.rhs_type = rhs_type

        def jax_div_with_constant(lhs, rhs):
            x = lhs / 5
            return x / rhs

        return jax_div_with_constant, None, 'div'

    test_data = [
        dict(lhs_shape=[4], rhs_shape=[4]),
        dict(lhs_shape=[2, 5], rhs_shape=[2, 5]),
        dict(lhs_shape=[2, 3, 4, 5], rhs_shape=[2, 3, 4, 5]),
    ]

    input_types = [
        (np.float32, np.float32),
        (np.int32, np.float32),
        (np.float32, np.float16),
        (np.int32, np.int64),
    ]

    @pytest.mark.nightly
    @pytest.mark.precommit
    @pytest.mark.precommit_jax_fe
    @pytest.mark.parametrize("params", test_data)
    @pytest.mark.parametrize("input_type", input_types)
    def test_div_with_literal_invar(self, ie_device, precision, ir_version, params, input_type):
        self._test(*self.create_model(**params, lhs_type=input_type[0], rhs_type=input_type[1]), ie_device, precision,
                   ir_version)