Exemple #1
0
 def verify_output(self, outs):
     outs = convert_uint16_to_float(outs)
     self.assertEqual(outs[0].shape, (123, 92))
     hist, _ = np.histogram(outs[0], range=(-3, 5))
     hist = hist.astype("float32")
     hist /= float(outs[0].size)
     data = np.random.normal(size=(123, 92), loc=1, scale=2)
     hist2, _ = np.histogram(data, range=(-3, 5))
     hist2 = hist2.astype("float32")
     hist2 /= float(outs[0].size)
     self.assertTrue(np.allclose(hist, hist2, rtol=0, atol=0.05),
                     "hist: " + str(hist) + " hist2: " + str(hist2))
    def check_input_and_optput(self,
                               scope,
                               place,
                               inplace,
                               w1_has_data=False,
                               w2_has_data=False,
                               w3_has_data=False):

        self.create_selected_rows(scope, place, "W1", w1_has_data)
        self.create_selected_rows(scope, place, "W2", w2_has_data)
        self.create_selected_rows(scope, place, "W3", w3_has_data)

        # create Out Variable
        if inplace:
            out_var_name = "W1"
        else:
            out_var_name = "Out"
        out = scope.var(out_var_name).get_selected_rows()

        # create and run sum operator
        sum_op = Operator("sum", X=["W1", "W2", "W3"], Out=out_var_name)
        sum_op.run(scope, place)

        has_data_w_num = 0
        for has_data in [w1_has_data, w2_has_data, w3_has_data]:
            if has_data:
                has_data_w_num += 1

        if has_data_w_num > 0:
            self.assertEqual(len(out.rows()), 7)
            out_bf16 = np.array(out.get_tensor())
            out_fp32 = convert_uint16_to_float(out_bf16)
            ref_fp32 = convert_uint16_to_float(
                self._get_array(self.rows, self.row_numel)) * has_data_w_num
            np.testing.assert_allclose(out_fp32,
                                       ref_fp32,
                                       atol=0,
                                       rtol=0.95e-2)
        else:
            self.assertEqual(len(out.rows()), 0)
Exemple #3
0
 def _check_output(self,
                   actual,
                   reference,
                   bf16=False,
                   atol=0,
                   rtol=0.15e-2):
     output = actual if bf16 else convert_uint16_to_float(actual)
     if bf16:
         np.testing.assert_allclose(output, reference, atol=atol, rtol=rtol)
     else:
         try:
             print('Compare with FP32 values:')
             np.testing.assert_allclose(output,
                                        reference,
                                        atol=atol,
                                        rtol=rtol)
         except AssertionError as e:
             print(e)
 def test_lookup_results(self):
     lookup_result = convert_uint16_to_float(self.result[1])
     lookup_ref = _lookup(self.w_fp32, self.ids, self.flat_ids)
     self.assertTrue(np.array_equal(lookup_result, lookup_ref))
 def test_embedding_weights(self):
     result = convert_uint16_to_float(self.result[0])
     self.assertTrue(np.array_equal(self.w_fp32, result))
 def _check_output(self, reference, result_array):
     result_array_fp32 = convert_uint16_to_float(result_array)
     np.testing.assert_allclose(result_array_fp32, reference, rtol=1.5e-2)
Exemple #7
0
 def check_output(self, actual_bf16, reference, atol=0, rtol=0.15e-2):
     actual_fp32 = convert_uint16_to_float(actual_bf16)
     np.testing.assert_allclose(actual_fp32,
                                reference,
                                atol=atol,
                                rtol=rtol)