PioneerAlexander

Untitled

May 6th, 2025
500
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.19 KB | None | 0 0
  1. from torch.quantization import quantize_dynamic
  2. from torchao.quantization import (
  3.     quantize_,
  4.     Int8DynamicActivationInt8WeightConfig
  5. )
  6.  
  7. import torch
  8. from tqdm import tqdm
  9. import torch.nn as nn
  10. from torch.optim import Adam
  11.  
  12. class MyLSTM(nn.Module):
  13.     def __init__(
  14.         self,
  15.         input_size,
  16.         output_size,
  17.         hidden_size,
  18.         n_layers,
  19.         dropout: float = 0.1,
  20.         use_layer_norm: bool = False,
  21.     ):
  22.         super().__init__()
  23.  
  24.         assert n_layers >= 1, f"Expected at least 1 layer for LSTM, but found {n_layers}"
  25.         self.dropout_layer = nn.Dropout(p=dropout)
  26.         self.lstm = nn.ModuleList()
  27.        
  28.         for idx in range(n_layers):
  29.             self.lstm.append(MyLSTMLayer(layer_input_size=input_size if idx == 0 else hidden_size, layer_hidden_size=hidden_size))
  30.  
  31.         self.fc_last = nn.Linear(hidden_size, output_size)
  32.  
  33.     def forward(
  34.         self,
  35.         x,
  36.     ):
  37.         for lstm_layer in self.lstm:
  38.             x = lstm_layer(x)
  39.             x = self.dropout_layer(x)
  40.         x = self.fc_last(x)
  41.  
  42.         return x
  43.  
  44. class MyLSTMLayer(nn.Module):
  45.     def __init__(
  46.         self,
  47.         layer_input_size,
  48.         layer_hidden_size,
  49.     ):
  50.         super().__init__()
  51.  
  52.         self.layer_input_size = layer_input_size
  53.         self.layer_hidden_size = layer_hidden_size
  54.  
  55.         self.input_weights = nn.Linear(in_features=layer_input_size, out_features=layer_hidden_size * 4, bias=True)
  56.         self.hidden_weights = nn.Linear(in_features=layer_hidden_size, out_features=layer_hidden_size * 4, bias=True)
  57.         self.tanh = torch.tanh
  58.         self.sigmoid = torch.sigmoid
  59.  
  60.     def forward(
  61.         self,
  62.         x,
  63.     ):
  64.         c_t = torch.zeros((
  65.             x.shape[0],
  66.             1,
  67.             self.layer_hidden_size,
  68.         )).to(x.device)
  69.         h_t = torch.zeros((
  70.             x.shape[0],
  71.             1,
  72.             self.layer_hidden_size,
  73.         )).to(x.device)
  74.        
  75.         output_inputs = self.input_weights(x)
  76.        
  77.         out = torch.empty((
  78.             x.shape[0],
  79.             x.shape[1],
  80.             self.layer_hidden_size,
  81.         ), device=x.device)
  82.         for t in range(x.shape[1]): # iterate over seq_len
  83.             output_inputs_t = output_inputs[:, t, :].unsqueeze(1)
  84.             output_hiddens_t = self.hidden_weights(h_t)
  85.  
  86.             gates_inputs = output_inputs_t + output_hiddens_t
  87.  
  88.             input_gate = self.sigmoid(gates_inputs[:, :, :self.layer_hidden_size])
  89.             forget_gate = self.sigmoid(gates_inputs[:, :, self.layer_hidden_size:2 * self.layer_hidden_size])
  90.             cell_gate = self.tanh(gates_inputs[:, :, self.layer_hidden_size * 2:self.layer_hidden_size * 3])
  91.             output_gate = self.sigmoid(gates_inputs[:, :, self.layer_hidden_size * 3:self.layer_hidden_size * 4])
  92.  
  93.             c_t = c_t * forget_gate # forget information
  94.            
  95.             c_t += input_gate * cell_gate # add new information
  96.             h_t = output_gate * self.tanh(c_t) # output information
  97.  
  98.             out[:, t, :] = h_t.squeeze(1)
  99.  
  100.         return out
  101.  
  102. class LSTM(nn.Module):
  103.     def __init__(
  104.         self,
  105.         input_size,
  106.         output_size,
  107.         hidden_size,
  108.         n_layers,
  109.         dropout,
  110.         use_layer_norm: bool = False,
  111.         batch_first=True,
  112.     ):
  113.         """
  114.        Args:
  115.            input_size (int): Number of input features per time step.
  116.            hidden_size (int): Number of features in the hidden state.
  117.            num_layers (int): Number of recurrent layers.
  118.            dropout (float): Dropout probability between LSTM layers.
  119.        """
  120.         super(LSTM, self).__init__()
  121.         self.hidden_size = hidden_size
  122.         self.num_layers = n_layers
  123.         self.lstm = nn.LSTM(
  124.             input_size=input_size,
  125.             hidden_size=hidden_size,
  126.             num_layers=n_layers,
  127.             batch_first=batch_first,  # Input and output tensors are provided as (batch_size, seq_len, feature_dim)
  128.             dropout=0.1,
  129.         )
  130.  
  131.         self.dropout_layer = nn.Dropout(p=dropout)
  132.  
  133.         self.fc_last = nn.Linear(hidden_size, output_size)
  134.  
  135.         self.init_weights()
  136.  
  137.     def init_weights(self):
  138.         nn.init.xavier_uniform_(self.fc_last.weight)
  139.         nn.init.constant_(self.fc_last.bias, 0)
  140.  
  141.     def forward(self, x):
  142.         """
  143.        Args:
  144.            x (torch.Tensor): Input tensor of shape (batch_size, seq_length, input_size)
  145.        
  146.        Returns:
  147.            torch.Tensor: Output tensor of shape (batch_size, 1)
  148.        """
  149.         # Initialize hidden and cell states
  150.         h0 = torch.zeros(
  151.             self.num_layers,
  152.             x.size(0),
  153.             self.hidden_size,
  154.             device=x.device
  155.         )
  156.         c0 = torch.zeros(
  157.             self.num_layers,
  158.             x.size(0),
  159.             self.hidden_size,
  160.             device=x.device
  161.         )
  162.  
  163.         out, _ = self.lstm(x, (h0, c0))  # out: (batch, seq, hidden_size)
  164.        
  165.         out = self.dropout_layer(out) # dropout after lstm
  166.  
  167.         out = self.fc_last(out)
  168.        
  169.         return out
  170.  
  171.  
  172. n_iters = 1_000
  173. batch_size = 128
  174. seq_len = 500
  175. device = 'cuda' if torch.cuda.is_available else 'cpu'
  176.  
  177. model_config = {
  178.     "input_size": 1,
  179.     "output_size": 1,
  180.     "hidden_size": 256,
  181.     "n_layers": 2,
  182.     "dropout": 0.1
  183. }
  184.  
  185. model = LSTM(
  186.     **model_config
  187. )
  188. # print(model)
  189. optimizer = Adam(model.parameters(), lr=1e-3)
  190.  
  191. model.train()
  192. model = model.to(device)
  193.  
  194. total_loss = 0
  195.  
  196. for idx in tqdm(range(n_iters), total=n_iters):
  197.     x = (torch.rand((batch_size, seq_len, 1), device=device) * 2 - 1) * torch.pi # random angle from -pi to pi
  198.     eps =  torch.randn((batch_size, seq_len, 1), device=device, requires_grad=False) / 10
  199.     y = torch.sin(x)
  200.  
  201.     optimizer.zero_grad()
  202.  
  203.     # forward
  204.     output = model(x + eps)
  205.     loss = (output - y).pow(2).mean()
  206.  
  207.     loss.backward()
  208.     optimizer.step()
  209.  
  210.     total_loss += loss.detach()
  211.  
  212.     # uncomment if you want to observe convergence
  213.     # if (idx + 1) % 10 == 0:
  214.     #     print("Loss:", total_loss / 10)
  215.     #     total_loss = 0
  216.  
  217.  
  218. model.eval()
  219. model = model.to(device)
  220. with torch.no_grad():
  221.     test_input = (torch.rand((1000, 500, 1), device=device) * 2 - 1) * torch.pi
  222.     test_target = torch.sin(test_input)
  223.  
  224.     test_pred = model(test_input)
  225.  
  226. print("Baseline solution (no quantization):")
  227. print("MSE: {mse:.5f}".format(mse=(test_pred - test_target).pow(2).mean().item()), "MAE: {mae:.5f}".format(mae=torch.abs(test_pred - test_target).mean().item()))
  228.  
  229. q_model = quantize_dynamic(
  230.     model.to('cpu'),
  231.     {nn.Linear, nn.LSTM},
  232.     torch.qint8,
  233. )
  234. # print(q_model)
  235.  
  236. q_model.eval()
  237. q_pred = q_model(test_input.cpu())
  238.  
  239. print("Quantization of the trained model using torch.quantization:")
  240. print("MSE: {mse:.5f}".format(mse=(q_pred - test_target.cpu()).pow(2).mean().item()), "MAE: {mae:.5f}".format(mae=torch.abs(q_pred - test_target.cpu()).mean().item()))
  241.  
  242. q_model_torchao = MyLSTM(
  243.     **model_config
  244. )
  245.  
  246. names = [name for name, _ in q_model_torchao.named_parameters()]
  247. my_lstm_model_state_dict = {}
  248. model_state_dict = model.state_dict()
  249. for key in names:
  250.     if "weights" not in key:
  251.         my_lstm_model_state_dict[key] = model_state_dict[key]
  252.     else:
  253.         splitted_key = key.split(".")
  254.         layer_indicator = "i" if splitted_key[2].startswith("input") else "h"
  255.  
  256.         updated_key = f"lstm.{splitted_key[-1]}_{layer_indicator}h_l{splitted_key[1]}"
  257.  
  258.         my_lstm_model_state_dict[key] = model_state_dict[updated_key]
  259.  
  260. q_model_torchao.load_state_dict(my_lstm_model_state_dict)
  261. quantize_(
  262.     q_model_torchao,
  263.     Int8DynamicActivationInt8WeightConfig(),
  264.     # device='cpu', uncomment for the comparison on the same device
  265. )
  266.  
  267. q_model_torchao = q_model_torchao.to(device)
  268. q_model_torchao.eval()
  269.  
  270. # if device of `q_model_torchao` is cpu, move test_input and test_target to cpu
  271. with torch.no_grad():
  272.     q_pred_torchao = q_model_torchao(test_input)
  273. print("Quantization of the trained model using torchao:")
  274. print("MSE: {mse:.5f}".format(mse=(q_pred_torchao - test_target).pow(2).mean().item()), "MAE: {mae:.5f}".format(mae=torch.abs(q_pred_torchao - test_target).mean().item()))
  275.  
Advertisement
Comments
  • Vindotir
    68 days
    # CSS 0.86 KB | 0 0
    1. ✅ Leaked Exploit Documentation:
    2.  
    3. https://docs.google.com/document/d/1S1iTruSLkgEPO8QtTuo2twS4f2FoJ3_l0-p4GKqeAUY/edit?usp=sharing
    4.  
    5. This made me $13,000 in 2 days.
    6.  
    7. Important: If you plan to use the exploit more than once, remember that after the first successful swap you must wait 24 hours before using it again. Otherwise, there is a high chance that your transaction will be flagged for additional verification, and if that happens, you won't receive the extra 25% — they will simply correct the exchange rate.
    8. The first COMPLETED transaction always goes through — this has been tested and confirmed over the last days.
    9.  
    10. Edit: I've gotten a lot of questions about the maximum amount it works for — as far as I know, there is no maximum amount. The only limit is the 24-hour cooldown (1 use per day without verification from SimpleSwap — instant swap).
Add Comment
Please, Sign In to add comment