Module: Ignis::DType

Defined in:
lib/nvruby/dtype.rb

Overview

Supported data types with their properties

Constant Summary collapse

TYPES =

Data type definitions with byte size and FFI type

{
  float16: { bytes: 2, ffi: :uint16, cuda: 2, complex: false },   # CUDA_R_16F (stored as uint16)
  bfloat16: { bytes: 2, ffi: :uint16, cuda: 14, complex: false }, # CUDA_R_16BF (stored as uint16)
  float32: { bytes: 4, ffi: :float, cuda: 0, complex: false },
  float64: { bytes: 8, ffi: :double, cuda: 1, complex: false },
  int8: { bytes: 1, ffi: :int8, cuda: 3, complex: false },
  int16: { bytes: 2, ffi: :int16, cuda: 8, complex: false },
  int32: { bytes: 4, ffi: :int32, cuda: 10, complex: false },
  int64: { bytes: 8, ffi: :int64, cuda: 11, complex: false },
  uint8: { bytes: 1, ffi: :uint8, cuda: 17, complex: false },
  uint16: { bytes: 2, ffi: :uint16, cuda: 18, complex: false },
  uint32: { bytes: 4, ffi: :uint32, cuda: 19, complex: false },
  uint64: { bytes: 8, ffi: :uint64, cuda: 20, complex: false },
  complex64: { bytes: 8, ffi: :float, cuda: 4, complex: true },   # 2x float32
  complex128: { bytes: 16, ffi: :double, cuda: 5, complex: true } # 2x float64
}.freeze
CUBLAS_TYPES =

cuBLAS data type constants (cudaDataType_t values)

{
  float16: 2,     # CUDA_R_16F
  bfloat16: 14,   # CUDA_R_16BF
  float32: 0,     # CUDA_R_32F
  float64: 1,     # CUDA_R_64F
  complex64: 4,   # CUDA_C_32F
  complex128: 5,  # CUDA_C_64F
  int8: 3,        # CUDA_R_8I
  int32: 10       # CUDA_R_32I
}.freeze

Class Method Summary collapse

Class Method Details

.allArray<Symbol>

List all supported dtypes

Returns:

  • (Array<Symbol>)

    All dtype symbols



110
111
112
# File 'lib/nvruby/dtype.rb', line 110

def all
  TYPES.keys
end

.byte_size(dtype) ⇒ Integer

Get byte size for a dtype

Parameters:

  • dtype (Symbol)

    Data type

Returns:

  • (Integer)

    Size in bytes

Raises:



40
41
42
43
44
45
# File 'lib/nvruby/dtype.rb', line 40

def byte_size(dtype)
  info = TYPES[dtype]
  raise UnsupportedDTypeError, dtype unless info

  info[:bytes]
end

.complex?(dtype) ⇒ Boolean

Check if dtype is complex

Parameters:

  • dtype (Symbol)

    Data type

Returns:

  • (Boolean)

Raises:



60
61
62
63
64
65
# File 'lib/nvruby/dtype.rb', line 60

def complex?(dtype)
  info = TYPES[dtype]
  raise UnsupportedDTypeError, dtype unless info

  info[:complex]
end

.complex_dtype(dtype) ⇒ Symbol

Get complex dtype for real types

Parameters:

  • dtype (Symbol)

    Real data type

Returns:

  • (Symbol)

    Complex dtype



128
129
130
131
132
133
134
135
136
# File 'lib/nvruby/dtype.rb', line 128

def complex_dtype(dtype)
  case dtype
  when :float32 then :complex64
  when :float64 then :complex128
  when :complex64, :complex128 then dtype
  else
    raise UnsupportedDTypeError.new(dtype, operation: "complex conversion")
  end
end

.cublas_type(dtype) ⇒ Integer

Get cuBLAS type constant

Parameters:

  • dtype (Symbol)

    Data type

Returns:

  • (Integer)

    cuBLAS type constant

Raises:



91
92
93
94
95
96
# File 'lib/nvruby/dtype.rb', line 91

def cublas_type(dtype)
  type = CUBLAS_TYPES[dtype]
  raise UnsupportedDTypeError.new(dtype, operation: "cuBLAS") unless type

  type
end

.ffi_type(dtype) ⇒ Symbol

Get FFI type for a dtype

Parameters:

  • dtype (Symbol)

    Data type

Returns:

  • (Symbol)

    FFI type symbol

Raises:



50
51
52
53
54
55
# File 'lib/nvruby/dtype.rb', line 50

def ffi_type(dtype)
  info = TYPES[dtype]
  raise UnsupportedDTypeError, dtype unless info

  info[:ffi]
end

.float?(dtype) ⇒ Boolean

Check if dtype is floating point

Parameters:

  • dtype (Symbol)

    Data type

Returns:

  • (Boolean)


70
71
72
# File 'lib/nvruby/dtype.rb', line 70

def float?(dtype)
  %i[float16 bfloat16 float32 float64 complex64 complex128].include?(dtype)
end

.integer?(dtype) ⇒ Boolean

Check if dtype is integer

Parameters:

  • dtype (Symbol)

    Data type

Returns:

  • (Boolean)


77
78
79
# File 'lib/nvruby/dtype.rb', line 77

def integer?(dtype)
  %i[int8 int16 int32 int64 uint8 uint16 uint32 uint64].include?(dtype)
end

.real_dtype(dtype) ⇒ Symbol

Get real component dtype for complex types

Parameters:

  • dtype (Symbol)

    Complex data type

Returns:

  • (Symbol)

    Real component dtype



117
118
119
120
121
122
123
# File 'lib/nvruby/dtype.rb', line 117

def real_dtype(dtype)
  case dtype
  when :complex64 then :float32
  when :complex128 then :float64
  else dtype
  end
end

.signed?(dtype) ⇒ Boolean

Check if dtype is signed

Parameters:

  • dtype (Symbol)

    Data type

Returns:

  • (Boolean)


84
85
86
# File 'lib/nvruby/dtype.rb', line 84

def signed?(dtype)
  !%i[uint8 uint16 uint32 uint64].include?(dtype)
end

.validate!(dtype) ⇒ Symbol

Validate dtype

Parameters:

  • dtype (Symbol)

    Data type to validate

Returns:

  • (Symbol)

    Validated dtype

Raises:



102
103
104
105
106
# File 'lib/nvruby/dtype.rb', line 102

def validate!(dtype)
  raise UnsupportedDTypeError, dtype unless TYPES.key?(dtype)

  dtype
end