|
@@ -51,8 +51,10 @@ func New(c ml.Config) (model.Model, error) {
|
|
|
Types: c.Uints("tokenizer.ggml.token_type"),
|
|
|
BOS: int32(c.Uint("tokenizer.ggml.bos_token_id")),
|
|
|
AddBOS: c.Bool("tokenizer.ggml.add_bos_token", true),
|
|
|
- EOS: int32(c.Uint("tokenizer.ggml.eos_token_id")),
|
|
|
+ EOS: int32(1),
|
|
|
AddEOS: c.Bool("tokenizer.ggml.add_eos_token", false),
|
|
|
+ EOT: int32(106),
|
|
|
+ AddEOT: c.Bool("tokenizer.ggml.add_eot_token", false),
|
|
|
},
|
|
|
),
|
|
|
ImageProcessor: newImageProcessor(c),
|
|
@@ -109,35 +111,46 @@ func (m *Model) PostTokenize(ctx ml.Context, inputs []input.Input) ([]input.Inpu
|
|
|
|
|
|
for i := range inputs {
|
|
|
if inputs[i].Multimodal == nil {
|
|
|
- if len(images) > 0 {
|
|
|
- inputs[i].Multimodal = images[0].Multimodal
|
|
|
- inputs[i].MultimodalHash = images[0].MultimodalHash
|
|
|
- for j := 1; j < len(images); j++ {
|
|
|
+ for j := range images {
|
|
|
+ if j == 0 {
|
|
|
+ inputs[i].Multimodal = images[j].Multimodal
|
|
|
+ inputs[i].MultimodalHash = images[j].MultimodalHash
|
|
|
+ } else {
|
|
|
inputs[i].Multimodal = inputs[i].Multimodal.(ml.Tensor).Concat(ctx, images[j].Multimodal.(ml.Tensor), 3)
|
|
|
fnvHash.Reset()
|
|
|
binary.Write(fnvHash, binary.NativeEndian, inputs[i].MultimodalHash)
|
|
|
- binary.Write(fnvHash, binary.NativeEndian, inputs[j].MultimodalHash)
|
|
|
+ binary.Write(fnvHash, binary.NativeEndian, images[j].MultimodalHash)
|
|
|
inputs[i].MultimodalHash = fnvHash.Sum64()
|
|
|
}
|
|
|
- images = nil
|
|
|
}
|
|
|
+
|
|
|
+ images = nil
|
|
|
} else {
|
|
|
images = append(images, inputs[i])
|
|
|
inputs[i].Token = -1
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- inputs = slices.DeleteFunc(inputs, func(input input.Input) bool { return input.Token == -1 })
|
|
|
+ for i := range inputs {
|
|
|
+ if inputs[i].Token == -1 {
|
|
|
+ imageInputs := []input.Input{
|
|
|
+ {Token: 108}, // "\n\n"
|
|
|
+ {Token: 255999}, // "<start_of_image>""
|
|
|
+ }
|
|
|
+
|
|
|
+ // <image_soft_token>
|
|
|
+ imageInputs = append(imageInputs, slices.Repeat([]input.Input{{Token: 262144}}, 256)...)
|
|
|
+ // <end_of_image>
|
|
|
+ imageInputs = append(imageInputs, input.Input{Token: 256000})
|
|
|
+
|
|
|
+ inputs = append(inputs[:i], append(imageInputs, inputs[i+1:]...)...)
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
return inputs, nil
|
|
|
}
|
|
|
|
|
|
func (m *Model) Forward(ctx ml.Context, opts input.Options) (ml.Tensor, error) {
|
|
|
- var embeddings ml.Tensor
|
|
|
- if opts.Multimodal != nil {
|
|
|
- embeddings = opts.Multimodal[0].Multimodal.(ml.Tensor)
|
|
|
- }
|
|
|
-
|
|
|
inputs, err := ctx.Input().FromIntSlice(opts.Inputs, len(opts.Inputs))
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
@@ -153,7 +166,7 @@ func (m *Model) Forward(ctx ml.Context, opts input.Options) (ml.Tensor, error) {
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
- return m.TextModel.Forward(ctx, inputs, positions, embeddings, outputs, m.Cache), nil
|
|
|
+ return m.TextModel.Forward(ctx, inputs, positions, outputs, opts.Multimodal, m.Cache), nil
|
|
|
}
|
|
|
|
|
|
func init() {
|