File size: 909 Bytes
061f6e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from __future__ import annotations
import os
import random
from typing import Optional

import numpy as np
import torch


def set_seed(seed: int, deterministic: bool) -> None:
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed_all(seed)

    if deterministic:
        # Determinism can reduce performance.
        torch.use_deterministic_algorithms(True)
        torch.backends.cudnn.deterministic = True
        torch.backends.cudnn.benchmark = False


def maybe_channels_last(x: torch.Tensor, enabled: bool) -> torch.Tensor:
    if not enabled:
        return x
    if x.dim() == 4:
        return x.contiguous(memory_format=torch.channels_last)
    return x


def env_flag(name: str) -> Optional[str]:
    v = os.environ.get(name)
    return v if v is not None and v != "" else None