ExTorch.JIT
(extorch v0.4.0)
Copy Markdown
TorchScript model loading, inference, and management.
This module provides functions to load pre-trained TorchScript (.pt) models,
run inference, inspect model structure, and manage model lifecycle.
Example
model = ExTorch.JIT.load("model.pt")
input = ExTorch.randn({1, 3, 224, 224})
output = ExTorch.JIT.forward(model, [input])
Summary
Functions
Get named buffers of a model.
Set a model to evaluation mode.
Run the forward method on a model.
Invoke a named method on a model.
Load a TorchScript model from a file.
Get the names of all methods on a model.
Get names of submodules of a model.
Get named parameters of a model.
Save a TorchScript model to a file.
Move a model to a different device.
Set a model to training mode.
Functions
@spec buffers(ExTorch.JIT.Model.t()) :: [{String.t(), ExTorch.Tensor.t()}]
Get named buffers of a model.
Returns
A list of {name, tensor} tuples.
@spec eval(ExTorch.JIT.Model.t()) :: :ok
Set a model to evaluation mode.
@spec forward(ExTorch.JIT.Model.t(), [ExTorch.Tensor.t()]) :: term()
Run the forward method on a model.
Arguments
model: The loaded model.inputs: A list of input tensors.
Returns
The model output, which can be a tensor, tuple, list, map, or scalar depending on the model's return type.
Examples
input = ExTorch.randn({1, 784})
output = ExTorch.JIT.forward(model, [input])
@spec invoke(ExTorch.JIT.Model.t(), String.t(), [ExTorch.Tensor.t()]) :: term()
Invoke a named method on a model.
Arguments
model: The loaded model.method_name: Name of the method to invoke.inputs: A list of input tensors.
Returns
The method output.
@spec load( String.t(), keyword() ) :: ExTorch.JIT.Model.t()
Load a TorchScript model from a file.
Arguments
path: Path to the.ptfile.opts: Keyword list of options.:device- Device to load the model onto (default::cpu).
Returns
A %ExTorch.JIT.Model{} struct.
Examples
model = ExTorch.JIT.load("model.pt")
model = ExTorch.JIT.load("model.pt", device: {:cuda, 0})
@spec methods(ExTorch.JIT.Model.t()) :: [String.t()]
Get the names of all methods on a model.
Returns
A list of method name strings.
@spec modules(ExTorch.JIT.Model.t()) :: [String.t()]
Get names of submodules of a model.
Returns
A list of submodule name strings.
@spec parameters(ExTorch.JIT.Model.t()) :: [{String.t(), ExTorch.Tensor.t()}]
Get named parameters of a model.
Returns
A list of {name, tensor} tuples.
@spec save(ExTorch.JIT.Model.t(), String.t()) :: :ok
Save a TorchScript model to a file.
Arguments
model: The model to save.path: Path to save the.ptfile.
@spec to(ExTorch.JIT.Model.t(), ExTorch.Device.device()) :: ExTorch.JIT.Model.t()
Move a model to a different device.
Arguments
model: The model to move.device: Target device (e.g.,:cpu,{:cuda, 0}).
Returns
A new %ExTorch.JIT.Model{} on the target device.
@spec train(ExTorch.JIT.Model.t()) :: :ok
Set a model to training mode.