Skip to content

Commit 2e34127

Browse files
change: use value in batch results and cast to concrete type in postprocessing
1 parent ed4ef41 commit 2e34127

7 files changed

Lines changed: 23 additions & 26 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/viant/afs v1.25.1
1313
github.com/viant/afsc v1.9.3
1414
github.com/yalue/onnxruntime_go v1.12.0
15-
golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e
15+
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0
1616
)
1717

1818
require (

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ github.com/yalue/onnxruntime_go v1.12.0 h1:UtrSZOV9cY9j8ualjiakzRSn7H+bvu6QyCHAA
5757
github.com/yalue/onnxruntime_go v1.12.0/go.mod h1:b4X26A8pekNb1ACJ58wAXgNKeUCGEAQ9dmACut9Sm/4=
5858
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
5959
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
60-
golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e h1:I88y4caeGeuDQxgdoFPUq097j7kNfw6uvuiNxUBfcBk=
61-
golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
60+
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 h1:e66Fs6Z+fZTbFBAxKfP3PALWBtpfqks2bwGcexMxgtk=
61+
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0/go.mod h1:2TbTHSBQa924w8M6Xs1QcRcFwyucIwBGpK1p2f1YFFY=
6262
golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs=
6363
golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
6464
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

pipelines/featureExtraction.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,9 @@ func (p *FeatureExtractionPipeline) Postprocess(batch *PipelineBatch) (*FeatureE
225225
tokenEmbeddings := make([][]float32, maxSequenceLength)
226226
tokenEmbeddingsCounter := 0
227227
batchInputCounter := 0
228+
outputTensor := batch.OutputValues[0].(*ort.Tensor[float32])
228229

229-
for _, result := range batch.OutputTensors[0].GetData() {
230+
for _, result := range outputTensor.GetData() {
230231
outputEmbedding[outputEmbeddingCounter] = result
231232
if outputEmbeddingCounter == int(embeddingDimension)-1 {
232233
// we gathered one embedding

pipelines/pipeline.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,19 @@ type tokenizedInput struct {
8383
// PipelineBatch represents a batch of inputs that runs through the pipeline.
8484
type PipelineBatch struct {
8585
Input []tokenizedInput
86-
InputTensors []*ort.Tensor[int64]
8786
MaxSequenceLength int
88-
OutputTensors []*ort.Tensor[float32]
87+
InputValues []ort.Value
88+
OutputValues []ort.Value
8989
}
9090

9191
func (b *PipelineBatch) Destroy() error {
92-
destroyErrors := make([]error, 0, len(b.InputTensors)+len(b.OutputTensors))
92+
destroyErrors := make([]error, 0, len(b.InputValues)+len(b.OutputValues))
9393

94-
for _, tensor := range b.InputTensors {
94+
for _, tensor := range b.InputValues {
9595
destroyErrors = append(destroyErrors, tensor.Destroy())
9696
}
9797

98-
for _, tensor := range b.OutputTensors {
98+
for _, tensor := range b.OutputValues {
9999
destroyErrors = append(destroyErrors, tensor.Destroy())
100100
}
101101
return errors.Join(destroyErrors...)
@@ -231,7 +231,7 @@ func createInputTensors(batch *PipelineBatch, inputsMeta []ort.InputOutputInfo)
231231
tensorSize := len(batch.Input) * (batch.MaxSequenceLength)
232232
batchSize := int64(len(batch.Input))
233233

234-
inputTensors := make([]*ort.Tensor[int64], len(inputsMeta))
234+
inputTensors := make([]ort.Value, len(inputsMeta))
235235
var tensorCreationErr error
236236

237237
for i, inputMeta := range inputsMeta {
@@ -263,7 +263,7 @@ func createInputTensors(batch *PipelineBatch, inputsMeta []ort.InputOutputInfo)
263263
return tensorCreationErr
264264
}
265265
}
266-
batch.InputTensors = inputTensors
266+
batch.InputValues = inputTensors
267267
return nil
268268
}
269269

@@ -297,8 +297,7 @@ func runSessionOnBatch(batch *PipelineBatch, session *ort.DynamicAdvancedSession
297297
maxSequenceLength := int64(batch.MaxSequenceLength)
298298

299299
// allocate vectors with right dimensions for the output
300-
outputTensors := make([]*ort.Tensor[float32], len(outputs))
301-
arbitraryOutputTensors := make([]ort.ArbitraryTensor, len(outputs))
300+
outputTensors := make([]ort.Value, len(outputs))
302301
var outputCreationErr error
303302

304303
for outputIndex, meta := range outputs {
@@ -326,22 +325,15 @@ func runSessionOnBatch(batch *PipelineBatch, session *ort.DynamicAdvancedSession
326325
if outputCreationErr != nil {
327326
return outputCreationErr
328327
}
329-
arbitraryOutputTensors[outputIndex] = ort.ArbitraryTensor(outputTensors[outputIndex])
330328
}
331329

332-
// Run Onnx model
333-
arbitraryInputTensors := make([]ort.ArbitraryTensor, len(batch.InputTensors))
334-
for i, t := range batch.InputTensors {
335-
arbitraryInputTensors[i] = ort.ArbitraryTensor(t)
336-
}
337-
338-
errOnnx := session.Run(arbitraryInputTensors, arbitraryOutputTensors)
330+
errOnnx := session.Run(batch.InputValues, outputTensors)
339331
if errOnnx != nil {
340332
return errOnnx
341333
}
342334

343335
// store resulting tensors
344-
batch.OutputTensors = outputTensors
336+
batch.OutputValues = outputTensors
345337
return nil
346338
}
347339

pipelines/textClassification.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ func (p *TextClassificationPipeline) Forward(batch *PipelineBatch) error {
240240
}
241241

242242
func (p *TextClassificationPipeline) Postprocess(batch *PipelineBatch) (*TextClassificationOutput, error) {
243-
outputTensor := batch.OutputTensors[0]
243+
outputValue := batch.OutputValues[0]
244244
outputDims := p.OutputsMeta[0].Dimensions
245245
nLogit := outputDims[len(outputDims)-1]
246246
output := make([][]float32, len(batch.Input))
@@ -257,7 +257,9 @@ func (p *TextClassificationPipeline) Postprocess(batch *PipelineBatch) (*TextCla
257257
return nil, fmt.Errorf("aggregation function %s is not supported", p.AggregationFunctionName)
258258
}
259259

260-
for _, result := range outputTensor.GetData() {
260+
resultTensor := outputValue.(*ort.Tensor[float32])
261+
262+
for _, result := range resultTensor.GetData() {
261263
inputVector[vectorCounter] = result
262264
if vectorCounter == int(nLogit)-1 {
263265
output[inputCounter] = aggregationFunction(inputVector)

pipelines/tokenClassification.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ func (p *TokenClassificationPipeline) Postprocess(batch *PipelineBatch) (*TokenC
256256
// construct the output vectors by gathering the logits,
257257
// however discard the embeddings of the padding tokens so that the output vector length
258258
// for an input is equal to the number of original tokens
259-
for _, result := range batch.OutputTensors[0].GetData() {
259+
outputTensor := batch.OutputValues[0].(*ort.Tensor[float32])
260+
for _, result := range outputTensor.GetData() {
260261
tokenVector[tokenVectorCounter] = result
261262
if tokenVectorCounter == tokenLogitsDim-1 {
262263
// raw result vector for token is now complete

pipelines/zeroShotClassification.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,8 @@ func (p *ZeroShotClassificationPipeline) RunPipeline(inputs []string) (*ZeroShot
444444
if e := errors.Join(runErrors...); e != nil {
445445
return nil, e
446446
}
447-
sequenceTensors = append(sequenceTensors, batch.OutputTensors[0].GetData())
447+
outputTensor := batch.OutputValues[0].(*ort.Tensor[float32])
448+
sequenceTensors = append(sequenceTensors, outputTensor.GetData())
448449
}
449450
outputTensors = append(outputTensors, sequenceTensors)
450451
}

0 commit comments

Comments
 (0)