Class: FFNFFICacheCuda

Inherits:
Object
  • Object
show all
Defined in:
lib/toy/ffi/tinynn_cuda.rb

Overview

Same as FFNFFICache in lib/toy/ffi/tinynn.rb but uses TinyNNCuda. The class name differs so that drivers requiring BOTH modules (e.g. the CUDA parity smoke loads tinynn_cuda directly, and lib/transformer.rb transitively pulls in tinynn) don’t trip Spinel’s same-class-defined- twice path. For CUDA training, sed-swap ‘FFNFFICache` to `FFNFFICacheCuda` in lib/transformer.rb along with `TinyNN.` to `TinyNNCuda.` in feed_forward_ffi.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFFNFFICacheCuda

Returns a new instance of FFNFFICacheCuda.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/toy/ffi/tinynn_cuda.rb', line 38

def initialize
  @realized = false
  @t_seq    = 0
  @d_model  = 0
  @d_ff     = 0
  @sess     = TinyNNCuda.tnn_null_ptr
  @t_h      = TinyNNCuda.tnn_null_ptr
  @t_w1_t   = TinyNNCuda.tnn_null_ptr
  @t_w2_t   = TinyNNCuda.tnn_null_ptr
  @t_pre    = TinyNNCuda.tnn_null_ptr
  @t_hidden = TinyNNCuda.tnn_null_ptr
  @t_out    = TinyNNCuda.tnn_null_ptr
end

Instance Attribute Details

#d_ffObject

Returns the value of attribute d_ff.



34
35
36
# File 'lib/toy/ffi/tinynn_cuda.rb', line 34

def d_ff
  @d_ff
end

#d_modelObject

Returns the value of attribute d_model.



34
35
36
# File 'lib/toy/ffi/tinynn_cuda.rb', line 34

def d_model
  @d_model
end

#realizedObject

Returns the value of attribute realized.



34
35
36
# File 'lib/toy/ffi/tinynn_cuda.rb', line 34

def realized
  @realized
end

#sessObject

Returns the value of attribute sess.



34
35
36
# File 'lib/toy/ffi/tinynn_cuda.rb', line 34

def sess
  @sess
end

#t_hObject

Returns the value of attribute t_h.



34
35
36
# File 'lib/toy/ffi/tinynn_cuda.rb', line 34

def t_h
  @t_h
end

#t_hiddenObject

Returns the value of attribute t_hidden.



34
35
36
# File 'lib/toy/ffi/tinynn_cuda.rb', line 34

def t_hidden
  @t_hidden
end

#t_outObject

Returns the value of attribute t_out.



34
35
36
# File 'lib/toy/ffi/tinynn_cuda.rb', line 34

def t_out
  @t_out
end

#t_preObject

Returns the value of attribute t_pre.



34
35
36
# File 'lib/toy/ffi/tinynn_cuda.rb', line 34

def t_pre
  @t_pre
end

#t_seqObject

Returns the value of attribute t_seq.



34
35
36
# File 'lib/toy/ffi/tinynn_cuda.rb', line 34

def t_seq
  @t_seq
end

#t_w1_tObject

Returns the value of attribute t_w1_t.



34
35
36
# File 'lib/toy/ffi/tinynn_cuda.rb', line 34

def t_w1_t
  @t_w1_t
end

#t_w2_tObject

Returns the value of attribute t_w2_t.



34
35
36
# File 'lib/toy/ffi/tinynn_cuda.rb', line 34

def t_w2_t
  @t_w2_t
end

Instance Method Details

#realize_for(t_seq, d_model, d_ff) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/toy/ffi/tinynn_cuda.rb', line 52

def realize_for(t_seq, d_model, d_ff)
  @t_seq   = t_seq
  @d_model = d_model
  @d_ff    = d_ff

  @sess   = TinyNNCuda.tnn_session_new(1)
  @t_h    = TinyNNCuda.tnn_input_2d_f32(@sess, t_seq,  d_model)
  @t_w1_t = TinyNNCuda.tnn_input_2d_f32(@sess, d_ff,   d_model)
  @t_w2_t = TinyNNCuda.tnn_input_2d_f32(@sess, d_model, d_ff)

  @t_pre    = TinyNNCuda.tnn_matmul(@sess, @t_w1_t, @t_h)
  @t_hidden = TinyNNCuda.tnn_gelu(@sess, @t_pre)
  @t_out    = TinyNNCuda.tnn_matmul(@sess, @t_w2_t, @t_hidden)
  TinyNNCuda.tnn_set_output(@t_pre)
  TinyNNCuda.tnn_set_output(@t_hidden)
  TinyNNCuda.tnn_set_output(@t_out)
  TinyNNCuda.tnn_realize(@sess, @t_out)

  @realized = true
end