Best Python code snippet using tempest_python
test_auto_diff_unit.py
Source:test_auto_diff_unit.py  
...4class AutoDiffUnitTesting(unittest.TestCase):5    def _assertAllClose(self, actual, desired, rtol=1e-07, atol=1e-12, equal_nan=True):6        np.testing.assert_allclose(actual, desired, rtol, atol, equal_nan)7class TestSingleVariableAutoDiff(AutoDiffUnitTesting):8    def _test_helper(self, f, x, df_dx, debug=False):9        if debug:10            breakpoint()11        input_x = x12        f_x = f(input_x)13        with auto_diff.AutoDiff(input_x) as x:14            y, Jf = auto_diff.get_value_and_jacobian(f(x))15        self._assertAllClose(y, f_x)16        self._assertAllClose(Jf, df_dx)17    18        # Some bugs only appeared with rectangular Jacobians.19        A = np.random.rand(input_x.shape[0], 3 * input_x.shape[0])20        b = np.random.rand(input_x.shape[0], 1)21        x = np.linalg.lstsq(A, input_x - b, rcond=None)[0]22        df_dx = df_dx @ A23        with auto_diff.AutoDiff(x) as x:24            y, Jf = auto_diff.get_value_and_jacobian(f(A @ x + b))25    26        self._assertAllClose(y, f_x)27        self._assertAllClose(Jf, df_dx)28    def _test_out(self, f, x, df_dx, debug=False):29        if debug:30            breakpoint()31        input_x = x32        f_x = f(input_x)33        with auto_diff.AutoDiff(input_x) as x:34            out_dest = np.ndarray(f_x.shape)35            f(x, out=out_dest)36            y, Jf = auto_diff.get_value_and_jacobian(out_dest)37        self._assertAllClose(f_x, y)38        self._assertAllClose(Jf, df_dx)39    def test_add_with_out(self):40        def f(x):41            y = np.sqrt(x)42            out = np.ndarray((3, 1))43            np.add(x, y, out=out)44            return out45        x = np.array([[2.], [4.], [9.0]])46        df_dx = np.array([[1 + 0.5 / np.sqrt(2.), 0.0, 0.0],47                            [0.0, 1 + 1./4., 0.0],48                            [0.0, 0.0, 1 + 1./6.]])49        self._test_helper(f, x, df_dx)50    def test_multiply_with_out(self):51        def f(x):52            y = np.sqrt(x)53            out = np.ndarray((3, 1))54            np.multiply(x, y, out=out)55            return out56        x = np.array([[2.], [4.], [9.0]])57        df_dx = np.array([[np.sqrt(2) + 1 / np.sqrt(2.), 0.0, 0.0],58                        [0.0, 2 + 4 * 1./4., 0.0],59                        [0.0, 0.0, 3 + 9 * 1./6.]])60        self._test_helper(f, x, df_dx)61    62    def test_abs(self):63        f = np.abs64        x = np.array([[2.], [-2.], [0.0]])65        df_dx = np.array([[1.0, 0.0, 0.0], [0.0, -1.0, 0.0], [0.0, 0.0, 0.0]])66        # x = np.array([[2.], [-2.], [4.0]])67        # df_dx = np.array([[1.0, 0.0, 0.0], [0.0, -1.0, 0.0], [0.0, 0.0, 1.0]])68        with self.assertWarns(UserWarning, msg='abs of a near-zero number, derivative is ill-defined'):69            self._test_helper(f, x, df_dx)70            self._test_out(f, x, df_dx)71    def test_sqrt(self):72        f = np.sqrt73        x = np.array([[2.], [4.], [9.0]])74        df_dx = np.array([[0.5 / np.sqrt(2.), 0.0, 0.0],75                        [0.0, 1./4., 0.0],76                        [0.0, 0.0, 1./6.]])77        # x = np.array([[2.], [-2.], [4.0]])78        # df_dx = np.array([[1.0, 0.0, 0.0], [0.0, -1.0, 0.0], [0.0, 0.0, 1.0]])79        self._test_helper(f, x, df_dx)80        self._test_out(f, x, df_dx)81        82    def test_sin(self):83        f = np.sin84        x = np.array([[np.pi], [-np.pi/2], [np.pi/4]])85        df_dx = np.array([[-1.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0, 0, np.sqrt(2) / 2]])86        self._test_helper(f, x, df_dx)87        self._test_out(f, x, df_dx)88        89       90    def test_cos(self):91        f = np.cos92        x = np.array([[np.pi], [-np.pi/2], [np.pi/4]])93        df_dx = np.array([[0.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0, 0, -np.sqrt(2) / 2]])94        self._test_helper(f, x, df_dx)95        self._test_out(f, x, df_dx)96        97    def test_tan(self):98        f = np.tan99        x = np.array([[np.pi], [-np.pi/3], [np.pi/4]])100        df_dx = np.array([[1.0, 0.0, 0.0], [0.0, 4.0, 0.0], [0, 0, 2.0]])101        self._test_helper(f, x, df_dx)102        self._test_out(f, x, df_dx)103    def test_tanh(self):104        f = np.tanh105        x = np.array([[np.log(2)], [-np.log(3)], [0.0]])106        df_dx = np.array([[0.64, 0.0, 0.0], [0.0, 0.36, 0.0], [0, 0, 1.0]])107        self._test_helper(f, x, df_dx)108        self._test_out(f, x, df_dx)109    def test_sinh(self):110        f = np.sinh111        x = np.array([[np.log(2)], [-np.log(3)], [0.0]])112        df_dx = np.array([[1.25, 0.0, 0.0], [0.0, 5 / 3, 0.0], [0, 0, 1.0]])113        self._test_helper(f, x, df_dx)114        self._test_out(f, x, df_dx)115    def test_cosh(self):116        f = np.cosh117        x = np.array([[np.log(2)], [-np.log(3)], [0.0]])118        df_dx = np.array([[2.25/3, 0.0, 0.0], [0.0, -4/3, 0.0], [0, 0, 0.0]])119        self._test_helper(f, x, df_dx)120        self._test_out(f, x, df_dx)121    def test_arctanh(self):122        f = np.arctanh123        x = np.array([[np.sqrt(1/4)], [0.5], [0.0]])124        df_dx = np.array([[4/3, 0.0, 0.0], [0.0, 1/(1 - 0.5**2), 0.0], [0, 0, 1.0]])125        self._test_helper(f, x, df_dx)126        self._test_out(f, x, df_dx)127    def test_arccosh(self):128        f = np.arccosh129        x = np.array([[np.sqrt(5)], [np.sqrt(10)], [np.sqrt(17)]])130        df_dx = np.array([[1/2, 0.0, 0.0], [0.0, 1/3, 0.0], [0, 0, 1.0/4]])131        self._test_helper(f, x, df_dx)132        self._test_out(f, x, df_dx)133    def test_arcsinh(self):134        f = np.arcsinh135        x = np.array([[np.sqrt(3)], [np.sqrt(8)], [np.sqrt(15)]])136        df_dx = np.array([[1/2, 0.0, 0.0], [0.0, 1/3, 0.0], [0, 0, 1.0/4]])137        self._test_helper(f, x, df_dx)138        self._test_out(f, x, df_dx)139    def test_arcsin(self):140        f = np.arcsin141        x = np.array([[0], [np.sqrt(2)/2], [1/2]])142        df_dx = np.array([[1.0, 0.0, 0.0],143                        [0.0, np.sqrt(2), 0.0],144                        [0, 0, 2 / np.sqrt(3)]])145        self._test_helper(f, x, df_dx)146        self._test_out(f, x, df_dx)147    148    def test_arccos(self):149        f = np.arccos150        x = np.array([[0], [np.sqrt(2)/2], [1/2]])151        df_dx = np.array([[-1.0, 0.0, 0.0],152                        [0.0, -np.sqrt(2), 0.0],153                        [0, 0, -2 / np.sqrt(3)]])154        self._test_helper(f, x, df_dx)155        self._test_out(f, x, df_dx)156        157    def test_arctan(self):158        f = np.arctan159        x = np.array([[-1.0], [99999], [1.0]])160        df_dx = np.array([[0.5, 0.0, 0.0],161                        [0.0, 1.0002e-10, 0.0],162                        [0, 0, 1/2]])163        self._test_helper(f, x, df_dx)164        self._test_out(f, x, df_dx)165        166    def test_log(self):167        f = np.log168        x = np.array([[1.0], [0.5], [2.5]])169        df_dx = np.diag([1.0, 2, .4])170        self._test_helper(f, x, df_dx)171        self._test_out(f, x, df_dx)172        173    def test_log2(self):174        f = np.log2175        x = np.array([[1.0], [0.5], [2.5]])176        df_dx = np.diag([1.0, 2, .4]) / np.log(2)177        self._test_helper(f, x, df_dx)178        self._test_out(f, x, df_dx)179        180    def test_log10(self):181        f = np.log10182        x = np.array([[1.0], [0.5], [2.5]])183        df_dx = np.diag([1.0, 2, .4]) / np.log(10)184        self._test_helper(f, x, df_dx)185        self._test_out(f, x, df_dx)186        187    def test_log1p(self):188        f = np.log1p189        x = np.array([[1.0], [-0.5], [1.5]])190        df_dx = np.diag([.5, 2, .4])191        self._test_helper(f, x, df_dx)192        self._test_out(f, x, df_dx)193        194    def test_negative(self):195        f = np.negative196        x = np.array([[1.0], [-0.5], [1.5]])197        df_dx = -np.eye(3)198        self._test_helper(f, x, df_dx)199        self._test_out(f, x, df_dx)200        201    def test_positive(self):202        f = np.positive203        x = np.array([[1.0], [-0.5], [1.5]])204        df_dx = np.eye(3)205        self._test_helper(f, x, df_dx)206        self._test_out(f, x, df_dx)207        208    def test_decomposing_x(self):209        def f(x):210            x_1, x_2, x_3 = x211            return np.array([x_1 + x_2 + x_3])212        x = np.array([[-1.0], [2.0], [3.0]])213        df_dx = np.array([[1, 1, 1]])214        self._test_helper(f, x, df_dx)215        def f(x):216            x_1, x_2, x_3 = x217            return np.array([x_1 - x_2 - 2 * x_3])218        x = np.array([[-1.0], [2.0], [3.0]])219        df_dx = np.array([[1, -1, -2]])220        self._test_helper(f, x, df_dx)221        def f(x):222            x_1, x_2, x_3 = x223            return np.array([x_1 * x_2 - 2. * x_3 - x_1 * 3.,224                            x_2 / x_3 - x_2 / 2. + 3. / x_3])225            226        x = np.array([[-1.0], [6.0], [3.0]])227        df_dx = np.array([[3.0, -1, -2], [0, .3333333333 - 0.5, -6 / 9.0 - 1 / 3.0]])228        self._test_helper(f, x, df_dx)229        def f(x):230            x_1, x_2 = x231            return np.array([x_1**2., np.e**x_2, x_1**x_2])232        x = np.array([[3.0], [3.0]])233        df_dx = np.array([[6.0, 0.0], [0.0, np.exp(3)], [27.0, 27.0 * np.log(3)]])234    def test_constant(self):235        def f(x):236            return np.array([[0], [1], [2.0]])237        x = np.array([[2.0]])238        df_dx = np.array([[0], [0], [0.0]])239        self._test_helper(f, x, df_dx)240    def test_matrixmul(self):241        A = np.array([[1.0, 4.0, 7.0], [5.0, 7.0, -200]])242        x = np.array([[2.0], [3.0], [-4.0]])243        self._test_helper(lambda x: A @ x, x, A)244    def test_affine(self):245        A = np.array([[1.0, 4.0, 7.0], [5.0, 7.0, -200]])246        b = np.array([[3.0], [-np.pi]])247        x = np.array([[2.0], [3.0], [-4.0]])248        self._test_helper(lambda x: A @ x + b, x, A)249    def test_exp_of_affine(self):250        A = np.array([[1.0, -2.0, 7.0], [5.0, 7.0, 1]])251        b = np.array([[48.0], [-8.0]])252        x = np.array([[2.0], [1.0], [-7.0]])253        k = A @ x + b254        [y_1], [y_2] = np.exp(k)255        df_dx = np.diag([y_1, y_2]) @ A256        self._test_helper(lambda x: np.exp(A @ x + b), x, df_dx)257    def test_assign_scalar(self):258        def f(x):259            C = 1.0e-7;260            retval = C * x261            for i in range(3):262                retval[i] = 0263            return retval264        265        x = np.array([[4.0], [3.0], [6.0], [7.0]])266        df_dx = np.array([[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0, 0], [0, 0, 0, 0], [0,0, 0, 1.0e-7]])267        self._test_helper(f, x, df_dx)268    def test_assign_vector(self):269        def f(x, u):270            C = 1.0e-7;271            retval = C * x272            for i in range(3):273                retval[i] = u274            return retval275        276        x = np.array([[4.0], [3.0], [6.0], [7.0]])277        df_dx = np.array([[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0, 0], [0, 0, 0, 0], [0,0, 0, 1.0e-7]])278        u = np.array([[1.0]])279        self._test_helper(lambda x: f(x, u), x, df_dx)280    def test_mutating_in_place(self):281        def f(x):282            out = np.zeros((3, 1))283            out[1, 0] -= x[0, 0]284            out[2, 0] += x[1]285            return out286        x = np.array([[5.], [2.]])287        df_dx = np.array([[0., 0.],288                          [-1., 0.],289                          [0., 1.]])290        self._test_helper(f, x, df_dx)291    def test_mutating_in_place_same_row(self):292        def f(x):293            out = np.zeros((1, 1))294            out[0, 0] += x[0, 0]295            out[0, 0] += x[1, 0]296            return out297        x = np.array([[5.], [2.]])298        df_dx = np.array([[1., 1.]])299        self._test_helper(f, x, df_dx)300class TestMultipleVariableAutoDiff(AutoDiffUnitTesting):301    def _test_helper(self, f, x, u, df_dx, df_du, debug=False):302        if debug:303            breakpoint()304        f_xu = f(x, u)305        input_x = x306        input_u = u307        with auto_diff.AutoDiff(x, u) as (x, u):308            y, (J_fx, J_fu) = auto_diff.get_value_and_jacobians(f(x, u))309        self._assertAllClose(y, f_xu)310        self._assertAllClose(J_fx, df_dx)311        self._assertAllClose(J_fu, df_du)312        u = input_u313        with auto_diff.AutoDiff(input_x) as x:314            y, J_fx = auto_diff.get_value_and_jacobian(f(x, u))315        self._assertAllClose(y, f_xu)316        self._assertAllClose(J_fx, df_dx)317        x = input_x318        with auto_diff.AutoDiff(input_u) as u:319            y, J_fu = auto_diff.get_value_and_jacobian(f(x, u))320        self._assertAllClose(y, f_xu)321        self._assertAllClose(J_fu, df_du)322        # Some bugs only appeared with rectangular Jacobians.323        A_x = np.random.rand(input_x.shape[0], 3 * input_x.shape[0])324        b_x = np.random.rand(input_x.shape[0], 1)325        affine_x = np.linalg.lstsq(A_x, input_x - b_x, rcond=None)[0]326        A_u = np.random.rand(input_u.shape[0], 3 * input_x.shape[0])327        b_u = np.random.rand(input_u.shape[0], 1)328        affine_u = np.linalg.lstsq(A_u, input_u - b_u, rcond=None)[0]329        df_dx = df_dx @ A_x330        df_du = df_du @ A_u331        with auto_diff.AutoDiff(affine_x, affine_u) as (x, u):332            y, (J_fx, J_fu) = auto_diff.get_value_and_jacobians(f(A_x @ x + b_x, A_u @ u + b_u))333    334        self._assertAllClose(y, f_xu)335        self._assertAllClose(J_fx, df_dx)336        self._assertAllClose(J_fu, df_du)337        with auto_diff.AutoDiff(affine_x) as x:338            y, J_fx = auto_diff.get_value_and_jacobian(f(A_x @ x + b_x, A_u @ affine_u + b_u))339    340        self._assertAllClose(y, f_xu)341        self._assertAllClose(J_fx, df_dx)342        with auto_diff.AutoDiff(affine_u) as u:343            y, J_fu = auto_diff.get_value_and_jacobian(f(A_x @ affine_x + b_x, A_u @ u + b_u))344    345        self._assertAllClose(y, f_xu)346        self._assertAllClose(J_fu, df_du)347    def test_linear(self):348        A = np.array([[5, 6., 3., 1.],349                      [2, 3.,  5,  4],350                      [np.pi, np.pi/2, np.e, np.exp(2)]])351        B = np.array([[4, 2., 1.5],352                      [.25, 2.5,  9],353                      [np.e, 0.0, np.exp(0.5)]])354        x = np.array([[.6, .8, .3, .4]]).T355        u = np.array([[.2, 8.3, .5]]).T356        self._test_helper(lambda x, u: A @ x + B @ u, x, u, A, B)357    def test_add_and_mulitply(self):358        def f(x, u):359            A = np.array([[2., -1], [-1, 2]])360            B = np.array([[1.] , [0]])361            return A @ x + B * u362        x = np.array([[5.], [3.]])363        u = np.array([[3.]])364        self._test_helper(f, x, u, np.array([[2, -1], [-1, 2.]]), np.array([[1], [0.]]))365    def test_addition_broadcasting_and_reshape(self):366        def f(x, u):367            y = x.T + u368            return y.reshape((6, 1))369        x = np.array([[5.], [2.]])370        u = np.array([[3.], [7.], [11.]])371        df_dx = np.array([[1., 0.],372                          [0., 1.],373                          [1., 0.],374                          [0., 1.],375                          [1., 0.],376                          [0., 1.]])377        378        df_du = np.array([[1., 0., 0.],379                          [1., 0., 0.],380                          [0., 1., 0.],381                          [0., 1., 0.],382                          [0., 0., 1.],383                          [0., 0., 1.]])384        self._test_helper(f, x, u, df_dx, df_du)385    def test_subtraction_broadcasting_and_reshape(self):386        def f(x, u):387            y = np.subtract(x.T, u)388            return y.reshape((6, 1))389        x = np.array([[5.], [2.]])390        u = np.array([[3.], [7.], [11.]])391        df_dx = np.array([[1., 0.],392                          [0., 1.],393                          [1., 0.],394                          [0., 1.],395                          [1., 0.],396                          [0., 1.]])397        df_du = np.array([[-1., 0., 0.],398                          [-1., 0., 0.],399                          [0., -1., 0.],400                          [0., -1., 0.],401                          [0., 0., -1.],402                          [0., 0., -1.]])403        self._test_helper(f, x, u, df_dx, df_du)404    def test_division_broadcasting_and_reshape(self):405        def f(x, u):406            y = np.divide(x.T, u)407            return y.reshape((4, 1))408        x = np.array([[5.], [2.]])409        u = np.array([[3.], [11.]])410        df_dx = np.array([[1./3, 0.],411                          [0., 1./3],412                          [1./11, 0.],413                          [0., 1./11]])414        df_du = np.array([[-5/9., 0.],415                          [-2/9., 0.],416                          [0., -5/121.],417                          [0., -2/121.]])418        self._test_helper(f, x, u, df_dx, df_du)419    def test_float_power_broadcasting_and_reshape(self):420        def f(x, u):421            y = np.float_power(x.T, u)422            return y.reshape((4, 1))423        x = np.array([[5.], [2.]])424        u = np.array([[3.], [4.]])425        df_dx = np.array([[3 * 5.**2, 0.],426                          [0., 3 * 2.**2],427                          [4 * 5.**3, 0.],428                          [0., 4 * 2.**3]])429        df_du = np.array([[np.log(5) * 5**3, 0.],430                          [np.log(2) * 2**3, 0.],431                          [0., np.log(5) * 5**4],432                          [0., np.log(2) * 2**4]])433        self._test_helper(f, x, u, df_dx, df_du)434    def test_power_broadcasting_and_reshape(self):435        def f(x, u):436            y = np.power(x.T, u)437            return y.reshape((4, 1))438        x = np.array([[5.], [2.]])439        u = np.array([[3.], [4.]])440        df_dx = np.array([[3 * 5.**2, 0.],441                          [0., 3 * 2.**2],442                          [4 * 5.**3, 0.],443                          [0., 4 * 2.**3]])444        df_du = np.array([[np.log(5) * 5**3, 0.],445                          [np.log(2) * 2**3, 0.],446                          [0., np.log(5) * 5**4],447                          [0., np.log(2) * 2**4]])448        self._test_helper(f, x, u, df_dx, df_du)449class TestArbitraryShapeAutoDiff(AutoDiffUnitTesting):450    def _test_helper(self, f, x, df_dx, debug=False):451        if debug:452            breakpoint()453        input_x = x454        f_x = f(input_x)455        with auto_diff.AutoDiff(input_x) as x:456            ad_f_x = f(x)457            y, Jf = ad_f_x.val, ad_f_x.der458        self._assertAllClose(y, f_x)459        self._assertAllClose(Jf, df_dx)460    def _test_transpose(self):461        # Testing transpose requires accessing internals as it enforces the output462        # being a column vector463        print("TODO: Write a test of transpose")464        # f = lambda x: x.T465        # x = np.array([[np.pi], [-np.pi/2], [np.pi/4]])466        # df_dx = np.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0, 0, 1.0]])467        # test(f, x, df_dx, 'transpose')468    def _test_2d_matrix_1d_vector(self):469        A = np.array([[1, 5., 0.], [3., 6., 2.]])470        x = np.array([0., 1., 2.])471        self._test_helper(lambda x: A @ x, x, A)472if __name__ == '__main__':...loss_scale_manager_test.py
Source:loss_scale_manager_test.py  
...42      else:43        self.evaluate(update_op)44      self.assertEqual(loss_scale, self.evaluate(lsm.get_loss_scale()))45class ExponentialUpdateLossScaleManagerTest(test.TestCase):46  def _test_helper(self,47                   inputs,48                   expected_outputs,49                   init_loss_scale=1,50                   incr_every_n_step=2,51                   decr_every_n_nan_or_inf=2):52    ratio = 253    lsm = lsm_lib.ExponentialUpdateLossScaleManager(54        init_loss_scale=init_loss_scale,55        incr_every_n_steps=incr_every_n_step,56        decr_every_n_nan_or_inf=decr_every_n_nan_or_inf,57        incr_ratio=ratio,58        decr_ratio=1. / ratio)59    itr = _GetExampleIter(inputs)60    update_fn = lambda: lsm.update_loss_scale(itr.get_next())61    self.evaluate(variables.global_variables_initializer())62    actual_outputs = []63    if not context.executing_eagerly():64      update_op = update_fn()65    for _ in range(len(inputs)):66      if context.executing_eagerly():67        update_fn()68      else:69        self.evaluate(update_op)70      actual_outputs.append(self.evaluate(lsm.get_loss_scale()))71    self.assertEqual(actual_outputs, expected_outputs)72  @test_util.run_in_graph_and_eager_modes73  def test_increase_every_n_steps(self):74    inputs = [True] * 675    expected_outputs = [1, 2, 2, 4, 4, 8]76    self._test_helper(inputs, expected_outputs)77  @test_util.run_in_graph_and_eager_modes78  def test_keep_increasing_until_capped(self):79    init_loss_scale = np.finfo(np.float32).max / 4 + 1080    max_float = np.finfo(np.float32).max81    inputs = [True] * 682    # Output is capped the 2nd time it doubles.83    expected_outputs = [84        init_loss_scale, init_loss_scale * 2, init_loss_scale * 2, max_float,85        max_float, max_float86    ]87    self._test_helper(inputs, expected_outputs, init_loss_scale)88  @test_util.run_in_graph_and_eager_modes89  def test_decrease_every_n_steps(self):90    inputs = [False] * 691    init_loss_scale = 102492    expected_outputs = [1024, 512, 512, 256, 256, 128]93    self._test_helper(inputs, expected_outputs, init_loss_scale)94  @test_util.run_in_graph_and_eager_modes95  def test_keep_decreasing_until_one(self):96    inputs = [False] * 1097    init_loss_scale = 1698    expected_outputs = [16, 8, 8, 4, 4, 2, 2, 1, 1, 1]99    self._test_helper(inputs, expected_outputs, init_loss_scale)100  @test_util.run_in_graph_and_eager_modes101  def test_incr_bad_step_clear_good_step(self):102    inputs = [True, True, True, False, True]103    expected_outputs = [1, 2, 2, 2, 2]104    self._test_helper(inputs, expected_outputs)105  @test_util.run_in_graph_and_eager_modes106  def test_incr_good_step_does_not_clear_bad_step(self):107    inputs = [True, True, True, False, True, False]108    expected_outputs = [1, 2, 2, 2, 2, 1]109    self._test_helper(inputs, expected_outputs)110  @test_util.run_in_graph_and_eager_modes111  def test_trigger_loss_scale_update_each_step(self):112    """Test when incr_every_n_step and decr_every_n_nan_or_inf is 1."""113    init_loss_scale = 1114    incr_every_n_step = 1115    decr_every_n_nan_or_inf = 1116    inputs = [True] * 3 + [False, True, True]117    expected_outputs = [2, 4, 8, 4, 8, 16]118    self._test_helper(inputs, expected_outputs, init_loss_scale,119                      incr_every_n_step, decr_every_n_nan_or_inf)120  @test_util.run_in_graph_and_eager_modes121  def test_alternating_good_and_bad_gradients_trigger_each_step(self):122    init_loss_scale = 1123    incr_every_n_step = 1124    decr_every_n_nan_or_inf = 1125    inputs = [True, False] * 4 + [True]126    expected_outputs = [2, 1, 2, 1, 2, 1, 2, 1, 2]127    self._test_helper(inputs, expected_outputs, init_loss_scale,128                      incr_every_n_step, decr_every_n_nan_or_inf)129  @test_util.run_in_graph_and_eager_modes130  def test_alternating_good_and_bad_gradients_trigger_incr_every_2steps(self):131    init_loss_scale = 32132    incr_every_n_step = 2133    decr_every_n_nan_or_inf = 1134    inputs = [True, False] * 3 + [True]135    expected_outputs = [32, 16, 16, 8, 8, 4, 4]136    self._test_helper(inputs, expected_outputs, init_loss_scale,137                      incr_every_n_step, decr_every_n_nan_or_inf)138  @test_util.run_in_graph_and_eager_modes139  def test_random_mix_good_and_bad_gradients(self):140    init_loss_scale = 4141    inputs = [142        False, False, True, True, True, False, True, False, True, True, True,143        False144    ]145    expected_outputs = [4, 2, 2, 4, 4, 4, 4, 2, 2, 4, 4, 4]146    self._test_helper(inputs, expected_outputs, init_loss_scale)147if __name__ == "__main__":...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
