Class: Toy::LLM::ClassifyBatch

Inherits:
Object
  • Object
show all
Defined in:
lib/toy/llm/classify_batch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(num_classes, patch_flat, n_patches) ⇒ ClassifyBatch

Returns a new instance of ClassifyBatch.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/toy/llm/classify_batch.rb', line 42

def initialize(num_classes, patch_flat, n_patches)
  if num_classes <= 0
    raise "ClassifyBatch: num_classes must be positive, got " +
          num_classes.to_s
  end
  if patch_flat <= 0 || n_patches <= 0
    raise "ClassifyBatch: patch_flat/n_patches must be positive, got " +
          patch_flat.to_s + "/" + n_patches.to_s
  end
  @cb_classes    = num_classes
  @cb_patch_flat = patch_flat
  @cb_n_patches  = n_patches

  # The image Mat at the engine's expected shape; zero until fill!.
  @image = Mat.new(patch_flat, n_patches)

  # The class-token read position: 0 in every current ViT graph.
  @cls_idx = [0]

  # Zero labels at the final shape until fill!; hp is caller-owned
  # (Mat(1,7) from Toy::AdamW) — zero placeholder pins the type.
  @labels = Mat.new(1, num_classes)
  @hp     = Mat.new(1, 7)
end

Instance Attribute Details

#cb_classesObject

Returns the value of attribute cb_classes.



39
40
41
# File 'lib/toy/llm/classify_batch.rb', line 39

def cb_classes
  @cb_classes
end

#cb_n_patchesObject

Returns the value of attribute cb_n_patches.



39
40
41
# File 'lib/toy/llm/classify_batch.rb', line 39

def cb_n_patches
  @cb_n_patches
end

#cb_patch_flatObject

Returns the value of attribute cb_patch_flat.



39
40
41
# File 'lib/toy/llm/classify_batch.rb', line 39

def cb_patch_flat
  @cb_patch_flat
end

#cls_idxObject

Returns the value of attribute cls_idx.



39
40
41
# File 'lib/toy/llm/classify_batch.rb', line 39

def cls_idx
  @cls_idx
end

#hpObject

Returns the value of attribute hp.



39
40
41
# File 'lib/toy/llm/classify_batch.rb', line 39

def hp
  @hp
end

#imageObject

Returns the value of attribute image.



39
40
41
# File 'lib/toy/llm/classify_batch.rb', line 39

def image
  @image
end

#labelsObject

Returns the value of attribute labels.



39
40
41
# File 'lib/toy/llm/classify_batch.rb', line 39

def labels
  @labels
end

Instance Method Details

#fill!(patches, label) ⇒ Object

Validate + load one (image, label) record. ‘patches` is the flat f32 record from ToyImageLoader.read_image (length must be exactly patch_flat * n_patches); `label` is the class index from read_label (0…num_classes — read_label’s -1 short-read sentinel fails the range check loud). Returns nil.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/toy/llm/classify_batch.rb', line 72

def fill!(patches, label)
  record_f = @cb_patch_flat * @cb_n_patches
  if patches.length != record_f
    raise "ClassifyBatch#fill!: patches length " +
          patches.length.to_s + " != patch_flat*n_patches " +
          record_f.to_s
  end
  i = 0
  while i < record_f
    @image.flat[i] = patches[i]
    i = i + 1
  end
  @labels = Toy::Labels.one_hot_class(@cb_classes, label)
  nil
end