Skip to content

Instantly share code, notes, and snippets.

@joelburget
Created July 15, 2024 00:14
Show Gist options
  • Save joelburget/fd18a958add96a108a0455633da2e1aa to your computer and use it in GitHub Desktop.
Save joelburget/fd18a958add96a108a0455633da2e1aa to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 16,
"id": "b0374368-eb08-4ceb-8c8e-c8f409aa7213",
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"from transformers import AutoModel"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "94c38ffa-afbf-4056-a0f0-9015a3e18fc9",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "4a33d7f3c8534fdb9f59d733343101b9",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"hf_model = AutoModel.from_pretrained(\"joelb/Mixtral-8x7B-1l\")"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "090a1f47-a067-4c0d-ba1a-a50db075957c",
"metadata": {},
"outputs": [],
"source": [
"w = hf_model.layers[0].block_sparse_moe.experts[0].w3.weight\n",
"w2 = w.T.contiguous().T\n",
"i = torch.randn(3, 4096)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "32aad9b4-9b9b-4f96-9222-450e0c3789f6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor(False)"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"torch.all(i @ w.T == i @ w2.T)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "ddabe036-b8e6-486e-8fd9-ca2d6eabf195",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"((4096, 1), (1, 14336))"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"w.stride(), w2.stride()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment