Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Choose which device "cuda:x" or "cpu" to use #48

Closed
AhmedBahget2017 opened this issue Apr 12, 2021 · 3 comments
Closed

Choose which device "cuda:x" or "cpu" to use #48

AhmedBahget2017 opened this issue Apr 12, 2021 · 3 comments

Comments

@AhmedBahget2017
Copy link

AhmedBahget2017 commented Apr 12, 2021

After installing libtensorflow that supports both CPU and GPU. How to select which device to run on ?
Thanks in advance

package main

import (
	"fmt"

	tf "github.com/galeone/tensorflow/tensorflow/go"
	tg "github.com/galeone/tfgo"
)

func describe(i interface{}) {
	fmt.Printf("(%v, %T)\n", i, i)
}

func main() {
	model := tg.LoadModel("/home/yafoz/Desktop/go/src/goTensorflow/tfModel", []string{"serve"}, nil)

	fakeInput, _ := tf.NewTensor([2][28][28][1]float32{})
	results := model.Exec([]tf.Output{
		model.Op("StatefulPartitionedCall", 0),
	}, map[tf.Output]*tf.Tensor{
		model.Op("serving_default_inputs_input", 0): fakeInput,
	})

	fmt.Println(len(results))
	for i := 0; i < 1; i++ {
		predictions := results[i]
		dummy := predictions.Value()
		describe(dummy)
		foo, _ := dummy.([][]float32)
		fmt.Println("%V", foo[0][1])
		fmt.Println(predictions.Shape())
		fmt.Println("-----------------")
	}
}
@galeone
Copy link
Owner

galeone commented Apr 13, 2021

Hi, I recommend doing this operation in Python before exporting the saved model.

# model is your model definition 
@tf.function
def inference(input, on_gpu):
  if tf.equal(on_gpu, True):
    with tf.device("/gpu:0"):
      return model(input)
  return model(input)

@galeone
Copy link
Owner

galeone commented Apr 19, 2021

Closing this since the answer seems sufficient to fix the problem

@chenyanchen
Copy link

Hi, I recommend doing this operation in Python before exporting the saved model.

# model is your model definition 
@tf.function
def inference(input, on_gpu):
  if tf.equal(on_gpu, True):
    with tf.device("/gpu:0"):
      return model(input)
  return model(input)

I'm new for TensorFlow.

Is that mean: use CPU or GPU are depend on saved model? Not a optional for server?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants