Coverage for /opt/cloud/slavespace/usr1/096471637100f3de0fcfc01072822a80/dttest/build_ut/python_tests/v1/ut/test_v1_tensor.py: 100%

52 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-07-27 10:02 +0800

1#!/usr/bin/env python3 

2# -*- coding: utf-8 -*- 

3# ------------------------------------------------------------------- 

4# ----------------------------------------------------------------------------------------------------------- 

5# Copyright (c) 2025 Huawei Technologies Co., Ltd. 

6# This program is free software, you can redistribute it and/or modify it under the terms and conditions of 

7# CANN Open Software License Agreement Version 2.0 (the "License"). 

8# Please refer to the License for details. You may not use this file except in compliance with the License. 

9# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, 

10# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. 

11# See LICENSE in the root of the software repository for the full text of the License. 

12# ----------------------------------------------------------------------------------------------------------- 

13 

14# content of test_sample.py 

15import unittest 

16 

17import numpy as np 

18from llm_datadist_v1 import DataType, Tensor, TensorDesc 

19 

20 

21class TensorUt(unittest.TestCase): 

22 def setUp(self) -> None: 

23 print("Begin ", self._testMethodName) 

24 

25 def tearDown(self) -> None: 

26 print("End ", self._testMethodName) 

27 

28 def test_str_tensor(self): 

29 arr1 = np.array([["aaaa", "bbbb"], ["ccc", "ddd"]]) 

30 src_type = arr1.dtype 

31 tensor = Tensor(arr1) 

32 self.assertEqual(tensor.numpy(True).dtype, src_type) 

33 print(tensor.numpy(True)) 

34 

35 def test_tensor(self): 

36 arr1 = np.random.rand(2, 3) 

37 arr1 = np.array(arr1, np.float32) 

38 print("src numpy:", arr1) 

39 tensor = Tensor(arr1) 

40 print("generated:", tensor) 

41 self.assertEqual(tensor.numpy().dtype, np.float32) 

42 

43 def test_tensor_with_tensor_desc(self): 

44 arr1 = np.array([[1 for i in range(256)]], np.int32) 

45 tensor_desc = TensorDesc(DataType.DT_INT32, (1, 256)) 

46 print(tensor_desc) 

47 tensor = Tensor(arr1, tensor_desc) 

48 self.assertEqual(tensor.numpy().dtype, np.int32) 

49 

50 def test_tensor_bf16(self): 

51 arr1 = np.array([1.875], np.float16) 

52 tensor_desc = TensorDesc(DataType.DT_BF16, [1]) 

53 tensor = Tensor(arr1, tensor_desc) 

54 print("generated numpy:", tensor.numpy()) 

55 self.assertEqual(tensor.numpy().dtype, np.float32) 

56 self.assertEqual(int(tensor.numpy()[0]), 1) 

57 

58 def test_tensor_foat16(self): 

59 arr1 = np.array([1.875], np.float16) 

60 tensor_desc = TensorDesc(DataType.DT_FLOAT16, [1]) 

61 tensor = Tensor(arr1, tensor_desc) 

62 print("generated numpy:", tensor.numpy()) 

63 self.assertEqual(tensor.numpy().dtype, np.float16) 

64 

65 def test_tensor_foat32_copy_true(self): 

66 arr1 = np.array([[1.0, 2.0], [3.0, 4.0]], np.float32) 

67 tensor_desc = TensorDesc(DataType.DT_FLOAT, (2, 2)) 

68 tensor = Tensor(arr1, tensor_desc) 

69 res = tensor.numpy(copy=True) 

70 print("generated numpy:", res) 

71 np.testing.assert_array_equal(res, arr1) 

72 self.assertEqual(res.dtype, np.float32) 

73 self.assertTrue(res.flags.c_contiguous) 

74 self.assertTrue(res.flags.writeable) 

75 arr1[0, 0] = 999.0 

76 self.assertNotEqual(res[0, 0], 999.0)