Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/ml_dtypes/_iinfo.py: 37%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

52 statements  

1# Copyright 2023 The ml_dtypes Authors. 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14 

15"""Overload of numpy.iinfo to handle dtypes defined in ml_dtypes.""" 

16 

17from ml_dtypes._ml_dtypes_ext import int2 

18from ml_dtypes._ml_dtypes_ext import int4 

19from ml_dtypes._ml_dtypes_ext import uint2 

20from ml_dtypes._ml_dtypes_ext import uint4 

21import numpy as np 

22 

23_int2_dtype = np.dtype(int2) 

24_uint2_dtype = np.dtype(uint2) 

25_int4_dtype = np.dtype(int4) 

26_uint4_dtype = np.dtype(uint4) 

27 

28 

29class iinfo: # pylint: disable=invalid-name,missing-class-docstring 

30 kind: str 

31 bits: int 

32 min: int 

33 max: int 

34 dtype: np.dtype 

35 

36 def __init__(self, int_type): 

37 if int_type == _int2_dtype: 

38 self.dtype = _int2_dtype 

39 self.kind = "i" 

40 self.bits = 2 

41 self.min = -2 

42 self.max = 1 

43 elif int_type == _uint2_dtype: 

44 self.dtype = _uint2_dtype 

45 self.kind = "u" 

46 self.bits = 2 

47 self.min = 0 

48 self.max = 3 

49 elif int_type == _int4_dtype: 

50 self.dtype = _int4_dtype 

51 self.kind = "i" 

52 self.bits = 4 

53 self.min = -8 

54 self.max = 7 

55 elif int_type == _uint4_dtype: 

56 self.dtype = _uint4_dtype 

57 self.kind = "u" 

58 self.bits = 4 

59 self.min = 0 

60 self.max = 15 

61 else: 

62 ii = np.iinfo(int_type) 

63 self.dtype = ii.dtype 

64 self.kind = ii.kind 

65 self.bits = ii.bits 

66 self.min = ii.min 

67 self.max = ii.max 

68 

69 def __repr__(self): 

70 return f"iinfo(min={self.min}, max={self.max}, dtype={self.dtype})" 

71 

72 def __str__(self): 

73 return repr(self)