Skip to content

Instantly share code, notes, and snippets.

@ddh0
Last active May 16, 2026 00:12
Show Gist options
  • Select an option

  • Save ddh0/fc3c9e081fad332d5f76609687dd19eb to your computer and use it in GitHub Desktop.

Select an option

Save ddh0/fc3c9e081fad332d5f76609687dd19eb to your computer and use it in GitHub Desktop.
Python code to convert SDXL fp16 safetensors to 8-bit safetensors
# safetensors_convert_fp16_to_8bit.py
# Python 3.11.7
import safetensors.torch
import safetensors
import torch
import os
# blacklist takes priority over whitelist
# a tensor will only be cast if it matches the whitelist but not the blacklist
# list of substrings to search for within tensor names
tensor_name_blacklist = [
'emb' # embedding tensors
]
# list of substrings to search for within tensor names
tensor_name_whitelist = [
'diffusion_model'
]
fn = input('Enter the path of a safetensors FP16 model file: ')
if not os.path.exists(fn):
raise FileNotFoundError(
f"'{fn}' does not exist"
)
if not os.path.isfile(fn):
raise FileNotFoundError(
f"'{fn}' is not a file"
)
if not fn.endswith('.safetensors'):
raise ValueError(
f"filename '{fn}' does not end in '.safetensors'. only safetensors files are supported"
)
if 'fp16' in fn:
output_fn = fn.replace('fp16', '8-bit')
elif 'f16' in fn:
output_fn = fn.replace('f16', '8-bit')
elif 'FP16' in fn:
output_fn = fn.replace('FP16', '8-bit')
elif 'F16' in fn:
output_fn = fn.replace('F16', '8-bit')
else:
output_fn = fn.replace('.safetensors', '-8-bit.safetensors')
if os.path.exists(output_fn):
raise FileExistsError(
f"destination file '{output_fn}' already exists"
)
def maybe_reduce_precision_tensor(tensor: torch.Tensor, tensor_name: str) -> torch.Tensor:
"""
Convert the given tensor to 8-bit if it is float16, otherwise
return the tensor unchanged
"""
# do not cast tensors that are not fp16
if tensor.dtype not in [torch.float16, torch.half]:
print(f"SKIP: tensor {tensor_name}: {tensor.dtype} unchanged")
return tensor
# fp16 tensor -> 8-bit tensor
print(f"CAST: - tensor {tensor_name}: {tensor.dtype} -> torch.int8")
return tensor.char()
fp16_tensors: dict[str, torch.Tensor] = {}
# change `device='mps'` to `device='cpu'` if you are not using Metal
with safetensors.safe_open(fn, framework="pt", device='mps') as f:
for tensor_name in f.keys():
print(f"LOAD: tensor {tensor_name}")
fp16_tensors[tensor_name] = f.get_tensor(tensor_name)
_8bit_tensors: dict[str, torch.Tensor] = {}
for tensor_name in fp16_tensors.keys():
if any(string in tensor_name for string in tensor_name_blacklist):
print(f'COPY: tensor {tensor_name} is blacklisted')
_8bit_tensors[tensor_name] = fp16_tensors[tensor_name]
else:
if any(string in tensor_name for string in tensor_name_whitelist):
_8bit_tensors[tensor_name] = maybe_reduce_precision_tensor(
tensor=fp16_tensors[tensor_name],
tensor_name=tensor_name
)
else:
print(f'COPY: tensor {tensor_name} is not whitelisted')
_8bit_tensors[tensor_name] = fp16_tensors[tensor_name]
safetensors.torch.save_file(
tensors=_8bit_tensors,
filename=output_fn
)
print(f'saved 8-bit file to {output_fn}')
@caruttiwngdntri-spec

Copy link
Copy Markdown

-- Mobile Lock Head + ESP GUI
-- Clean English Script

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local LocalPlayer = Players.LocalPlayer
local Camera = workspace.CurrentCamera

local AimLock = false
local ESPEnabled = false
local MenuVisible = true
local Target = nil

-- GUI
local ScreenGui = Instance.new("ScreenGui")
ScreenGui.Parent = game.CoreGui
ScreenGui.Name = "MobileHub"

local MainFrame = Instance.new("Frame")
MainFrame.Parent = ScreenGui
MainFrame.Size = UDim2.new(0, 220, 0, 180)
MainFrame.Position = UDim2.new(0.05, 0, 0.35, 0)
MainFrame.BackgroundColor3 = Color3.fromRGB(15,15,15)
MainFrame.BorderSizePixel = 0
MainFrame.Active = true
MainFrame.Draggable = true

