Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.set_num_threads function

PyTorch torch.set_num_threads function


PyTorch torch reference manual PyTorch torch reference manual

torch.set_num_threads is a function in PyTorch used to set the number of intra-op threads. These threads are used to execute tensor operations (such as matrix multiplication, convolution, etc.) in parallel.

Function definition

torch.set_num_threads(num_threads)

Parameter Description


Usage examples

Example

import

torch

Set the number of threads

torch.

set_num_threads

(

4

)

print

(

"Number of threads set to:"

,

torch.

get_num_threads

(

)

)

Create a large matrix for testing

a

=

torch.

randn

(

1000

,

1000

)

b

=

torch.

randn

(

1000

,

1000

)

Matrix multiplication (uses the set number of threads)

c

=

torch.

matmul

(

a

,

b

)

print

(

"Matrix multiplication complete, result shape:"

,

c.

shape

)

Restore the default value

torch.

set_num_threads

(

1

)

The output is:

线程数已设置为: 4

矩阵乘法完成,结果形状: torch.Size([1000, 1000])


PyTorch torch reference manual PyTorch torch reference manual