{"id":27373434,"url":"https://github.com/maxrmorrison/torchutil","last_synced_at":"2025-04-13T11:14:50.477Z","repository":{"id":199964018,"uuid":"703073324","full_name":"maxrmorrison/torchutil","owner":"maxrmorrison","description":"General utilities for developing deep learning projects using PyTorch","archived":false,"fork":false,"pushed_at":"2024-11-07T20:12:37.000Z","size":50,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-10T14:52:58.422Z","etag":null,"topics":["checkpoint","deep-learning","monitor","profile","pytorch"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maxrmorrison.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-10-10T14:43:37.000Z","updated_at":"2024-11-07T20:12:41.000Z","dependencies_parsed_at":"2023-10-14T20:14:14.465Z","dependency_job_id":"3821ee14-665a-4b26-8659-b1560bed51eb","html_url":"https://github.com/maxrmorrison/torchutil","commit_stats":null,"previous_names":["maxrmorrison/torchutil"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxrmorrison%2Ftorchutil","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxrmorrison%2Ftorchutil/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxrmorrison%2Ftorchutil/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxrmorrison%2Ftorchutil/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maxrmorrison","download_url":"https://codeload.github.com/maxrmorrison/torchutil/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248459340,"owners_count":21107293,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["checkpoint","deep-learning","monitor","profile","pytorch"],"created_at":"2025-04-13T11:14:49.832Z","updated_at":"2025-04-13T11:14:50.466Z","avatar_url":"https://github.com/maxrmorrison.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003etorchutil\u003c/h1\u003e\n\u003cdiv align=\"center\"\u003e\n\n[![PyPI](https://img.shields.io/pypi/v/torchutil.svg)](https://pypi.python.org/pypi/torchutil)\n[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)\n[![Downloads](https://static.pepy.tech/badge/torchutil)](https://pepy.tech/project/torchutil)\n\nGeneral utilities for developing deep learning projects using PyTorch\n\n`pip install torchutil`\n\u003c/div\u003e\n\n\n## Table of contents\n\n- [Checkpoint](#checkpoint)\n    * [`torchutil.checkpoint.best_path`](#torchutilcheckpointbest_path)\n    * [`torchutil.checkpoint.latest_path`](#torchutilcheckpointlatest_path)\n    * [`torchutil.checkpoint.load`](#torchutilcheckpointload)\n    * [`torchutil.checkpoint.save`](#torchutilcheckpointsave)\n- [Cuda](#cuda)\n    * [`torchutil.cuda.utilization`](#torchutilcudautilization)\n- [Download](#download)\n    * [`torchutil.download.file`](#torchutildownloadfile)\n    * [`torchutil.download.tarbz2`](#torchutildownloadtarbz2)\n    * [`torchutil.download.targz`](#torchutildownloadtargz)\n    * [`torchutil.download.zip`](#torchutildownloadzip)\n- [Gradients](#gradients)\n    * [`torchutil.gradients.stats`](#torchutilgradientsstats)\n- [Inference](#inference)\n    * [`torchutil.inference.context`](#torchutilinferencecontext)\n- [Iterator](#iterator)\n    * [`torchutil.iterator`](#torchutiliterator)\n    * [`torchutil.multiprocess_iterator`](#torchutilmultiprocess_iterator)\n- [Mask](#mask)\n    * [`torchutil.mask.from_lengths`](#torchutilmaskfromlengths)\n- [Metrics](#metrics)\n    * [`torchutil.metrics.Accuracy`](#torchutilmetricsaccuracy)\n    * [`torchutil.metrics.Average`](#torchutilmetricsaverage)\n    * [`torchutil.metrics.F1`](#torchutilmetricsf1)\n    * [`torchutil.metrics.L1`](#torchutilmetricsl1)\n    * [`torchutil.metrics.MeanStd`](#torchutilmetricsmeanstd)\n    * [`torchutil.metrics.Precision`](#torchutilmetricsprecision)\n    * [`torchutil.metrics.PearsonCorrelation`](#torchutilmetricspearsoncorrelation)\n    * [`torchutil.metrics.Recall`](#torchutilmetricsrecall)\n    * [`torchutil.metrics.RMSE`](#torchutilmetricsrmse)\n- [Notify](#notify)\n    * [`torchutil.notify`](#torchutilnotify)\n- [Paths](#paths)\n    * [`torchutil.paths.chdir`](#torchutilpathschdir)\n    * [`torchutil.paths.measure`](#torchutilpathsmeasure)\n    * [`torchutil.paths.purge`](#torchutilpathspurge)\n- [Tensorboard](#tensorboard)\n    * [`torchutil.tensorboard.update`](#torchutiltensorboardupdate)\n- [Time](#time)\n    * [`torchutil.time.context`](#torchutiltimecontext)\n    * [`torchutil.time.results`](#torchutiltimeresults)\n    * [`torchutil.time.reset`](#torchutiltimereset)\n\n\n## Checkpoint\n\n```python\nimport torch\nimport torchutil\n\n# Checkpoint location\nfile = 'model.pt'\n\n# Initialize PyTorch model\nmodel = torch.nn.Sequential(torch.nn.Conv1d())\n\n# Initialize optimizer\noptimizer = torch.nn.Adam(model.parameters())\n\n# Save\ntorchutil.checkpoint.save(file, model, optimizer, step=0, epoch=0)\n\n# Load for training\nmodel, optimizer, state = torchutil.checkpoint.load(file, model, optimizer)\nstep, epoch = state['step'], state['epoch']\n\n# Load for inference\nmodel, *_ = torchutil.checkpoint.load(file, model, optimizer)\n```\n\n\n### `torchutil.checkpoint.best_path`\n\n```python\ndef best_path(\n    directory: Union[str, bytes, os.PathLike],\n    glob: str = '*.pt',\n    best_fn: Callable = highest_score\n) -\u003e Tuple[Union[str, bytes, os.PathLike], float]:\n    \"\"\"Retrieve the path to the best checkpoint\n\n    Arguments\n        directory - The directory to search for checkpoint files\n        glob - The regular expression matching checkpoints\n        best_fn - Takes a list of checkpoint paths and returns the latest\n                  Default assumes checkpoint names are training step count.\n\n    Returns\n        best_file - The filename of the checkpoint with the best score\n        best_score - The corresponding score\n    \"\"\"\n```\n\n\n### `torchutil.checkpoint.latest_path`\n\n```python\ndef latest_path(\n        directory: Union[str, bytes, os.PathLike],\n        glob: str = '*.pt',\n        latest_fn: Callable = largest_number_filename,\n    ) -\u003e Union[str, bytes, os.PathLike]:\n    \"\"\"Retrieve the path to the most recent checkpoint in a directory\n\n    Arguments\n        directory - The directory to search for checkpoint files\n        glob - The regular expression matching checkpoints\n        latest_fn - Takes a list of checkpoint paths and returns the latest.\n                    Default assumes checkpoint names are training step count.\n\n    Returns\n        The latest checkpoint in directory according to latest_fn\n    \"\"\"\n```\n\n\n### `torchutil.checkpoint.load`\n\n```python\ndef load(\n    file: Union[str, bytes, os.PathLike],\n    model: torch.nn.Module,\n    optimizer: Optional[torch.optim.Optimizer] = None,\n    map_location: str = 'cpu') -\u003e Tuple[\n        torch.nn.Module,\n        Union[None, torch.optim.Optimizer],\n        Dict\n    ]:\n    \"\"\"Load model checkpoint\n\n    Arguments\n        file - The checkpoint file\n        model - The PyTorch model\n        optimizer - Optional PyTorch optimizer for training\n        map_location - The device to load the checkpoint on\n\n    Returns\n        model - The model with restored weights\n        optimizer - Optional optimizer with restored parameters\n        state - Additional values that the user defined during save\n    \"\"\"\n```\n\n### `torchutil.checkpoint.save`\n\n```python\ndef save(\n    file: Union[str, bytes, os.PathLike],\n    model: torch.nn.Module,\n    optimizer: torch.optim.Optimizer,\n    **kwargs):\n    \"\"\"Save training checkpoint to disk\n\n    Arguments\n        file - The checkpoint file\n        model - The PyTorch model\n        optimizer - The PyTorch optimizer\n        kwargs - Additional values to save\n    \"\"\"\n```\n\n## Cuda\n\n```python\nimport torch\nimport torchutil\n\n# Directory to write Tensorboard files\ndirectory = 'tensorboard'\n\n# Training step\nstep = 0\n\n# Log VRAM utilization in MB to Tensorboard\ntorchutil.tensorboard.update(\n    directory,\n    step,\n    scalars=torchutil.cuda.utilization(torch.device('cuda:0'), 'MB'))\n```\n\n\n### `torchutil.cuda.utilization`\n\n```python\ndef utilization(device: torch.Device, unit: str ='B') -\u003e Dict[str, float]:\n    \"\"\"Get the current VRAM utilization of a specified device\n\n    Arguments\n        device\n            The device to query for VRAM utilization\n        unit\n            Unit of memory utilization (bytes to terabytes); default bytes\n\n    Returns\n        Allocated and reserved VRAM utilization in the specified unit\n    \"\"\"\n```\n\n\n## Download\n\n### `torchutil.download.file`\n\n```python\ndef file(url: 'str', path: Union[str, bytes, os.PathLike]):\n    \"\"\"Download file from url\n\n    Arguments\n        url - The URL to download\n        path - The location to save results\n    \"\"\"\n```\n\n\n### `torchutil.download.tarbz2`\n\n```python\ndef tarbz2(url: 'str', path: Union[str, bytes, os.PathLike]):\n    \"\"\"Download and extract tar bz2 file to location\n\n    Arguments\n        url - The URL to download\n        path - The location to save results\n    \"\"\"\n```\n\n\n### `torchutil.download.targz`\n\n```python\ndef targz(url: 'str', path: Union[str, bytes, os.PathLike]):\n    \"\"\"Download and extract tar gz file to location\n\n    Arguments\n        url - The URL to download\n        path - The location to save results\n    \"\"\"\n```\n\n\n### `torchutil.download.zip`\n\n```python\ndef zip(url: 'str', path: Union[str, bytes, os.PathLike]):\n    \"\"\"Download and extract zip file to location\n\n    Arguments\n        url - The URL to download\n        path - The location to save results\n    \"\"\"\n```\n\n\n## Gradients\n\n```python\nimport torch\nimport torchutil\n\n# Directory to write Tensorboard files\ndirectory = 'tensorboard'\n\n# Training step\nstep = 0\n\n# Setup model and optimizer\n# ...\n\n# Compute forward pass and loss\n# ...\n\n# Zero gradients\noptimizer.zero_grad()\n\n# Compute gradients\nloss.backward()\n\n# Monitor gradients on tensorboard\ntorchutil.tensorboard.update(\n    directory,\n    step,\n    scalars=torchutil.gradients.stats(model))\n\n# Apply gradient update\noptimizer.step()\n```\n\n\n### `torchutil.gradients.stats`\n\n```python\ndef stats(model: torch.nn.Module) -\u003e Dict[str, float]:\n    \"\"\"Get gradient statistics\n\n    Arguments\n        model\n            The torch model\n\n    Returns\n        The L2 norm, maximum, and minimum gradients\n    \"\"\"\n```\n\n\n## Inference\n\n### `torchutil.inference.context`\n\n```python\n@contextlib.contextmanager\ndef context(model: torch.nn.Module, autocast: bool = True) -\u003e None:\n    \"\"\"Inference-time handling of model training flag and optimizations\n\n    Arguments\n        model\n            The torch model performing inference\n        autocast\n            Whether to use mixed precision\n    \"\"\"\n```\n\n\n## Iterator\n\n```python\nimport time\nimport torchutil\n\ndef wait(seconds):\n    time.sleep(seconds)\n\nn = 8\niterable = range(n)\n\n# Monitor single-process job\nfor i in torchutil.iterator(iterable, message='single-process'):\n    wait(i)\n\n# Monitor multi-process job\ntorchutil.multiprocess_iterator(wait, iterable, message='multi-process')\n```\n\n\n### `torchutil.iterator`\n\n```python\ndef iterator(\n    iterable: Iterable,\n    message: Optional[str] = None,\n    initial: int = 0,\n    total: Optional[int] = None\n) -\u003e Iterable:\n    \"\"\"Create a tqdm iterator\n\n    Arguments\n        iterable\n            Items to iterate over\n        message\n            Static message to display\n        initial\n            Position to display corresponding to index zero of iterable\n        total\n            Length of the iterable; defaults to len(iterable)\n\n    Returns\n        Monitored iterable\n    \"\"\"\n```\n\n\n### `torchutil.multiprocess_iterator`\n\n```python\ndef multiprocess_iterator(\n    process: Callable,\n    iterable: Iterable,\n    message: Optional[str] = None,\n    initial: int = 0,\n    total: Optional[int] = None,\n    num_workers: int = os.cpu_count(),\n    worker_chunk_size: Optional[int] = None\n) -\u003e List:\n    \"\"\"Create a multiprocess tqdm iterator\n\n    Arguments\n        process\n            The single-argument function called by each multiprocess worker\n        iterable\n            Items to iterate over\n        message\n            Static message to display\n        initial\n            Position to display corresponding to index zero of iterable\n        total\n            Length of the iterable; defaults to len(iterable)\n        num_workers\n            Multiprocessing pool size; defaults to number of logical CPU cores\n        worker_chunk_size\n            Number of items sent to each multiprocessing worker\n\n    Returns\n        Return values of calling process on each item, in original order\n    \"\"\"\n```\n\n\n## Mask\n\n### `torchutil.mask.from_lengths`\n\n```python\ndef from_lengths(lengths: torch.Tensor) -\u003e torch.Tensor:\n    \"\"\"Create boolean mask from sequence lengths\n\n    Arguments\n        lengths\n            The integer-type sequence lengths\n\n    Returns\n        mask\n            The boolean-type sequence mask\n    \"\"\"\n```\n\n\n## Metrics\n\n```python\nimport torch\nimport torchutil\n\n# Define a custom, batch-updating loss metric\nclass Loss(torchutil.metrics.Average):\n    def update(self, predicted, target):\n\n        # Compute your loss and the number of elements to average over\n        loss = ...\n        count = ...\n\n        super().update(loss, count)\n\n# Instantiate metrics\nloss = Loss()\nrmse = torchutil.metrics.RMSE()\n\n# Generator that produces batches of predicted and target tensors\niterable = ...\n\n# Update metrics\nfor predicted_tensor, target_tensor in iterable:\n    loss.update(predicted_tensor, target_tensor)\n    rmse.update(predicted_tensor, target_tensor)\n\n# Get results\nprint('loss': loss())\nprint('rmse': rmse())\n```\n\n\n### `torchutil.metrics.Accuracy`\n\n```python\nclass Accuracy(Metric):\n    \"\"\"Batch-updating accuracy metric\"\"\"\n\n    def __call__(self)-\u003e float:\n        \"\"\"Retrieve the current accuracy value\n\n        Returns\n            The current accuracy value\n        \"\"\"\n\n    def update(self, predicted: torch.Tensor, target: torch.Tensor) -\u003e None:\n        \"\"\"Update accuracy\n\n        Arguments\n            predicted\n                The model prediction\n            target\n                The corresponding ground truth\n        \"\"\"\n\n    def reset(self) -\u003e None:\n        \"\"\"Reset accuracy\"\"\"\n```\n\n\n### `torchutil.metrics.Average`\n\n```python\nclass Average(Metric):\n    \"\"\"Batch-updating average metric\"\"\"\n\n    def __call__(self)-\u003e float:\n        \"\"\"Retrieve the current average value\n\n        Returns\n            The current average value\n        \"\"\"\n\n    def update(self, values: torch.Tensor, count: int) -\u003e None:\n        \"\"\"Update running average\n\n        Arguments\n            values\n                The values to average\n            target\n                The number of values\n        \"\"\"\n\n    def reset(self) -\u003e None:\n        \"\"\"Reset running average\"\"\"\n```\n\n\n### `torchutil.metrics.F1`\n\n```python\nclass F1(Metric):\n    \"\"\"Batch-updating F1 score\"\"\"\n\n    def __call__(self) -\u003e float:\n        \"\"\"Retrieve the current F1 value\n\n        Returns\n            The current F1 value\n        \"\"\"\n\n    def update(self, predicted: torch.Tensor, target: torch.Tensor) -\u003e None:\n        \"\"\"Update F1\n\n        Arguments\n            predicted\n                The model prediction\n            target\n                The corresponding ground truth\n        \"\"\"\n\n    def reset(self) -\u003e None:\n        \"\"\"Reset F1\"\"\"\n```\n\n\n### `torchutil.metrics.L1`\n\n```python\nclass L1(Metric):\n    \"\"\"Batch updating L1 score\"\"\"\n\n    def __call__(self) -\u003e float:\n        \"\"\"Retrieve the current L1 value\n\n        Returns\n            The current L1 value\n        \"\"\"\n\n    def update(self, predicted, target) -\u003e None:\n        \"\"\"Update L1\n\n        Arguments\n            predicted\n                The model prediction\n            target\n                The corresponding ground truth\n        \"\"\"\n\n    def reset(self) -\u003e None:\n        \"\"\"Reset L1\"\"\"\n```\n\n\n### `torchutil.metrics.MeanStd`\n\n```python\nclass MeanStd(Metric):\n    \"\"\"Batch updating mean and standard deviation\"\"\"\n\n    def __call__(self) -\u003e Tuple[float, float]:\n        \"\"\"Retrieve the current mean and standard deviation\n\n        Returns\n            The current mean and standard deviation\n        \"\"\"\n\n    def update(self, values: torch.Tensor) -\u003e None:\n        \"\"\"Update mean and standard deviation\n\n        Arguments\n            predicted\n                The model prediction\n            target\n                The corresponding ground truth\n        \"\"\"\n\n    def reset(self) -\u003e None:\n        \"\"\"Reset mean and standard deviation\"\"\"\n```\n\n\n### `torchutil.metrics.PearsonCorrelation`\n\n```python\nclass PearsonCorrelation(Metric):\n    \"\"\"Batch-updating Pearson correlation\"\"\"\n\n    def __init__(\n        self,\n        predicted_mean: float,\n        predicted_std: float,\n        target_mean: float,\n        target_std: float\n    ) -\u003e None:\n        \"\"\"\n        Arguments\n            predicted_mean - Mean of predicted values\n            predicted_std - Standard deviation of predicted values\n            target_mean - Mean of target values\n            target_std - Standard deviation of target values\n        \"\"\"\n\n    def __call__(self) -\u003e float:\n        \"\"\"Retrieve the current correlation value\n\n        Returns\n            The current correlation value\n        \"\"\"\n\n    def update(self, predicted, target) -\u003e None:\n        \"\"\"Update Pearson correlation\n\n        Arguments\n            predicted\n                The model prediction\n            target\n                The corresponding ground truth\n        \"\"\"\n\n    def reset(self) -\u003e None:\n        \"\"\"Reset Pearson correlation\"\"\"\n```\n\n\n### `torchutil.metrics.Precision`\n\n```python\nclass Precision(Metric):\n    \"\"\"Batch-updating precision metric\"\"\"\n\n    def __call__(self) -\u003e float:\n        \"\"\"Retrieve the current precision value\n\n        Returns\n            The current precision value\n        \"\"\"\n\n    def update(self, predicted: torch.Tensor, target: torch.Tensor) -\u003e None:\n        \"\"\"Update precision\n\n        Arguments\n            predicted\n                The model prediction\n            target\n                The corresponding ground truth\n        \"\"\"\n\n    def reset(self) -\u003e None:\n        \"\"\"Reset precision\"\"\"\n```\n\n\n### `torchutil.metrics.Recall`\n\n```python\nclass Recall(Metric):\n    \"\"\"Batch-updating recall metric\"\"\"\n\n    def __call__(self) -\u003e float:\n        \"\"\"Retrieve the current recall value\n\n\n            The current recall value\n        \"\"\"\n\n    def update(self, predicted: torch.Tensor, target: torch.Tensor) -\u003e None:\n        \"\"\"Update recall\n\n        Arguments\n            predicted\n                The model prediction\n            target\n                The corresponding ground truth\n        \"\"\"\n\n    def reset(self) -\u003e None:\n        \"\"\"Reset recall\"\"\"\n```\n\n\n### `torchutil.metrics.RMSE`\n\n```python\nclass RMSE(Metric):\n    \"\"\"Batch-updating RMSE metric\"\"\"\n\n    def __call__(self) -\u003e float:\n        \"\"\"Retrieve the current rmse value\n\n        Returns\n            The current rmse value\n        \"\"\"\n\n    def update(self, predicted: torch.Tensor, target: torch.Tensor) -\u003e None:\n        \"\"\"Update RMSE\n\n        Arguments\n            predicted\n                The model prediction\n            target\n                The corresponding ground truth\n        \"\"\"\n\n    def reset(self) -\u003e None:\n        \"\"\"Reset RMSE\"\"\"\n```\n\n\n## Notify\n\nTo use the `torchutil` notification system, set the `PYTORCH_NOTIFICATION_URL`\nenvironment variable to a supported webhook as explained in\n[the Apprise documentation](https://pypi.org/project/apprise/).\n\n```python\nimport torchutil\n\n# Send notification when function returns\n@torchutil.notify('train')\ndef train():\n    ...\n\n# Equivalent using context manager\ndef train():\n    with torchutil.notify('train'):\n        ...\n```\n\n\n### `torchutil.notify`\n\n```python\n@contextlib.contextmanager\ndef notify(\n    description: str,\n    track_time: bool = True,\n    notify_on_fail: bool = True):\n    \"\"\"Context manager for sending job notifications\n\n    Arguments\n        description - The name of the job being run\n        track_time - Whether to report time elapsed\n        notify_on_fail - Whether to send a notification on failure\n    \"\"\"\n```\n\n\n## Paths\n\n### `torchutil.paths.chdir`\n\n```python\n@contextlib.contextmanager\ndef chdir(directory: Union[str, bytes, os.PathLike]) -\u003e None:\n    \"\"\"Context manager for changing the current working directory\n\n    Arguments\n        directory\n            The desired working directory\n    \"\"\"\n```\n\nThis function is both a context manager and decorator.\n\n```python\nimport tempfile\nfrom pathlib import Path\n\nimport torchutil\n\n# Create a directory\ndirectory = tempfile.TemporaryDirectory()\n\n# Create a file\nfile = 'tmp.txt'\n(Path(directory.name) / file).touch()\n\n# File is not in current working directory\nassert not Path(file).exists()\n\n# Change working directory using context manager\nwith torchutil.paths.chdir(directory.name):\n    assert Path(file).exists()\n\n# File is not in current working directory\nassert not Path(file).exists()\n\n# Change working directory using decorator\n@torchutil.paths.chdir(directory.name)\ndef exists(file):\n    assert Path(file).exists()\nexists(file)\n\n# File is not in current working directory\nassert not Path(file).exists()\n\n# Remove temporary paths\ndirectory.cleanup()\n```\n\n\n### `torchutil.paths.measure`\n\n```python\ndef measure(\n    globs: Optional[List[Union[str, List[str]]]] = None,\n    roots: Optional[\n        List[\n            Union[\n                Union[str, bytes, os.PathLike],\n                List[Union[str, bytes, os.PathLike]]\n            ]\n        ]\n    ] = None,\n    recursive: bool = False,\n    unit='B'\n) -\u003e Union[int, float]:\n    \"\"\"Measure data usage of files and directories\n\n    Arguments\n        globs\n            Globs matching paths to measure\n        roots\n            Directories to apply glob searches; current directory by default\n        recursive\n            Apply globs to all subdirectories of root directories\n        unit\n            Unit of memory utilization (bytes to terabytes); default bytes\n\n    Returns\n        Data usage in the specified unit\n    \"\"\"\n```\n\nThis function also has a command-line interface.\n\n```\npython -m torchutil.paths.measure \\\n    [-h] \\\n    --globs GLOBS \\\n    [--roots ROOTS] \\\n    [--recursive] \\\n    [--unit]\n\nMeasure data usage of files and directories\n\narguments:\n  --globs GLOBS\n    Globs matching paths to measure\n\noptional arguments:\n  -h, --help\n    show this help message and exit\n  --roots ROOTS\n    Directories to apply glob searches; current directory by default\n  --recursive\n    Apply globs to all subdirectories of root directories\n  --unit\n    Unit of memory utilization (bytes to terabytes); default bytes\n```\n\n\n### `torchutil.paths.purge`\n\n```python\ndef purge(\n    globs: Optional[List[Union[str, List[str]]]] = None,\n    roots: Optional[\n        List[\n            Union[\n                Union[str, bytes, os.PathLike],\n                List[Union[str, bytes, os.PathLike]]\n            ]\n        ]\n    ] = None,\n    recursive: bool = False,\n    force: bool = False\n) -\u003e None:\n    \"\"\"Remove all files and directories within directory matching glob\n\n    Arguments\n        globs\n            Globs matching paths to delete\n        roots\n            Directories to apply glob searches; current directory by default\n        recursive\n            Apply globs to all subdirectories of root directories\n        force\n            Skip user confirmation of deletion\n    \"\"\"\n```\n\nThis function also has a command-line interface.\n\n```\npython -m torchutil.paths.purge \\\n    [-h] \\\n    --globs GLOBS \\\n    [--roots ROOTS] \\\n    [--recursive] \\\n    [--force]\n\nRemove files and directories\n\narguments:\n  --globs GLOBS\n    Globs matching paths to delete\n\noptional arguments:\n  -h, --help\n    show this help message and exit\n  --roots ROOTS\n    Directories to apply glob searches; current directory by default\n  --recursive\n    Apply globs to all subdirectories of root directories\n  --force\n    Skip user confirmation of deletion\n```\n\n\n## Tensorboard\n\n```python\nimport matplotlib.pyplot as plt\nimport torch\nimport torchutil\n\n# Directory to write Tensorboard files\ndirectory = 'tensorboard'\n\n# Training step\nstep = 0\n\n# Example audio\naudio = torch.zeros(1, 16000)\nsample_rate = 16000\n\n# Example figure\nfigure = plt.figure()\nplt.plot([0, 1, 2, 3])\n\n# Example image\nimage = torch.zeros(256, 256, 3)\n\n# Example scalar\nloss = 0\n\n# Update Tensorboard\ntorchutil.tensorboard.update(\n    directory,\n    step,\n    audio={'audio': audio},\n    sample_rate=sample_rate,\n    figures={'figure': figure},\n    images={'image': image},\n    scalars={'loss': loss})\n```\n\n\n### `torchutil.tensorboard.update`\n\n```python\ndef update(\n    directory: Union[str, bytes, os.PathLike],\n    step: int,\n    audio: Optional[Dict[str, torch.Tensor]] = None,\n    sample_rate: Optional[int] = None,\n    figures: Optional[Dict[str, matplotlib.figure.Figure]] = None,\n    images: Optional[Dict[str, torch.Tensor]] = None,\n    scalars: Optional[Dict[str, Union[float, int, torch.Tensor]]] = None):\n    \"\"\"Update Tensorboard\n\n    Arguments\n        directory - Directory to write Tensorboard files\n        step - Training step\n        audio - Optional dictionary of 2D audio tensors to monitor\n        sample_rate - Audio sample rate; required if audio is not None\n        figures - Optional dictionary of Matplotlib figures to monitor\n        images - Optional dictionary of 3D image tensors to monitor\n        scalars - Optional dictionary of scalars to monitor\n    \"\"\"\n```\n\n\n## Time\n\n```python\nimport time\nimport torchutil\n\n# Perform timing\nwith torchutil.time.context('outer'):\n    time.sleep(1)\n    for i in range(2):\n        time.sleep(1)\n        with torchutil.time.context('inner'):\n            time.sleep(1)\n\n# Prints {'inner': 2.0020763874053955, 'outer': 5.005248308181763, 'total': 5.005248308181763}\nprint(torchutil.time.results())\n```\n\n\n### `torchutil.time.context`\n\n```python\n@contextlib.contextmanager\ndef context(name: str):\n    \"\"\"Wrapper to handle context changes of global timer\n\n    Arguments\n        name - Name of the timer to add time to\n    \"\"\"\n```\n\n\n### `torchutil.time.reset`\n\n```python\ndef reset():\n    \"\"\"Clear timer state\"\"\"\n```\n\n\n### `torchutil.time.results`\n\n```python\ndef results() -\u003e dict:\n    \"\"\"Get timing results\n\n    Returns\n        Timing results: {name: elapsed_time} for all names\n    \"\"\"\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxrmorrison%2Ftorchutil","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaxrmorrison%2Ftorchutil","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxrmorrison%2Ftorchutil/lists"}