ftorch_model Module

Module for the FTorch torch_model type and associated procedures.

  • License FTorch is released under an MIT license. See the LICENSE file for details.

Uses

  • module~~ftorch_model~~UsesGraph module~ftorch_model ftorch_model iso_c_binding iso_c_binding module~ftorch_model->iso_c_binding module~ftorch_devices ftorch_devices module~ftorch_model->module~ftorch_devices module~ftorch_tensor ftorch_tensor module~ftorch_model->module~ftorch_tensor module~ftorch_types ftorch_types module~ftorch_model->module~ftorch_types iso_fortran_env iso_fortran_env module~ftorch_devices->iso_fortran_env module~ftorch_tensor->iso_c_binding module~ftorch_tensor->module~ftorch_devices module~ftorch_tensor->module~ftorch_types module~ftorch_tensor->iso_fortran_env module~ftorch_types->iso_fortran_env

Used by

  • module~~ftorch_model~~UsedByGraph module~ftorch_model ftorch_model module~ftorch ftorch module~ftorch->module~ftorch_model

Derived Types

type, public ::  torch_model

Type for holding a torch neural net (nn.Module).

Components

Type Visibility Attributes Name Initial
type(c_ptr), public :: p = c_null_ptr

pointer to the neural net in memory

Finalizations Procedures

final :: torch_model_delete

Type-Bound Procedures

procedure, public :: is_training => torch_model_is_training
procedure, public :: print_parameters => torch_model_print_parameters

Functions

public function torch_model_is_training(self) result(is_training)

Determines whether a model is set up for training

Arguments

Type IntentOptional Attributes Name
class(torch_model), intent(in) :: self

Model to query

Return Value logical

Whether the model is set up for training


Subroutines

public subroutine torch_model_delete(model)

Deallocates a TorchScript model

Arguments

Type IntentOptional Attributes Name
type(torch_model), intent(inout) :: model

Torch Model to deallocate

public subroutine torch_model_forward(model, input_tensors, output_tensors, requires_grad)

Performs a forward pass of the model with the input tensors

Arguments

Type IntentOptional Attributes Name
type(torch_model), intent(in) :: model

Model

type(torch_tensor), intent(in), dimension(:) :: input_tensors

Array of Input tensors

type(torch_tensor), intent(in), dimension(:) :: output_tensors

Returned output tensors

logical, intent(in), optional :: requires_grad

Whether gradients need to be computed for the created tensor

public subroutine torch_model_load(model, filename, device_type, device_index, requires_grad, is_training)

Loads a TorchScript nn.module (pre-trained PyTorch model saved with TorchScript)

Arguments

Type IntentOptional Attributes Name
type(torch_model), intent(out) :: model

Returned deserialized model

character(len=*), intent(in) :: filename

Filename of saved TorchScript model

integer(kind=c_int), intent(in) :: device_type

Device type the tensor will live on (torch_kCPU or a GPU device type)

integer(kind=c_int), intent(in), optional :: device_index

Device index for GPU devices

logical, intent(in), optional :: requires_grad

Whether gradients need to be computed for the created tensor

logical, intent(in), optional :: is_training

Whether the model is being trained, rather than evaluated

public subroutine torch_model_print_parameters(self)

Prints the parameters associated with a model NOTE: While viewing parameters in this way can be helpful for small toy models, it will produce large amounts of output for models with many, large, or high-dimensional parameters. In particular, tensors of 3 or more dimensions will be represented in terms of 2D arrays.

Arguments

Type IntentOptional Attributes Name
class(torch_model), intent(in) :: self

Model to print the parameters of