local UICorner = Instance.new("UICorner", MainFrame)
UICorner.CornerRadius = UDim.new(0,12)

local Title = Instance.new("TextLabel")
Title.Parent = MainFrame
Title.Size = UDim2.new(1,0,0,40)
Title.BackgroundTransparency = 1
Title.Text = "MOBILE HUB"
Title.TextColor3 = Color3.fromRGB(0,255,255)
Title.Font = Enum.Font.GothamBold
Title.TextScaled = true

-- Button Function
local function CreateButton(text, posY)
local Button = Instance.new("TextButton")
Button.Parent = MainFrame
Button.Size = UDim2.new(0.85,0,0,35)
Button.Position = UDim2.new(0.075,0,0,posY)
Button.BackgroundColor3 = Color3.fromRGB(25,25,25)
Button.TextColor3 = Color3.fromRGB(255,255,255)
Button.Font = Enum.Font.GothamBold
Button.TextScaled = true
Button.Text = text

local Corner = Instance.new("UICorner", Button)
Corner.CornerRadius = UDim.new(0,10)

return Button

end

local LockButton = CreateButton("Aim Lock : OFF", 50)
local ESPButton = CreateButton("ESP : OFF", 95)
local HideButton = CreateButton("Hide Menu", 140)

-- Find Closest Player
local function GetClosestPlayer()
local Closest = nil
local Distance = math.huge

for _, Player in pairs(Players:GetPlayers()) do
    if Player ~= LocalPlayer and Player.Character and Player.Character:FindFirstChild("Head") then
        local Head = Player.Character.Head
        local ScreenPos, Visible = Camera:WorldToViewportPoint(Head.Position)

        if Visible then
            local Magnitude = (Vector2.new(ScreenPos.X, ScreenPos.Y) - Vector2.new(Camera.ViewportSize.X/2, Camera.ViewportSize.Y/2)).Magnitude

            if Magnitude < Distance then
                Distance = Magnitude
                Closest = Player
            end
        end
    end
end

return Closest

end

-- Aim Lock
LockButton.MouseButton1Click:Connect(function()
AimLock = not AimLock

if AimLock then
    LockButton.Text = "Aim Lock : ON"
else
    LockButton.Text = "Aim Lock : OFF"
end

end)

RunService.RenderStepped:Connect(function()
if AimLock then
Target = GetClosestPlayer()

    if Target and Target.Character and Target.Character:FindFirstChild("Head") then
        Camera.CFrame = CFrame.new(Camera.CFrame.Position, Target.Character.Head.Position)
    end
end

end)

-- ESP
local function CreateESP(Player)
if Player == LocalPlayer then return end

local Highlight = Instance.new("Highlight")
Highlight.Name = "ESP"
Highlight.FillColor = Color3.fromRGB(0,255,255)
Highlight.OutlineColor = Color3.fromRGB(255,255,255)
Highlight.FillTransparency = 0.4
Highlight.OutlineTransparency = 0
Highlight.Enabled = ESPEnabled

local function Setup()
    if Player.Character then
        Highlight.Parent = Player.Character
    end
end

Setup()
Player.CharacterAdded:Connect(Setup)

end

for _, v in pairs(Players:GetPlayers()) do
CreateESP(v)
end

Players.PlayerAdded:Connect(CreateESP)

ESPButton.MouseButton1Click:Connect(function()
ESPEnabled = not ESPEnabled

if ESPEnabled then
    ESPButton.Text = "ESP : ON"
else
    ESPButton.Text = "ESP : OFF"
end

for _, Player in pairs(Players:GetPlayers()) do
    if Player.Character and Player.Character:FindFirstChild("ESP") then
        Player.Character.ESP.Enabled = ESPEnabled
    end
end

end)

-- Hide / Show Menu
HideButton.MouseButton1Click:Connect(function()
MenuVisible = not MenuVisible

for _, v in pairs(MainFrame:GetChildren()) do
    if v:IsA("TextButton") or v:IsA("TextLabel") then
        if v ~= HideButton then
            v.Visible = MenuVisible
        end
    end
end

if MenuVisible then
    HideButton.Text = "Hide Menu"
else
    HideButton.Text = "Show Menu"
end

end)###

@caruttiwngdntri-spec

Copy link
Copy Markdown

Reeed.lua

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment