Comments (9)
A fix #20 done on master
and v0.3.8
. Now you can cast output to []ts.Tensor
as below. If that what you expect, please close this issue.
package main
import (
"fmt"
"log"
"github.com/sugarme/gotch"
ts "github.com/sugarme/gotch/tensor"
)
func main() {
input_ids, _ := ts.Ones([]int64{2, 73}, gotch.Int64, gotch.CPU)
input_mask, _ := ts.Ones([]int64{2, 73}, gotch.Int64, gotch.CPU)
input_ids_ival := ts.NewIValue(*input_ids)
input_mask_ival := ts.NewIValue(*input_mask)
inputs := []ts.IValue{*input_ids_ival, *input_mask_ival}
model, err := ts.ModuleLoad("distilled_scripted.pt")
if err != nil {
log.Fatal(err)
}
prediction, err := model.ForwardIs(inputs)
if err != nil {
log.Fatal(err)
}
xs := prediction.Value().([]ts.Tensor)
for _, x := range xs {
fmt.Printf("%i", &x)
}
}
// Output:
TENSOR INFO:
Shape: [2 6]
DType: float32
Device: {CPU 1}
Defined: true
TENSOR INFO:
Shape: [2 70 37]
DType: float32
Device: {CPU 1}
Defined: true
TENSOR INFO:
Shape: [2 70]
DType: int64
Device: {CPU 1}
Defined: true
from gotch.
Just managed to make the multi-inputs work.
input_ids, _ := ts.Ones([]int64{2,73}, gotch.Int64, gotch.CPU)
input_mask, _ := ts.Ones([]int64{2,73}, gotch.Int64, gotch.CPU)
inputs := []ts.Tensor{*input_ids,*input_mask}
model, _ := ts.ModuleLoad("distilled_scripted.pt")
predictTmp,err := model.ForwardTs(inputs) //ForwardTs(Sclice)
fmt.Println(err)
fmt.Printf("%i", predictTmp)
However I'm still clueless for the multi-outputs. I coudn't find any function that allows the output to be a slice.
Libtorch API Error: forward did not return a tensor
from gotch.
Although JIT API has not been complete yet, gotch provides 2 APIs to forward pass CModule:
- func (cm *CModule) ForwardTs(tensors []Tensor) (*Tensor, error)
- func (cm *CModule) ForwardIs(ivalues []IValue) (*IValue, error)
Have a look at JIT unit test for how to use.
Also, there 2 separate examples of using JIT in example folder: inference and train
Not sure if that what you're after?
from gotch.
Just managed to make the multi-inputs work.
input_ids, _ := ts.Ones([]int64{2,73}, gotch.Int64, gotch.CPU) input_mask, _ := ts.Ones([]int64{2,73}, gotch.Int64, gotch.CPU) inputs := []ts.Tensor{*input_ids,*input_mask} model, _ := ts.ModuleLoad("distilled_scripted.pt") predictTmp,err := model.ForwardTs(inputs) //ForwardTs(Sclice) fmt.Println(err) fmt.Printf("%i", predictTmp)
However I'm still clueless for the multi-outputs. I coudn't find any function that allows the output to be a slice.
Libtorch API Error: forward did not return a tensor
Have you handled error in model, _ := ts.ModuleLoad("distilled_scripted.pt")
? This error coming from C and just make sure the JIT module is loaded properly.
from gotch.
Hi, Thanks for the hints!
the error was coming from ForwardTs
, because the python signature of the model looks like this:
forward(input_ids, input_mask) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]
which ForwardTs
can't handle.
Converting the input tensors to IValue
and using ForwardIs
fixed it:
input_ids, _ := ts.Ones([]int64{2,73}, gotch.Int64, gotch.CPU)
input_mask, _ := ts.Ones([]int64{2,73}, gotch.Int64, gotch.CPU)
input_ids_ival := ts.NewIValue(*input_ids)
input_mask_ival := ts.NewIValue(*input_mask)
inputs := []ts.IValue{*input_ids_ival,*input_mask_ival}
model, _ := ts.ModuleLoad("distilled_scripted.pt")
prediction,err := model.ForwardIs(inputs)
Now I would just figure out how to get the three Tensors back out of the prediction
IValue
.
from gotch.
prediction
is IValue
type. You may try to use func (iv *IValue) Value() interface{} and cast return value to your expected type.
Something like
var output []ts.Tensor
output = prediction.Value().([]ts.Tensor)
from gotch.
prediction.Kind()
gives []tensor.IValue
, that should be ok I suppose.
The expected type casting however is not doing it.
panic: interface conversion: interface {} is []interface {}, not []tensor.Tensor
I'm really new to Go, as you can tell - and the concept of interface
especially. I actually just have a codebase that uses tfgo and I need to swap out the core model for pytorch.
Thanks a lot for your help!
from gotch.
I think the issue is related to type casting inside gotch JIT itself and it may fall into one of the unsupported cases.
As you mentioned output is Tuple[torch.Tensor, torch.Tensor, torch.Tensor]
, gotch may cast output to 'GenericList' and end up with []inferface{}
instead of 'TensorList' which is []Tensor
.
Can you try to cast in []interface{}
type. I would suspect some error: IValueFromC method call - GenericList case: Unsupported item type...
.
var output []interface{}
output = prediction.Value().([]interface{})
// Or just
output := prediction.Value()
If you can share your JIT module .pt
or any simplified version, I can trace up and may add more support to the library.
from gotch.
Hi,
gave it a try:
Casting works, yielding output
type: []interface {}
as expected.
Here's the jit model:
https://mega.nz/file/uZ0DVQ7b#oDDerv_8mJmj1ypC_CX8OaWBxfYe4dXDvHGVgOiwCks
It's rather large (essentially Bert+custom prediction head), but it's probably better to check the real thing. As stated, it takes two Int64
tensors of size (batch_size, 73)
as inputs.
Update:
I changed the signature in Python to List[torch.Tensor, torch.Tensor, torch.Tensor]
. Now
output := prediction.Value().([]ts.Tensor)
works. Easy fix for the problem, as there is no advantage of using Tuple over List really.
from gotch.
Related Issues (20)
- v2.0 support HOT 3
- Possible Memory Leak From C.malloc(0) HOT 3
- how to load model pytorch_model.bin HOT 5
- Indexing documentation HOT 1
- Can't build project with gotch v0.9.0 HOT 3
- Production use for gotch just for inference HOT 3
- Concurrency issue in generating tensor name in newTensor HOT 3
- Can memory leak in tensor-generated.go because of malloc(0) ? HOT 8
- Memory Leak in JIT Model under Multi-Goroutine Environment HOT 9
- how can i convert gocv.Mat image data to Tensor ? HOT 2
- `*ts.CModule` does not implement `ts.Module` interface correctly
- Cannot Run the Application with Cgo HOT 1
- Cannot Run the Application using Libtorch 2.1 (CPU) Docker Image
- Memory Leak HOT 5
- Mac Support HOT 4
- go mod tidy trying to search for 'github.com/sugarme/gotch/tensor' which does not exist HOT 3
- error while reading libtch HOT 4
- Q) Is there a function or method to clear the cached memory? HOT 1
- Float64Values() shows an error 'Unsupported Go type: []float64' HOT 2
- Releasing tensor causes segmentation fault error
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gotch.