torch-einops-utils 0.0.23__tar.gz → 0.0.24__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: torch-einops-utils
3
- Version: 0.0.23
3
+ Version: 0.0.24
4
4
  Summary: Personal utility functions
5
5
  Project-URL: Homepage, https://pypi.org/project/torch-einops-utils/
6
6
  Project-URL: Repository, https://github.com/lucidrains/torch-einops-utils
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "torch-einops-utils"
3
- version = "0.0.23"
3
+ version = "0.0.24"
4
4
  description = "Personal utility functions"
5
5
  authors = [
6
6
  { name = "Phil Wang", email = "lucidrains@gmail.com" }
@@ -0,0 +1,8 @@
1
+ import torch
2
+ from torch import nn
3
+
4
+ def test_module_device():
5
+ from torch_einops_utils.device import module_device
6
+
7
+ assert module_device(nn.Linear(3, 3)) == torch.device('cpu')
8
+ assert module_device(nn.Identity()) is None
@@ -0,0 +1,20 @@
1
+ from itertools import chain
2
+
3
+ import torch
4
+ from torch.nn import Module
5
+
6
+ # helpers
7
+
8
+ def exists(v):
9
+ return v is not None
10
+
11
+ # infer the device for a module
12
+
13
+ def module_device(m: Module):
14
+
15
+ first_param_or_buffer = next(chain(m.parameters(), m.buffers()), None)
16
+
17
+ if not exists(first_param_or_buffer):
18
+ return None
19
+
20
+ return first_param_or_buffer.device